#! /bin/sh -

# --------- YOU SHOULD NOT NEED TO CHANGE ANYTHING BELOW --------
# This version of the "build" script will ask you for parameters.
# Just run it... :-)
#
# The only parameter which is always set to default is the location of your
# sources (3 lines below). You can change it by editing the script.

# You can set the SRC variable which points to your source tree. It's
# /usr/src by default (most people shouldn't change it).
SRC=/usr/src


set -e

# Build kernel with previously set parameters.

build_kernel() {
	echo ""
	echo "-> We must make the PICOBSD${suffix}.${SIZE} kernel first..."
	pwd=`pwd`
	cat ../${TYPE}/conf/PICOBSD | grep -v "MFS_ROOT">${CONF}/PICOBSD${suffix}.${SIZE}
	echo "options	\"MFS_ROOT=${SIZE}\"" >>${CONF}/PICOBSD${suffix}.${SIZE}
	cd ${CONF}
	config PICOBSD${suffix}.${SIZE}
	cd ../../compile/PICOBSD${suffix}.${SIZE}
	make depend && make
	cd ${pwd}
}

# Main build procedure. It calls other scripts (stage1-3 and populate)
main() {

if [ "${TYPE}" = "dial" ]
then
	suffix="-D"
fi
if [ "${TYPE}" = "net" ]
then
	suffix="-N"
fi
if [ "${TYPE}" = "isp" ]
then
	suffix="-I"
fi

CONF="${SRC}/sys/i386/conf"

clear
echo "-> Building with following parameters:"
echo "	Type:     ${TYPE}"
echo "	MFS size: ${SIZE} kB"
echo "	Language: ${LANGUAGE}"
echo ""
echo "-> We'll use the sources living in ${SRC}"
echo ""
echo "-> I hope you have checked the ../${TYPE}/conf/PICOBSD config file..."
echo ""
echo ""
sleep 2

# Now check if we must build the kernel first

if [ ! -f ${SRC}/sys/i386/conf/PICOBSD${suffix}.${SIZE} ]
then
	build_kernel
elif [ ! -f ${SRC}/sys/compile/PICOBSD${suffix}.${SIZE}/kernel ]
then
	build_kernel
fi

for i in stage1 populate stage2 stage3
do
	echo "====================== ${i} started ====================="
	./${i} ${SIZE} ${LANGUAGE} ${TYPE} ${SRC}
	if [ "X$?" != "X0" ]
	then
		echo ""
		echo "-> ERROR in \"${i}\" script. Aborting the build process."
		echo -n "-> Cleaning temporary files... "
		umount -f /mnt
		vnconfig -u /dev/rvn0
		rm -f kernel* fs.PICOBSD picobsd.flp *.o *core
		echo "Done."
		exit 10
	else
	echo "==================== ${i} completed ====================="
	fi
done
}

# Set the LANGUAGE variable

set_lang() {
	clear
	echo "                    Language setup"
	echo ""
	echo "Language can be either 'en' (English - default) or 'pl' (Polish)"
	echo ""
	read -p "Enter the LANGUAGE (en, pl): " LANGUAGE
	if [ "X${LANGUAGE}" = "X" ]
	then
		LANGUAGE=en
	fi
}

# Set the default MFS size depending on the type of setup

set_dflt_size() {
	echo ""
	if [ "X${TYPE}" = "X" ]
	then
		TYPE=dial
	fi
	echo "Setting default MFS size for \"${TYPE}\" type floppy."
	if [ "X${TYPE}" = "Xdial" ]
	then
		SIZE=2200
	elif [ "X${TYPE}" = "Xnet" ]
	then
		SIZE=2800
	elif [ "X${TYPE}" = "Xisp" ]
	then
		SIZE=2800
	else
		echo "Unknown type of setup: \"${TYPE}\". Aborting..."
		exit 10
	fi
}

# Set MFS size interactively

set_size() {
	clear
	echo "            Memory Filesystem (MFS) Size setup"
	echo ""
	echo " Size can be anything decent (usually 1700 or 2500) in kB."
	echo " NOTE1: you can also use other numbers (e.g. 1500, 1456, 1789 ...)"
	echo " even much bigger (like 4567), but keep in mind that this memory is"
	echo " totally lost to other programs. Usually you want to keep this as small as"
	echo " possible."
	echo ""
	echo " NOTE2: for pre-canned setups there are specific requirements:"
	echo "	dial - requires at least SIZE=2200"
	echo "	net  - requires at least SIZE=2800 (2200 if you remove SNMP agent)"
	echo "	isp  - requires at least SIZE=2800"
	echo "  The last two configurations are not likely to run reliably on machines"
	echo " with less than 10MB of RAM, while the 'dial' is tested and proved to run"
	echo " as many as ~30 processes on 10 consoles with only 8MB RAM. YMMV."
	echo ""
	read -p "Enter the SIZE in kB: " SIZE
	if [ "X${SIZE}" = "X" ]
	then
		set_dflt_size
	fi
}

# Set type of floppy interactively

set_type() {
	clear
	echo "               Setup the type of configuration"
	echo ""

	echo " Type can be either 'dial', 'net' or 'isp'. There are three sets of"
	echo " configuration files in subdirs ../dial, ../net and ../isp respectively -"
	echo " the contents of the floppy is constructed basing on the Makefiles and"
	echo " scripts in them."
	echo ""
	echo " E.g. if you define TYPE=dial, you'll end up having a floppy which is"
	echo " suitable for dialup access and not much else. If you define TYPE=net,"
	echo " you'll have a small router-fixit-like floppy, which lacks some"
	echo " interactive tools. TYPE=isp gives you a dialin server floppy."
	echo ""
	read -p "Enter the TYPE of the floppy (dial, net, isp): " TYPE
	if [ "X${TYPE}" = "X" ]
	then
		TYPE=dial
	fi
	set_dflt_size
}

put_title() {
	clear
	echo "                        Building the PicoBSD floppy"
	echo "============================================================================"
	echo ""
}


#-------------------------------------------------------------------
# Main entry of the script

# If $1="package", it creates a neat set of floppies

if [ "X$1" = "Xpackage" ]
then
	touch build.status
	echo "##############################################" >>build.status
	echo "## `date` ">>build.status
	echo "##############################################" >>build.status
	./clean dial
	for y in en pl
	do
		for z in dial net isp
		do
			TYPE=${z}
			LANGUAGE=${y}
			set_dflt_size
			echo "---------------------------------------------">>build.status
			echo "Building TYPE=${z}, LANGUAGE=${y}, SIZE=${SIZE}" >>build.status
			main
			if [ "X$?" != "X0" ]
			then
				echo "	** FAILED! **">>build.status
			else
				echo "	(ok)">>build.status
			fi
			mv picobsd.flp pb_${y}${suffix}.flp
			echo "Calling ./clean for ${TYPE}, ${LANGUAGE}, ${SIZE}">>build.status
			./clean ${TYPE}
		done
	done
	exit 0
fi

# Set build parameters interactively

TYPE=dial
SIZE=2200
LANGUAGE=en
while [ "X${ans}" != "Xn" ]
do
	put_title
	echo "Current build parameters are as follows:"
	echo ""
	echo "	1.	Type:     ${TYPE}"
	echo "	2.	MFS size: ${SIZE} kB"
	echo "	3.	Language: ${LANGUAGE}"
	echo ""
	echo "Which parameter would you like to change?"
	read -p "(1, 2, 3; n -no change, build it ; q -quit): " ans
	case ${ans} in
	1)	set_type
		clear
		;;
	2)	set_size
		clear
		;;
	3)	set_lang
		clear
		;;
	q)	echo ""
		echo "Hey! Don't give up so quickly. Quitting for now..."
		echo ""
		exit 0
		;;
	n)	;;
	*)	echo "Unknown option \"${ans}\". Try again."
		sleep 2
		clear
		;;
	esac
done
# Call the build procedure
main
# Install if it's ok.
echo ""
if [ "X$?" = "X0" ]
then
	echo "The build process was completed successfuly."
	echo ""
	echo "Now we are going to install the image on the floppy."
	./install
fi
exit 0
