#!/usr/bin/perl
# aD! BBS 3.0 (c) Chris Church / aD! Data Systems
#
#------------------------------------------------------

require 5.001;

use less 'memory';

require '/usr/local/etc/adbbs.cf';
require '/usr/local/etc/strings.ph';
require 'bbs-lib.pl';

if($ansi == 1) {
	&rtpi("$AMDIR/mod_ansi.adbbs");
	}
if($uisr == 1) {
	&rtpi("$AMDIR/mod_intstr.adbbs");
	}
if($sllogin == 1) {
	&rtpi("$AMDIR/mod_lasttime.adbbs");
	}
if($ufil == 1) {
	&rtpi("$AMDIR/mod_fbase.adbbs");
	}
if($ugroup == 1) {
	&rtpi("$AMDIR/mod_group.adbbs");
	}

$nop = 'NOOP';

$| = 1;
$done = 0;


sub do_pre_wel {

	 &clear;

	 $MPWF = "/usr/local/etc/prewel";

	 if(-r $MPWF) {
		*LF = \$MPWF;
		open(LF);
		while(<LF>) {
			&crp($_);
			}
		undef($MPWF);
		}		
	 
	 if($skwel == 1) {
		 &crp($stasw);
		 &ckc;
		 &show_welcome if($enval =~ /n/i);
		 } else {
			sleep(5);
			&show_welcome;
			}
         if($shownews == 1) {
		 if($skwel == 1) {
			 &do_news if($enval =~ /y/i);
			 $done = 1 if($enval =~ /y/i);
			 } else {
				&do_news;
				$done = 1
				}
		} else {
			return;
			}

} 

sub show_welcome {

 &clear;
 opendir(WDIR, $WDIR) || die("Could Not Open Directory $WDIR -> $!\n");
 @wfiles = grep(!/^\./, readdir(WDIR));
 closedir(WDIR);

 foreach $i (0 .. $#wfiles) {

	$f = $i;
	$f++;

	$CURFILE = "$WDIR/welcome.$f";
	open(CURFILE) || die("Could Not Open $curfile -> $!\n");

	while(<CURFILE>) {
		print;
		}

	&eprom;
	&clear;

	}
}	  	

sub do_news {
	
 &clear;
 opendir(NDIR, $NDIR) || die("Could Not Open Directory $ndir -> $!\n");
 @nfiles = grep(!/^\./, readdir(NDIR));
 closedir(NDIR);

 foreach $i (0 .. $#nfiles) {
	
	$f = $i;
	$f++;
	&crp($stasn);
	$CURFILE = "$NDIR/news.$f";

	open(CURFILE) || die("Could Not Open $CURFILE -> $!\n");

	while(<CURFILE>) {
		print;
		}

	&eprom;
	&clear;

	}
 undef(@nfiles);
 
}	

						
sub do_men {

 die("$stant") if(! -t STDOUT);
 &checkcolor('fore', 7);

format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$line1,                     $line2
.

 $menu = shift(@_);
 $CURM = "$MDIR/$menu.cf";
 $AMEN = "$MDIR/$menu.scr";
 $bad = 0;
 $pray = 0;

 $mrpti = "$MDIR/$menu.pi";

 &rtpi($mrtpi) if(-r $mrpti);

######################
# idea for menu.scr from uncle bob
######################

 $pray = 1 if(-r $AMEN);

 $UMEN = "$home/$menu.cf";
 if(-r $UMEN) {
	$CURM = $UMEN;
	$pray = 0;
	}
 
 undef(@opts); undef(@progs); undef(@mens); undef(@keys);


 
 open(CURM) || die("Could Not Open $menu Menu File: $!\n"); ;# you get the point
 while(<CURM>) {    ;# open up that big bad config file =)

	next if /^\s*#/ || /^$/;

	if(/\s*group\s*(.*)/i) {
		$cgid = $1;
		&get_ustat("$id","$cgid");
		if((&get_ustat("$id","$cgid")) != 0) {
			$bad = 1;
			} else { next; }
		}

	if(/\s*option\s*(.*)/i) {
		push(@opts, $1);
	}

	if(/\s*prog\s*(.*)/i) {  ;# parse that puppy
		push(@progs, $1);
		push(@mens, $nop);
		} 
			elsif (/^\s*menu\s*(.*)/) {
				push(@mens, $1);
				push(@progs, $nop);
				} 
	if(/\s*mkey\s*(.*)/i) {
		push(@keys, $1);
		}
	}
 close(CURM);

 if($bad == 1) {
	&clear;
	&crp($stanna);
	&eprom;
	&do_men("main");
	}

 while (1) {

	&clear;
	if($pray != 1) {
		&crp($stamh) if($shmmsg == 1);
		&crp($staea) if($menu eq 'mail' && $shmmsg == 1);
		if(-r "$MTOP\.$menu" && $rndmtp != 1) {
			$OMTOP = $MTOP . ".$menu";
			open(OMTOP);
			while(<OMTOP>) {
				&crp($_);
				}
			close(OMTOP);
			undef($OMTOP);
			} elsif($rndmtp == 1) {
				opendir(RNDIR, $RNDIR) || die("Unable to open directory $RNDIR -> $!\n");
				@mtfiles = grep(!/^\./, readdir(RNDIR));
				closedir(RNDIR);
				srand(time); 
				$num = int(rand($#mtfiles));
				$LTF = "${RNDIR}/$mtfiles[$num]";
				open(LTF);
				while(<LTF>) {
					&crp($_);
					}
				close(LTF); undef($LTF);
			} else {
					open(MTOP);
					while(<MTOP>) {
						print;
						}
				}
		}

	die("Error In $menu Menu File!\n") unless ($#opts == $#progs);
	die("Error In $menu Menu File!\n") unless ($#progs == $#mens);
	die("Error In $menu Menu File!\n") unless ($#mens == $#keys);

	if($pray != 1) {
		&checkcolor('fore', 7);
		$i = 0;
		while($i <= $#opts) {  ;# print out formatted options
		
			$line1 = "\($keys[$i]\) $opts[$i]";
			$i++;
			$line2 = "\($keys[$i]\) $opts[$i]";	
			$line2 = "" unless ($keys[$i] ne '');
			write; 
			$i++;
			}

		print("\n\n");
		} else { 
			open(AMEN) || die("Unable to open $AMEN -> $!\n");
			while(<AMEN>) {
				print;
				}
			}
	if($uisr == 1) {
		$fukcnt++;
	        if($fukcnt >= 50) {
			die("aD!BBS Exiting: Internal Error, failed -> looping");
			}
		&int_str;
		} else {
			&ckc;
			$fukcnt++;
		        if($fukcnt >= 50) {
				die("aD!BBS Exiting: Internal Error, failed -> looping");
				}
			foreach $i (0 .. $#keys) {  ;# parse the input
				if ($enval eq $keys[$i] || ( $enval =~ /\w+\b/ && $enval =~ /$keys[$i]/i)) {
					$fukcnt = 0;
					if ($progs[$i] eq $nop) {
						&do_men($mens[$i]);
						} else { &int_intcalls($progs[$i]); last; }
				
					}
				}
			}

	}
}			
	
	

		
;# setup some variables, and then print the initial screen
 $tdinr = $FDIR;
 &int_envvars;
 &clear;
 &user_defs;
 &get_ttype;
 &last_time;
 &rtpi("$MDIR/prerun.pi") if(-r "$MDIR/prerun.pi");
 &do_pre_wel if($showwel == 1);
 unless($done == 1) { &do_news if($shownews == 1); }

 if ($showmail == 1) {
	 &crp($stamn);
	 &check_messages;
	 print "\n\n";
	 &eprom;
 }

 &do_men("main");

;# all done!

  