#!/usr/bin/perl
#
# cd /glftpd/ftp-data/users; convall.pl
# "results will be in /glftpd/ftp-data/new directory"
# cd ../new; cp * /.ftp-data/users
# "and now you have gftpd userbase"


@LS=qx{ls -1};
foreach $user (@LS) {

undef @IPLIST;
undef @GROUPLIST;
undef @GROUPLIST2;
$level=1;
$now=time;
@GLFTPD=qx{cat $user};
foreach $line (@GLFTPD) {
	if($line =~/^GENERAL (.*?) (.*?) (.*?) (.*?) (.*?)\n/) {
		$ratio=$1;
		$weekly=$2;
		$maxlogins=$3;
		$credits2=$4;
		$maxspeed=$5;
	}
	if($line =~/^CREDITS (.*?)\n/) {
		$credits=$1;
	}
	if($line =~/^UNFO (.*?)\n/) {
                $unfo=$1;
        }
	if($line =~/^ALLUP (.*?) (.*?) (.*?)\n/) {
		$alupfiles=$1;
		$alupbytes=$2;
		$aluptime=$3;
	}
	if($line =~/^ALLDN (.*?) (.*?) (.*?)\n/) {
		$aldnfiles=$1;
		$aldnbytes=$2;
		$aldntime=$3;
	}
	if($line =~/^WKUP (.*?) (.*?) (.*?)\n/) {
		$wkupfiles=$1;
		$wkupbytes=$2;
		$wkuptime=$3;
	}
	if($line =~/^WKDN (.*?) (.*?) (.*?)\n/) {
		$wkdnfiles=$1;
		$wkdnbytes=$2;
		$wkdntime=$3;
	}
	if($line =~/^DAYUP (.*?) (.*?) (.*?)\n/) {
		$dayupfiles=$1;
		$dayupbytes=$2;
		$dayuptime=$3;
	}
	if($line =~/^DAYDN (.*?) (.*?) (.*?)\n/) {
		$daydnfiles=$1;
		$daydnbytes=$2;
		$daydntime=$3;
	}
	if($line =~/^MONTHUP (.*?) (.*?) (.*?)\n/) {
		$monupfiles=$1;
		$monupbytes=$2;
		$monuptime=$3;
	}
	if($line =~/^MONTHDN (.*?) (.*?) (.*?)\n/) {
		$mondnfiles=$1;
		$mondnbytes=$2;
		$mondntime=$3;
	}
	if($line =~/^NUKE (.*?) (.*?) (.*?)\n/m) {
		$nukestamp=$1;
		$timesnuked=$2;
		$megsnuked=$3;
	}
	if($line =~/^TIME (.*?) (.*?) (.*?) (.*?) (.*?)\n/) {
		$logins=$1;
		$laston=$2;
		$timelimit=$3;
		$timeonline=$4;
	}
	if($line=~/^GROUP (.*?)\n/m) {
		push(@GROUPLIST,$1);
	}
	if($line=~/^PRIVATE (.*?)\n/m) {
		push(@GROUPLIST2,$1);
	}
	if($line=~/^IP (.*?)\n/m) {
		push(@IPLIST,$1);
	}
	if($line=~/^FLAGS (.*?)\n/m) {
		$flags=$1;
#group sysop
		if($flags=~/2/) {
			$gadmin=1;
		}
#exempt_from_limit  1
		if($flags=~/4/) {
			$exempt=1;
		}
#use_color 1
		if($flags=~/5/) {
			$color=1;
		}
#level 6
		if($flags=~/A/) {
			$level=6;
		}
#level 6
		if($flags=~/B/) {
			$level=6;
		}
#level 7
		if($flags=~/C/) {
			$level=7;
		}
#level 8
		if($flags=~/G/) {
			$level=8;
		}
#level 19
		if($flags=~/1/) {
		$level=19;
		}
	}
}
open NEW,">../new/$user";
print NEW <<END

# General information:
ratio $ratio
level $level
restrict /home/ftp
tagline $unfo
num_logins $maxlogins
exempt_from_limit $exempt
credits $credits
weeklimit $weekly

# Normal Xfer information:
files_up $alupfiles
bytes_up $alupbytes
seconds_up $aluptime
files_down $aldnfiles
bytes_down $aldnbytes
seconds_down $aldntime
files_up_wk $wkupfiles
bytes_up_wk $wkupbytes
seconds_up_wk $wkuptime
files_down_wk $wkdnfiles
bytes_down_wk $wkdnbytes
seconds_down_wk $wkdntime
files_up_m $monupfiles
bytes_up_m $monupbytes
seconds_up_m $monuptime
files_down_m $mondnfiles
bytes_down_m $mondnbytes
seconds_down_m $mondntime


# Time-related information:
login_times $logins
last_on $laston
account_created $now
added_by convertscript
comment 
time_limit $timelimit
time_on_today $timeonline
last_nuked 0
use_color $color
quiet 0
speed_fuckups 0
week_bad 0
all_bad 0
friend 0

# Groups for $user
group_sysop Not_a_group_sysop 0 0
END
;
foreach $group (@GROUPLIST) {
	print NEW "group_user $group\n";
}
foreach $group2 (@GROUPLIST2) {
	print NEW "group_priv $group2\n";
}

print NEW "# Approved IP's for $user\n";
foreach $ip (@IPLIST) {
	print NEW "ip $ip\n";
}
close (NEW);
print STDERR $user;
}
