#!/bin/sh
#
# linuxrc - main trinux initialization script
# 
# Author:	Matthew Franz <mfranz@cisco.com>
#               Bill Burdick for the User-Mode-Linux fixes
########################################################################
#
# Attempts to do the following:
#
# -  Determine where trinux booted from [ Floppy/CD-ROM/IDE/UML ]
# -  Mount boot device and copy configuration files to /etc/tux
# -  Initialize and create ramdisks/tmpfs or mount filesystems
# -  Determine where packages are located and to be loaded from
# -  Initialize kernel modules and detect hardware
# -  Configure and enable networking
# -  Load and initialize packages
# -  Initialize system daemons
#
########################################################################
## Define stuff that is probably rejected by busybox
########################################################################

UMLROOT="/trinux-uml"
PATH="/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin:/usr/local/sbin"
TERM=linux
PS1="trinux> "
PS2='>'

export PATH TERM PS1 PS2 VERSION PKGSRC BOOT PKGSRC BOOTFS FLCFG FXCFG PKGLIST FIXED 
umask 022
[ -d /etc/proc ] || mkdir -p /etc/proc
mount -t proc proc /proc
mkdir -p /lib/modules
mkdir -p /etc/tux/config
ln -sf /proc/self/fd/0 /dev/stdin
ln -sf /proc/self/fd/2 /dev/stderr
ln -sf /proc/self/fd/1 /dev/stdout

chmod a+w /tmp
chmod a+w /dev/null

#####################################################################
## Gather information about system
#####################################################################

MEMORY=`grep MemTotal /proc/meminfo | cut -d':' -f2 | cut -d'k' -f1`
KERNEL=`cat /proc/version | cut -d' ' -f3`
CDEV=`dmesg | grep D-ROM | grep hd | cut -d: -f1 | sort | uniq`  # CD-ROM devices
FDEV=`dmesg | grep -v LDM | grep -v "ldm_val" | grep "^ [sh]d[a-f]" | cut -d':' -f2 | sort | uniq`  # FIXED

#echo "FDEV: $FDEV"
#echo "CDEV: $CDEV"
#sleep 1

echo "$FDEV" > /tmp/fixed


for i in `cat /tmp/fixed`
do
	echo $i 
done  > /etc/proc/fixed


[ "$CDEV" ] && echo "$CDEV" >> /etc/proc/fixed


#check initial filesytem support

for i in `grep -v nodev /proc/filesystems`
do
	echo $i 
done > /etc/proc/filesystems


######################################################################
### Special Probing stuff
######################################################################

## Did we boot from UML?
#if dmesg | grep 'User-mode Linux' > /dev/null

if grep ' ubd' /proc/devices > /dev/null
then
	echo "Trinux booted via User Mode Linux (UML)"
    sleep 3
    UML="true"
    BOOT="UML"
fi

## Did we boot from ms-dos loadlin

echo "Checking all vfat partitions for loadlin.exe"

for i in `cat /etc/proc/fixed 2> /dev/null`
do
#	[ -d /${i} ] || mkdir /${i}
#	echo "Checking $i"

	if mount -o ro -t vfat /dev/${i} /mnt 2> /dev/null
	then
		echo -n "VFAT found on $i"
		if ls /mnt/loadlin.exe 2> /dev/null
		then
			echo -n " found loadlin.exe"
			if [ -d /mnt/trinux ]
			then
				echo " found trinux"
				BOOT="/dev/${i}"
				BOOTFS="vfat"
				echo "BOOT=$BOOT"
				echo "$i" > /etc/proc/fixedpkg
				LOADLIN="true"
				cd /
				umount /mnt 2> /dev/null
				break
			fi		
		fi
		
	fi
	umount /mnt 2> /dev/null
done

# Check to see if checking for floppy drive should be disabled

if grep nofloppy /proc/cmdline > /dev/null
then
	NOFLOPPY="true"
	echo "Disabling checks for floppy drive"
fi

########################################################################
# Determine where Trinux booted from in order of likelihood
#
#   /dev/fd0         - floppy (this is normally the case)
#   /dev/hd[alpha]   - IDE CD-ROM (usually /dev/hdc)
#   /dev/hd[numeric] - IDE Hard Disk (including flash disk)
#
#  We assume that if a floppy is present and no-›
#
########################################################################
#
# first check if CD-ROM is present

if [ "$CDEV" ]
then
	echo -n "Attempting to mount CD-ROM at $CDEV:"

	if mount -t iso9660 /dev/${CDEV} /mnt 2> /dev/null > /dev/null
	then
		echo "successful"
		if [ -d /mnt/trinux ]
		then
			echo "/trinux found on $CDEV"
			PKGSRC=$CDEV # we assume packages will be loaded from it
			
			# Now check and see if we booted from CD-ROM

			if [ -d /mnt/isolinux ]
			then
				BOOT=/dev/${CDEV}
				BOOTFS="iso9660"
				NOFLOPPY="true"
				echo $CDEV > /etc/proc/fixedpkg
			fi
			cd /
			umount /mnt 2> /dev/null
		fi
	else
		echo "failed"
		[ "$UML" ] || BOOT="/dev/fd0"
	fi
fi

if [ ! "$NOFLOPPY" ]
then
	echo "Checking for floppy in /dev/fd0"
	sleep 1

	if mount -t vfat /dev/fd0 /boot 2> /dev/null
	then
		[ -d /boot/bootpkg ] && BOOT="/dev/fd0"
		[ -d /boot/tux ] && FLCFG="true"
		[ -f /boot/tux/options/floppy ] && PKGSRC="/dev/fd0"
		umount /boot 2> /dev/null

		[ "$BOOT" = "/dev/fd0" ] && echo "Trinux booted from a floppy."
		[ "$PKGSRC" = "/dev/fd0" ] && echo "Trinux will load packages from one or more floppies."
	else
		echo "Unable to mount boot floppy"
	fi
else
	echo "Floppy checking is disabled"
fi

echo $BOOT > /etc/proc/boot
echo $BOOTFS > /etc/proc/bootfs

echo
echo "========================"
echo "BOOT: $BOOT"
echo "KERNEL: $KERNEL"
echo -n "CDEV: "

if [ $CDEV ]
then
	echo "$CDEV"
else
	echo "none" 
fi

echo -n "FDEV: " 

if [ "$FDEV" ]
then
	echo $FDEV
else
	echo "none" 
fi


## Determine boot filesystem type

if [ "$BOOT" = "/dev/fd0" ] 
then
	BOOTFS="vfat"
elif [ "$UML" ]
then
	BOOTFS="ext2"
elif [ "$BOOT" = "$CDEV" ]
then
	BOOTFS="iso9660"	
fi

echo "BOOTFS: $BOOTFS"
echo "========================"
sleep 2

####################################################################
# Determine where packages should be loaded from. By default, Trinux
# will look at /etc/tux/config/pkgsrc otherwise it will look at 
# parameters passed from syslinux.cfg (i.e. pkgsrc=/dev/hda1). 
# Otherwise Trinux will assume network package loading unlesss
# the network is unable to be configured
####################################################################

if grep 'pkg=floppy' /proc/cmdline > /dev/null
then
	PKGSRC="/dev/fd0"
fi

PKGSRC="network"   ## Assume network package loading unless...

#####################################################################
## Mount the boot filesystem
## 1) copy config from tux over
## 2) install kernel module pkgs from kpkg
#####################################################################

if [ "$UML" ]
then
	echo 
	# mount the host ext2 filesystem RO so initial packages can be installed
	mount none /boot -t hostfs -o $UMLROOT,ro
else
	mount -o ro -t $BOOTFS $BOOT /boot
	VERSION=`cat /boot/version 2> /dev/null`
	PS1="Trinux $VERSION> "
fi

[ "$BOOT" != "/dev/fd0" ] && PREFIX="trinux/"

cd /boot

#pwd
#ls -al

echo "Copying /tux from boot filesystem..." 
cp -a ${PREFIX}tux /etc


echo "Copying modules from boot filesystem..."

# support either within /trinux/modules or /trinux/*.o

cp ${PREFIX}*.o /lib/modules  2> /dev/null
cp ${PREFIX}modules/*.o /lib/modules  2> /dev/null
cp ${PREFIX}modules/*.gz /lib/modules  2> /dev/null

cd /lib/modules

for i in `ls *.gz`
do
	FNAME=`basename $i .gz`
	echo "Uncompressing $i"
	gunzip $i
	mv $FNAME $FNAME.o
done

if [ -f /etc/tux/version ]
then
	VERSION=`cat /etc/tux/version 2> /dev/null`
else
	VERSION="unknown"
fi

cd /boot
if cd ${PREFIX}kpkg 2> /dev/null
then
	echo "Installing kernel module packages from boot device"
	for i in `ls`
	do
		pkgadd $i 
		echo $i >> /etc/proc/kpkg.installed
	done
fi

######## Store temporary scripts in scripts directory ###########

if [ -d ${PREFIX}scripts ]
then
	echo "Copying local scripts to /bin:"
	cd ${PREFIX}scripts
	for i in `ls`
	do	
		cp $i /bin/
		chmod u+x /bin/$i
	done
fi

cd /
umount /boot

echo "Unmounting boot filesystem"

# load in kernel modules

#cd /lib/modules

if [ -f /etc/tux/config/modules ]
then
	while read i
	do
		echo "Installing $i"
		insmod /lib/modules/$i	2> /dev/null > /dev/null
	done < /etc/tux/config/modules
fi 

#### Since modules have already been loaded now is a good
#### time to scour the partitions, checking for devices

echo

#### Check for a saved fixedpkg to avoid checking all found partitions

if [ -f /etc/tux/config/fixedpkg ] 
then
	echo -n "Found previously saved fixed package source: "
	FIXEDPKG=`cat /etc/tux/config/fixedpkg`
	if [ "$FIXEDPKG" = "cdrom" -o "$FIXEDPKG" = "cd-rom" ]
	then
		FIXEDPKG=$CDEV
	fi

	echo $FIXEDPKG	
	echo $FIXEDPKG > /etc/proc/fixed
fi 

for i in `cat /etc/proc/fixed`  # loop through fixed devices
do
	[ -d /${i} ] || mkdir /${i}  # create mount point

	for j in `cat /etc/proc/filesystems` # loop through supported fstype
	do
		#echo -n "Checking $i:" 
        if mount -o ro -t $j /dev/$i /$i  2> /dev/null > /dev/null
	 	then
     	   #echo " found $j"
           echo "$i:$j" >> /etc/tux/config/fstab
           umount /dev/$i 2> /dev/null > /dev/null 2> /dev/null
        else
            echo
        fi
	done
done

if [ -f /etc/tux/config/fstab ]
then 
	echo "The following partitions/filesytems were found:"
	cat /etc/tux/config/fstab
	cat /etc/tux/config/fstab | sort | uniq > /tmp/fstab
	cat /tmp/fstab > /etc/tux/config/fstab
fi

######### 
# Search filesystems for /trinux directories 

if [ -f /etc/proc/fixedpkg ]
then
	fixedpkg=`cat /etc/proc/fixedpkg`
	echo "/trinux already found on $fixedpkg, aborting search"
	mount
else
	if [ -f /etc/tux/config/fstab ]
	then  
		for i in `cat /etc/tux/config/fstab`
		do
			part=`cut -d: -f1 /etc/tux/config/fstab`
			fstype=`cut -d: -f2 /etc/tux/config/fstab`

			if mount -t $fstype /dev/${part} /mnt 2> /dev/null
			then
				echo -n "Checking $part for trinux directory: "

				if [ -d /mnt/trinux/ ]	
				then
					echo " found"	
					umount /mnt 2> /dev/null
					echo $part > /etc/proc/fixedpkg
					break
				else
					echo " not found"
				fi

				cd /; umount /mnt 2> /dev/null
			else
				echo -n "Failed to mount $part"
			fi
		done
	fi
fi

chmod a+x /etc/tux/init/first 2> /dev/null
if [  -x /etc/tux/init/first ]
then
	. /etc/tux/init/first
fi

#################################################################
################### Ramdisk building stuff ######################
#################################################################

if grep ' 2.4' /proc/version 2> /dev/null
then
	echo "Found 2.4.x kernel: tmpfs capable"
	if [ -f /etc/tux/config/tmpfs ]
	then
		echo "Using tmpfs"
		TMPFS="true"
	fi
else
	TMPFS="false"
fi

#### If /etc/tux/config/corefs is present we use that for 
#### partitions *other* than root. If fstab is blank then
#### everything is mounted as root. Trinux will attempt to mount
#### to filesystem and will *not* reformat
####
#### An additional file (/etc/tux/config/suppfs) will be mounted
#### later so that nfs/smbfs can be mounted
####
#### corefs format
#### partition:mount point:size in Kb:filesystem:options 
####
#### /dev/sda1:/var:512:minix:keep,ro
####
####
#### possible filesystems
####   vfat, minix, tmpfs, ram (actually minix)
####
#### plus anything compiled in your kernel

if [ -f /etc/tux/config/corefs ]
then   
	for i in `cat /etc/tux/config/corefs`
	do 
		DEV=`echo $i | cut -d: -f1`
		MOUNT=`echo $i | cut -d: -f2`
		SIZE=`echo $i | cut -d: -f3`
		FSTYPE=`echo $i | cut -d: -f4`
		OPTIONS=`echo $i | cut -d: -f5`

		echo "[DEV: $DEV]"
		echo "MOUNT: $MOUNT"
		echo "SIZE: $SIZE"
		echo "FSTYPE: $FSTYPE"
		echo "OPTIONS: $OPTIONS"

		if [ ! "$DEV"  ]
		then 
			# if no device is specified, assume tmpfs
			mount -n -t tmpfs -o size=${SIZE}000 /dev/null $MOUNT
		else
			mkdir $MOUNT 2> /dev/null

			if grep "$FSTYPE" /proc/filesystems > /dev/null
			then
					if [ ! "$SIZE" ]
					then

						# assume entire partition should formatted
						if [ "$FSTYPE" = "minix" ]
						then
							mkfs.minix $DEV
							mount -t minix $DEV $MOUNT
						else
							echo "Currently only minix is supported: skipping $$DEV"
						fi

					fi # not $SIZE
			else 
				# if no FSTYPE is specified, assume minix
				mkfs.minix $DEV # 2> /dev/null > /dev/null
				mount -t minix $DEV $MOUNT 
			fi

		fi	# no $DEV
	done # loop through each line of corefs
else
	#
	# Autosize ramdisks based on amount of RAM discovered
	#

	VAR=""
	USR=""
	USRLOC=""
	HOMEP=""

	echo "RAM: $MEMORY MB"
	echo -n "RAM is "

	if [ $MEMORY -lt 16000 ]
	then
		echo "< 16MB, you may run into problems"
		USR="2048"
		HOMEP="128"
		VAR="512"
		PKGMEM="min"
	#	echo "USR: $USR USRLOC: $USRLOC HOME: $HOMEP"
	elif [ $MEMORY -lt 24000 ]
	then
		echo "> 16MB"
		USR="4096"
		HOMEP="256"
		VAR="512"
		PKGMEM="16"
	#	echo "USR: $USR USRLOC: $USRLOC HOME: $HOMEP"
	elif [ $MEMORY -lt 32000 ]
	then
		echo "> 24MB"
		USR="8192"
		HOMEP="512"
		VAR="1024"
		PKGMEM="24"
	#	echo "USR: $USR USRLOC: $USRLOC HOME: $HOMEP"
	elif [ $MEMORY -lt 48000 ]
	then
		echo "> 32MB"
		USR="16384"
		HOMEP="1024"
		VAR="1024"
		PKGMEM="32"

	#	echo "USR: $USR USRLOC: $USRLOC HOME: $HOMEP"
	elif [ $MEMORY -lt 64000 ]
	then
		echo "> 48MB"
		USR="16384"
		USRLOC="8192"
		HOMEP="2048"
		VAR="2048"
		PKGMEM="48"
	#	echo "USR: $USR USRLOC: $USRLOC HOME: $HOMEP"
	elif [ $MEMORY -lt 96000 ]
	then
		echo "> 64MB"
		USR="16384"
		USRLOC="16384"
		HOMEP="4096"
		VAR="2048"
		PKGMEM="64" 
	#	echo "USR: $USR USRLOC: $USRLOC HOME: $HOMEP"
	elif [ $MEMORY -lt 128000 ]
	then
		echo "> 96MB"
		USR="16384"
		USRLOC="32768"
		HOMEP="8192"
		VAR="4192"
		PKGMEM="96"
	#	echo "USR: $USR USRLOC: $USRLOC HOME: $HOMEP"
	else
		echo "> 128MB"
		USR="16384"
		USRLOC="16384"
		HOMEP="16384"
		VAR="4192"
		PKGMEM="128"
	#	echo "USR: $USR USRLOC: $USRLOC HOME: $HOMEP"
	fi


	if [ "$USR" != "" ]
	then
		echo "Creating /usr"
	#	echo "USR: $USR USRLOC: $USRLOC HOME: $HOMEP"

		if [ "$TMPFS" = "true" ]
		then
			mount -n -t tmpfs -o size=${USR}000 /dev/null /usr
		else
			mkdir /usr 2> /dev/null
			mkfs.minix /dev/ram3 $USR 2> /dev/null > /dev/null
			mount -t minix /dev/ram3 /usr
		fi
	fi

	if [ "$USRLOC" -gt 0 ]
	then

		echo "Creating /usr/local"
	#	echo "USR: $USR USRLOC: $USRLOC HOME: $HOMEP"
		mkdir /usr/local 2> /dev/null

		if [ "$TMPFS" = "true" ]
		then
			mount -n -t tmpfs -o size=${USRLOC}000 /dev/null /usr/local
		else
			mkfs.minix /dev/ram4 $USRLOC 2> /dev/null > /dev/null
			mount -t minix /dev/ram4 /usr/local
		fi
	fi

	if [ "$HOME" != "" ]
	then
		echo "Creating /home"
		mkdir /home 2> /dev/null
		if [ "$TMPFS" = "true" ]
		then
			mount -n -t tmpfs -o size=${HOMEP}000 /dev/null /home
		else
			mkfs.minix /dev/ram5 $HOMEP 2> /dev/null > /dev/null
			mount -t minix /dev/ram5 /home
		fi
	fi

	#
	# Need to add a check for "physical" var
	#

	if [ "$VAR" != "" ]
	then
		echo "Creating /var"
		mkdir /var 2> /dev/null
		if [ "$TMPFS" = "true" ]
		then
			mount -n -t tmpfs -o size=${VAR}000 /dev/null /var
		else
			mkfs.minix /dev/ram5 $VAR 2> /dev/null > /dev/null
			mount -t minix /dev/ram6 /var
		fi
	fi

fi

############################################################################
#
# handle /etc/tux/config/permfs
#
# mount "permanent filesystems" -- for now these should be umounted
# manually to avoid corruption 
#
# partition:mount point:filesystem:options 
#
############################################################################




if [ -f /etc/tux/config/permfs ]
then
	echo "Mounting permanent (non-ramdisk) filesystems based on /etc/tux/config/permfs"

	for  i in `/etc/tux/config/permfs`
	do
		DEV=`cat /etc/tux/config/permfs | cut -d":" -f1`
		MOUNT=`cat /etc/tux/config/permfs | cut -d":" -f2`
		FSTYPE=`cat /etc/tux/config/permfs | cut -d":" -f3`

		if mount -t $FSTYPE $DEV $MOUNT
		then
			echo "-- $DEV ($FSTYPE) was mounted at $MOUNT"
		else
			echo "-- $DEV ($FSTYPE) failed to mount at $MOUNT!"
		fi
	done

	echo "Remember, these must be manually umounted prior to reboot"
fi

echo "--------------------- FILESYSTEM CONFIGURATION ------------------------"
df
sleep 1
echo "-----------------------------------------------------------------------"
echo

#################################################################
#
# Kludgy stuff to do after the filesystems have been created
#
################################################################

echo "Creating misc links and directories..."

mkdir -p /lib/modules/`uname -r`
touch /lib/modules/`uname -r`/modules.dep
mkdir /usr/lib/ 2> /dev/null
ln -sf /lib/terminfo /usr/lib/terminfo
mkdir /usr/home
cd /var
mkdir pkg log spool run 2> /dev/null
cd log
cp /dev/null mtp
cd /
echo

if [ -f /etc/proc/fixedpkg -o "$UML" ]
then
	if [ -f /etc/proc/fixedpkg ]
	then
		part=`cat /etc/proc/fixedpkg 2> /dev/null`
		echo "Trinux packages were found on $part"
		PKGSRC=$part

	 	cat /etc/tux/config/fstab | grep $part | cut -d: -f2 2> /dev/null

	 	fstype=`cat /etc/tux/config/fstab | grep $part | cut -d: -f2 2> /dev/null`
	fi
	
	##echo "[PKGSRC] = $PKGSRC"
	##echo "[fstype] = $fstype"

	if [ "$fstype" ]
	then
		if mount -t $fstype /dev/${part} /mnt 2> /dev/null
		then
			mounted="true"	
		fi
	fi



	if [ "$UML" ]
	then
		echo -n "Attempting to mount UML host filesystem: "
	 	if mount none /mnt -t hostfs -o $UMLROOT,ro	 
		then
			mounted="true"
			echo "ok"
		else
			echo "failed"
		fi
	fi

	echo "[mounted] = $mounted"

	if [ "$mounted" ]
	then
		echo "Preparing to load base packages from $part"		
		if cd /mnt/trinux/bootpkg 2> /dev/null
		then
			for i in `ls *.tgz 2> /dev/null` 
			do
				pkgadd $i
			done
		else
			"No base packages found, hmmm..."
		fi

		echo "Preparing to load kernel packages from $part"		
		if cd /mnt/trinux/kpkg 2> /dev/null
		then	
			if [ -f /etc/tux/conf/kpkglist ]
			then	
				for i in `cat /etc/tux/conf/pkglist`
				do
					pkgadd $i
				done
			else
				echo "No kpkglist found, loading everything"
				for i in `ls *.tgz 2> /dev/null` 
				do
					if grep $i /etc/proc/kpkg.installed > /dev/null	
					then
						echo "$i is already installed."
					else
						pkgadd $i
					fi
				done
				
			fi
		else
			echo "Kernel package directory not found on $part!"
		fi

		echo "Preparing to load packages from $part"
		
		if cd /mnt/trinux/pkg 2> /dev/null
		then	
			if [ -f /etc/tux/conf/pkglist ]
			then	
				for i in `cat /etc/tux/conf/pkglist`
				do
					pkgadd $i
				done

			# NOTE: now the desired behavior is to put whatever
			# you want to load into bootpkg
			#
			#else
			#	echo "No pkglist found, loading everything"
			#	for i in `ls *.tgz 2> /dev/null` 
			#	do 
			#		pkgadd $i
			#	done
				
			fi
		else
			echo "Package directory not found on $part!"
		fi

		cd /
		umount /mnt
	else
		echo -n "Failed to mount $part"
	fi
else
	### This should only go for a floppy
	### Load required packages from boot device
	### echo "For some reason we made it here"

	sleep 1

	if mount -t $BOOTFS $BOOT /boot 2> /dev/null
	then
		if [ "$PKGSRC" != "$CDEV" ]
		then 
			if cd /boot/bootpkg 
			then
				echo "Installing base packages from boot device"
				for i in `ls`
				do
					pkgadd $i 
				done
				echo
			fi
		fi
		cd / 
		umount /boot
	else
		echo "Unable to mount $BOOT on $BOOTFS"
	fi
	cd /
	umount /boot 2> /dev/null
fi


[ -x /etc/init.d/pcmcia ] && . /etc/init.d/pcmcia

sleep 1




############### Syslog Configuration ###################

LOGHOST=`cat /etc/tux/options/loghost 2> /dev/null`
if [ "$LOGHOST" ]
then
	if [ "$LOGHOST" = "prompt" ]
	then
		echo -n "Enter syslog server: "
		read $LOGHOST
	fi
	$REMOTELOG="-R $LOGHOST"
fi

echo "Starting syslogd"
[ -x /sbin/syslogd ] && syslogd $REMOTELOG

#
# At this point we have loaded all the packages if a CD-ROM
# boot or package load from fixed partition
# otherwise base packages have been loaded from the 
# floppy.  We also have any saved config data. Now we attempt 
# to configure the network, then load packages from an HTTP/FTP server 
#

chmod a+x /etc/tux/init/prenet 2> /dev/null
if [  -x /etc/tux/init/prenet ]
then
	. /etc/tux/init/prenet
fi


##### Attempt to initialize networking #######################


ifconfig lo 127.0.0.1 up



# [ "$IPADDR" ] && echo "Network configured successfully"


if [ -f /etc/tux/config/eth0 ]
then 
	/sbin/net-start
	IPADDR=`cat /etc/proc/ipaddr 2> /dev/null`
else
	echo 'No network configuration found. Type "net-config"'
fi

if [ -f /etc/tux/config/proxy ] 
then
	SNARF_PROXY=`cat /etc/tux/config/proxy`
	export SNARF_PROXY
	echo "Found proxy: $SNARF_PROXY"
	echo
fi

if [ "$PKGSRC" = "network" ]
then
	
	if [ "$IPADDR" != "" ]
	then
		echo "The network was succesfully configured"

		[ -f /etc/tux/config/pkglist ] && PKGLIST=`cat /etc/tux/config/pkglist`

		# attempt to connect to servers in /etc/tux/config/server

		#snarf $server | grep tgz | cut -d">" -f3 | cut -d"<" -f1 > /tmp/packages

		#echo "3" > /proc/sys/net/ipv4/tcp_syn_retries
		#echo "3" > /proc/sys/net/ipv4/tcp_retries1
		#echo "3" > /proc/sys/net/ipv4/tcp_retries2

		if [ -f /etc/tux/config/server ]
		then

				for SERVER in `cat /etc/tux/config/server`
				do
					echo "Contacting $SERVER..."
					if snarf $SERVER > /dev/null 2> /dev/null
					then
						getpkg all
				#			echo; echo "Retrieving base packages from $SERVER"
				#		for i in $PKGLIST
				#		do
				#			getpkg $i $SERVER
				#		done
				
						if  [ -f /etc/tux/config/kpkglist ] 
						then
							KPKGLIST=`cat /etc/tux/config/kpkglist`
							echo "Retrieving Linux $KERNEL packages from $SERVER"
							for i in $KPKGLIST
							do
								getpkg $KERNEL/${i} $SERVER
							done
						fi

						break
					fi
				done
		fi
	else		
		PKGSRC="/dev/fd0"
	fi
fi 

if [ "$PKGSRC" = "/dev/fd0" ]
then
	echo -n "Do you have a package disk? "
	read response

	until  [ "$response" != "y" ]
	do
		if mount -t vfat -r /dev/fd0 /floppy 2> /dev/null
		then
			cd /floppy

			if ls *.o > /dev/null 2> /dev/null
			then
				echo "Copying modules from package disk"
				cp *.o /lib/modules 2> /dev/null
				cp modules/*.o  /lib/modules 2> /dev/null
			fi

			for i in *.tgz
			do
				cd /floppy
				pkgadd $i
			done	
		fi
		
		cd /; umount /floppy
		echo -n "Do you have another floppy? "
		read response
	done


	#if ifconfig | grep eth0 /dev/null 2> /dev/null
	#then
	#		echo "Network was sucessfully initialized"
	#else
	#net-start
	# fi
else
	if echo $PKGSRC | grep "/dev/[sh]d" > /dev/null
	then
		echo "Packages will be loaded from fixed disk: $PKGSRC"
	fi
fi


if [  -x /etc/tux/init/postnet ]
then
	chmod a+x /etc/tux/init/postnet 2> /dev/null
	. /etc/tux/init/postnet
fi

# Install modules that end up in /lib/modules
# this will include any on the boot disk
#
# Consult /etc/tux/config/modules for any parameters
# (i.e. irq's io's for NIC modules
#

# This should be moved to /etc/init.d/system

echo "Enabling port scan (scanlogd)"
[ -x /sbin/scanlogd ] && scanlogd


### Fix the linker/library stuff
if [ -x /usr/sbin/ldconfig ]
then
	echo "Rebuilding ld.so.cache one last time"
	ldconfig -v 2> /dev/null > /dev/null
else
	echo "Unable to rebuild run-time link bindings"
fi


chmod a+x /etc/tux/init/prepkg 2> /dev/null
if [  -x /etc/tux/init/prepkg ]
then
	. /etc/tux/init/prepkg
fi

[ -x /usr/bin/bash ] && ln -sf /usr/bin/bash /bin/sh



## This needs to be fixed *bad*

##/sbin/loadmodules # load any last minute kernel modules

chmod a+x /etc/tux/init/last 2> /dev/null
if [  -x /etc/tux/init/last ]
then
	. /etc/tux/init/last
fi

# Increasing syn_retries to something more reasonable
echo "7" > /proc/sys/net/ipv4/tcp_syn_retries

######################################################################
# Set up swap space
######################################################################

SWAPCONF=`cat /etc/tux/config/swap 2> /dev/null`

if [ "$SWAPCONF" ]
then
	echo "Configuring swap space"

	[ -x /sbin/mkswap ] || getpkg swaputil

	SWAPFILE=`echo $SWAPCONF | cut -d: -f1`
	SWAPSIZE=`echo $SWAPCONF | cut -d: -f2`

	[ "$SWAPSIZE" ] || SWAPSIZE=8192     # default to 8MB swap
	
	if echo "$SWAPFILE" | grep "dev" > /dev/null 	
	then
		# user specified a partition
		echo "Attempting to use $SWAPFILE for swap partition"
	else
		echo "Using $SWAPFILE as your swap file"
		dd if=/dev/zero of=$SWAPFILE bs=512 count=$SWAPSIZE
	fi

	mkswap $SWAPFILE
	swapon $SWAPFILE
	free
fi


####

if [ -f /etc/tux/config/ipfwd ]
then
	echo "Enabling IP forwarding"
	echo "1" > /proc/sys/net/ipv4/ip_forward
fi

if [ -f /etc/tux/config/noping ]
then
	echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all	
fi



#### This stuff should all get moved to the system direcotry ######
###
#### Notify any emails addr in /etc/tux/options/mailmon that you're up

if [ -f /etc/tux/config/monitor/smtp ]
then
    for ADDR in `cat /etc/tux/config/emaildst`
    do
        echo | mail $ADDR UP
    done
fi

if [ -f /etc/tux/config/monitor/http ]
then
	webmon
	# add to crontab
	echo '0,10,20,30,40,50 * * * * /usr/sbin/webmon' >> /etc/tux/config/crontab
fi

if [ -f /etc/tux/config/monitor/cdp ]
then
	echo "Starting CDP (Cisco Discovery Protocol)"
	echo '* * * * * /usr/sbin/cdpdump' >> /etc/tux/config/crontab
fi


if [ -f /etc/tux/config/crontab ]
then
	if /usr/sbin/crond 2> /dev/null
	then
		echo "Starting crond"
		sort /etc/tux/config/crontab | uniq > /tmp/crontab 2> /dev/null
		cat /tmp/crontab > /etc/tux/config/crontab 2> /dev/null
		/usr/sbin/crontab /etc/tux/config/crontab 2> /dev/null
		crontab -l
		echo
	else
		echo "Unable to start crond"
	fi
else
	echo "No crontab config found, not starting"
fi

### Erase /floppy/tux/options/console to disable console access
### 
##if [ -f /etc/tux/options/console ]
##then
##	[  -f /etc/tux/config/home ] && gethome
##else
##	grep -v askfirst /etc/inittab > /etc/inittab.new
##	rm /etc/inittab
##	mv /etc/inittab.new /etc/inittab 
##fi

### Add a hack for dropbear with fixed partition


# check for custom /etc/inittab

if [ -f /etc/tux/config/inittab ]
then
	echo "Found custom inittab"
	cp /etc/tux/config/inittab /etc/inittab
else
	echo "Using default inittab"
fi

if [ -x /etc/init.d/dropbear ]
then
	killall dropbear >2
	echo "Restarting dropbear"
	/etc/init.d/dropbear
fi

### This shouldn't be necessary, but just in case

if [ -x /usr/local/sbin/sshd ]
then
	if ps | grep -v sshd > /dev/null
	then
		echo "SSH server failed to start, trying again"
		/etc/init.d/opensshd	
	fi
fi

cat /README | tr '\t' ' ' >  /usr/doc/trinux

#################################################################

sleep 1 
clear
echo -n "Welcome to Trinux: A Linux Security Toolkit" 
if [ "$VERSION" ]
then
	echo -n " (version "
	echo -n $VERSION
	echo ")"
fi
echo
echo "Type 'man' for a list of help topics or 'man trinux' for docs."
echo "ALT-<Left/Right> allows you to view other virtual terminals. " 
echo
