#!/bin/sh -

#
# $FreeBSD: src/release/picobsd/build/build,v 1.12.2.7 1999/10/17 23:05:11 dwhite Exp $
#
# 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

# --------- YOU SHOULD NOT NEED TO CHANGE ANYTHING BELOW -----------
# The "build" script will ask you for parameters. Just run it... :-)
# --------- YOU SHOULD NOT NEED TO CHANGE ANYTHING BELOW -----------

# EDITOR is the editor you use
EDITOR=${EDITOR:-vi}
# LANGUAGE     language to be used (en or pl)
LANGUAGE=en
# SITE         site-specific files
SITE=

# FLOPPY_SIZE  floppy size in KB (default to 1440)
FLOPPY_SIZE=1440

# DEVFS is currently broken. Always set this.
#
NO_DEVFS=yes

# RISU is where dialog replies go.
RISU=.build.reply

# abort in case of error...
set -e

. ../Version

# support symbolically linked custom directory
BUILDDIR=`pwd`
export BUILDDIR

# assign default values to variables.
set_defaults() {
    TYPE=${TYPE:-dial} # PicoBSD type (isp, router, net, custom, ...

    while read name suff sz init mfs_in floppy_in ; do
	if [ "$name" != "#" -a $name != "" ] ; then
	    eval ${name}_suffix=$suff
	    eval ${name}_DFLT_SIZE=$sz
	    eval ${name}_INIT=$init
	    eval ${name}_MFS_INODES=$mfs_in
	    eval ${name}_FLOPPY_INODES=$floppy_in
	fi
    done <<__eof__
# build	MFS		- Byte/inode -
# name		suffix	DFLT_SZ	init	MFS	FLOPPY
dial		-D	1600	init	4096	32768
router		-R	820	oinit	3072	32768
net		-N	2200	init	4096	32768
isp		-I	2400	init	4096	32768
install		-S	2400	init	4096	4096
custom		-C	2400	init	8192	32768
__eof__
}

set_msgs() {
    MSG1="Type: ${TYPE}"
    if [ "X${TYPE}" = "Xcustom" ] ; then
	MSG1="${MSG1} (from ${CUST_DIR})"
    fi

    MSG="PicoBSD build -- Current parameters:\n\n\t1.  ${MSG1}\n\
\t2.  MFS size: ${SIZE} kB\n\t3.  Language: ${LANGUAGE}\n\
\t4.  Site-info: ${SITE}\n"
}


# Main build procedure. It calls another scripts (stage1)
main() {

    export suffix

    clear
    set_msgs
    printf "${MSG}"
    echo "-> We'll use the sources living in ${SRC}"
    echo ""
    echo "-> I hope you have checked the ../${TYPE}/PICOBSD config file..."
    echo ""
    echo ""
    sleep 2

    export SIZE LANGUAGE TYPE SRC NO_DEVFS INIT
    export SITE MFS_INODES FLOPPY_INODES FLOPPY_SIZE

    # Build the kernel, if necessary
    echo ""
    echo "-> Checking if we have to build the PICOBSD kernel..."
    (cd ../${TYPE}; make -f ${BUILDDIR}/Makefile.conf )
    echo ""

    ./stage1
    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
	./clean ${TYPE}
	echo "Done."
	exit 10
    fi
    echo "==================== ${i} completed ====================="
}

# check for existing custom config directory and ask user if they want to use it
# 
# Note that specifying an alternate directory is broken due to relative
# path assumptions during the kernel compilation phase of the build.
# For now just dialog-ify.

check_for_old_custom() {
   KEEP_CUSTOM=no
   if [ ! -d ../custom ] ; then
       return
   fi
   
   CUSTOMLINK=`ls -l ../custom | awk -F\> '{ print $2 }' | awk '{ print $1 }'`
   if dialog --title "Custom directory setup" --yesno "A custom configuration\n ${CUSTOMLINK}\nalready exists. Would you like to use it?" 7 50 ; then 
       KEEP_CUSTOM=yes
       CUST_DIR=${CUSTOMLINK}
   else 
       KEEP_CUSTOM=no
   fi

   # If custom is just a simple directory, assume the user wants it

}

# Ask for, and set the custom config directory

set_custom() {
    dialog --title "Custom directory setup" --inputbox \
    "Please enter the full path to the directory containing your \
    custom setup. \
    This directory tree must be laid out exactly like the standard \
    ones. (E.g. you can type in /home/abial/work/custom, which \
    should contain crunch1/, floppy.tree/, lang/ )." \
    10 70 2> ${RISU}
    CUST_DIR=`cat ${RISU}`
}

# Set type of floppy interactively. Also set the various defaults
# associated to each type. We can override them manually afterwards.

set_type() {

    eval SIZE=$"${TYPE}_DFLT_SIZE"
    eval INIT=$"${TYPE}_INIT"
    eval MFS_INODES=$"${TYPE}_MFS_INODES"
    eval FLOPPY_INODES=$"${TYPE}_FLOPPY_INODES"
    eval suffix=$"${TYPE}_suffix"

    case ${TYPE} in
    custom)
	check_for_old_custom
	if [ "X${KEEP_CUSTOM}" != "Xyes" ] ; then
	    [ -r ../custom ] && rm -f ../custom
	    set_custom
	    (cd ..; ln -sf ${CUST_DIR} custom)
        fi
	;;
    esac
	
}


build_package() {
    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 router net isp ; do
	    TYPE=${z}
	    LANGUAGE=${y}
	    set_type
	    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.bin pb_${y}${suffix}.bin
	    echo "Calling ./clean for ${TYPE}, ${LANGUAGE}, ${SIZE}">>build.status
	    ./clean ${TYPE}
	done
    done
    exit 0
}

# Set build parameters interactively

main_dialog() {
  while [ true ] ; do
    set_msgs
    dialog --menu "PicoBSD build menu -- ver ${VER} (22aug1999)" 19 70 12 \
	N "No change, build it" \
	T "${MSG1}" \
	K "edit Kernel config file for ${TYPE}" \
	E "Edit crunch.conf file for ${TYPE}" \
	S "MFS Size: ${SIZE}kB" \
	L "Language: ${LANGUAGE}" \
	I "Init type: ${INIT}" \
	F "Floppy size: ${FLOPPY_SIZE}kB" \
	M "MFS bytes per inode: ${MFS_INODES}" \
	U "UFS bytes per inode: ${FLOPPY_INODES}" \
	$ "Site-info: ${SITE}" \
	Q "Quit" \
	2> ${RISU}
    ans=`cat ${RISU}`
    rm ${RISU}
    case ${ans} in
    T)
	dialog --menu "Setup the type of configuration" 12 70 5 \
	    dial "dialin (ppp etc.)" \
	    router "router/bridge/mrouter" \
	    net "net admin tool" \
	    isp "isp (ppp and terminal server)" \
	    custom "custom config (need pathname)" 2> ${RISU} || rm ${RISU}
	if [ -f ${RISU} ] ; then
	    TYPE=`cat ${RISU}`
	    set_type
	fi
	;;
    I)
	dialog --menu "Choose your init(8) program" \
	10 70 2 init "Standard init (requires getty)" \
	oinit "small init from TinyWare" 2> ${RISU} || rm ${RISU}
	if [ -f ${RISU} ] ; then
	    INIT=`cat ${RISU}`
	fi
	;;

    K) ${EDITOR} ../${TYPE}/PICOBSD ;;

    E) ${EDITOR} ../${TYPE}/crunch1/crunch.conf ;;

    S)
	dialog --title "MFS Size setup" --inputbox \
"MFS size depends on what you need to put on the MFS image. Typically \
ranges between 820kB (for very small bridge/router images) to \
as much as 2500kB kB for a densely packed image. \
Keep in mind that this memory is \
totally lost to other programs. Usually you want to keep \
this as small as possible. " 10 70 2> ${RISU} || rm ${RISU}
	if [ -f ${RISU} ] ; then
	    SIZE=`cat ${RISU}`
	    if [ "${SIZE}" = "" ] ; then
		eval SIZE=$"${TYPE}_DFLT_SIZE"
	    fi
	fi
	;;

    L)
	dialog --menu "Please choose language" \
	10 40 2 en English pl Polish 2> ${RISU} || rm ${RISU}
	if [ -f ${RISU} ] ; then
	    LANGUAGE=`cat ${RISU}`
	fi
	;;

    \$)
	dialog --title "Site info setup" --inputbox \
	"Please enter the full path to the directory \
	containing site-specific setup. \
	This directory tree must contain files that replace \
	standard ones in floppy.tree/ and mfs.tree/ . " \
	10 70 2> ${RISU} || rm ${RISU}
	if [ -f ${RISU} ] ; then
	    SITE=`cat ${RISU}`
	fi
	;;

    F)
	dialog --menu "Set floppy size" 15 70 4 \
		1440 "1.44MB" 1720 "1.72MB" \
		2880 "2.88MB" 4096 "4MB" 2> ${RISU} || rm ${RISU}
	if [ -f ${RISU} ] ; then
	    FLOPPY_SIZE=`cat ${RISU}`
	fi
	;;

    M)
	dialog --title "MFS bytes per inode:" --inputbox \
	"Enter MFS bytes per inode (typically 4096..65536). \
	A larger value means fewer inodes but more space on MFS" \
	10 70 2> ${RISU} || rm ${RISU}
	if [ -f ${RISU} ] ; then
	    MFS_INODES=`cat ${RISU}`
	    if [ "${MFS_INODES}" = "" ] ; then
		eval MFS_INODES=$"${TYPE}_DFLT_MFS_INODES"
	    fi
	fi
	;;

    U)
	dialog --title "Floppy bytes per inode:" --inputbox \
	"Enter floppy bytes per inode (typically 3072..65536). \
	A larger value means fewer inodes but more space on the floppy." \
	10 70 2> ${RISU} || rm ${RISU}
	if [ -f ${RISU} ] ; then
	    FLOPPY_INODES=`cat ${RISU}`
	    if [ "${FLOPPY_INODES}" = "" ] ; then
		eval FLOPPY_INODES=$"${TYPE}_DFLT_FLOPPY_INODES"
	    fi
	fi
	;;

    N) break 2
	;;

    Q) exit 0 ;;

    *) echo "Unknown option \"${ans}\". Try again."
	sleep 2
	clear
	;;
    esac
  done
}

# Call the build procedure
# Install image
do_install() {
    dialog --title "Build ${TYPE} completed" --inputbox \
"\nThe build process was completed successfuly.\n\
Now we are going to install the image on the floppy.\n\
Please insert a blank floppy in /dev/fd0.\\n
WARNING: the contents of the floppy will be permanently erased!\n\
\n\
Your options:\n\
	* ^C or [Cancel] to abort,\n\
	* Enter to install \"picobsd.bin\",\n\
	* name of other file to install.\n\
" 20 70 2> ${RISU}
    if [ "$?" = "0" ]; then
	FILENAME=`cat ${RISU}`
	if [ "${FILENAME}" = "" ] ; then
	    FILENAME=picobsd.bin
	fi
	echo "Writing ${FILENAME}..."
	dd if=${FILENAME} of=/dev/rfd0
    else
	echo "Ok, the image is in picobsd.bin"
    fi
    echo "Done."
}

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

interactive="YES"
if [ "$1" = "-n" ] ; then
    interactive="NO"
    shift
fi
TYPE=$1
set_defaults
set_type

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

if [ "$1" = "package" ] ; then
    build_package
fi
if [ "$interactive" = "YES" ] ; then
    main_dialog
fi
main	# build ${TYPE}
do_install
exit 0
