#!/bin/sh

CURDIR=`/bin/pwd`

INPUTBOXLIVEFS() {
    VALUE=`if [ $FREESBIEBASEDIR ]; then echo $FREESBIEBASEDIR; else echo "/usr/local/livefs"; fi`    
    TEMPFILE=`/usr/bin/mktemp -t startup`
    /usr/bin/dialog --title "FreeSBIE LiveCD startup configuration" \
	--inputbox "LiveFS path you are going to create" 19 70 \
	"$VALUE" 2> $TEMPFILE
	
    TEMPVAR1=`/bin/cat ${TEMPFILE}`
    if [ -d $TEMPVAR1 ]; then
   	FREESBIEBASEDIR=$TEMPVAR1 
	export FREESBIEBASEDIR
    else
	/usr/bin/dialog --title "FreeSBIE LiveCD startup configuration" --yesno "ATTENTION PLEASE!\n The path you have entered does not seem to be valid,\n or the directory does not exist.\n Do you want to create it?" 10 60
	
	if [ $? = 0 ]; then
	    FREESBIEBASEDIR=$TEMPVAR1
	    export FREESBIEBASEDIR
	    /bin/mkdir -p $FREESBIEBASEDIR
	fi
     fi
}

INPUTBOXLOCALDIR() {
    VALUE=`if [ $LOCALDIR ]; then echo $LOCALDIR; else echo "${CURDIR}"; fi`
    TEMPFILE=`/usr/bin/mktemp -t startup`
    /usr/bin/dialog --title "FreeSBIE LiveCD startup configuration" \
	--inputbox "FreeSBIE HOME (current directory)" 19 70 \
	"$VALUE" 2> $TEMPFILE
    
    TEMPVAR2=`/bin/cat ${TEMPFILE}` 
    if [ -d $TEMPVAR2/files ]; then
        LOCALDIR=`/bin/cat ${TEMPFILE}`
        export LOCALDIR
    else
        /usr/bin/dialog --title "FreeSBIE LiveCD startup configurations" --msgbox "ATTENTION PLEASE!\n The path you have entered does not seem to be valid,\n or the directory does not exist.\n It _MUST_ be the FreeSBIE scripts base directory!" 10 60
     fi

}

INPUTBOXPATHISO() {
    VALUE=`if [ $PATHISO ]; then echo $PATHISO; else echo "$CURDIR/FreeSBIE.iso"; fi`
    TEMPFILE=`/usr/bin/mktemp -t startup`
    /usr/bin/dialog --title "FreeSBIE LiveCD startup configuration" \
	--inputbox "ISO path you are going to create" 19 70 \
	"$VALUE" 2> $TEMPFILE

    TEMPVAR3=`/bin/cat ${TEMPFILE}`
    if [ ! -z $TEMPVAR3 ]; then
        PATHISO=$TEMPVAR3 
        export PATHISO 
    fi
}

OUTPUT() {
    if [ -f $LOCALDIR/config.sh ]; then
	rm $LOCALDIR/config.sh
    fi

    echo "FREESBIEBASEDIR=${FREESBIEBASEDIR}" >> $LOCALDIR/config.sh
    echo "LOCALDIR=${LOCALDIR}" >> $LOCALDIR/config.sh
    echo "PATHISO=${PATHISO}" >> $LOCALDIR/config.sh 

    #Buildkernel and Buildworld tweaks
    echo "MAKE=\"make\"" >> $LOCALDIR/config.sh
    echo "MAKEOPT=\"\"" >> $LOCALDIR/config.sh
    echo "RELEASE="`awk '/^#define __FreeBSD_version/ { print substr(\$3,1,1); }' /usr/src/sys/sys/param.h` >> $LOCALDIR/config.sh

    RELEASE=`awk '/^#define __FreeBSD_version/ { print substr(\$3,1,1); }' /usr/src/sys/sys/param.h`

    if [ "$RELEASE" -ge "5" ]; then
	/usr/bin/dialog --title "MiniBSD?" \
	    --yesno "Do you want do build\na minimal version of FreeSBIE\nsuitable for embedded use?" 8 35
	
	if [ "$?" = 0 ]; then
	    echo "MINIMAL=YES" >> $LOCALDIR/config.sh
	else
	    echo "MINIMAL=NO" >> $LOCALDIR/config.sh
	fi

	/usr/bin/dialog --title "BSD Installer?" \
	    --yesno "Would you like to include the BSD Installer?" 8 35
	if [ "$?" = 0 ]; then
	    echo "BSDINSTALLER=YES" >> $LOCALDIR/config.sh
	else
	    echo "BSDINSTALLER=NO"  >> $LOCALDIR/config.sh
	fi
    fi
}

CONFIGMENU() {
    if [ -f ./config.sh ]; then . ./config.sh; fi 
    TEMPFILE=`/usr/bin/mktemp -t config`
    /usr/bin/dialog --menu "FreeSBIE LiveCD startup configurations" 19 70 11 \
	LiveFS "LiveFS path you are going to create" \
 	Home "FreeSBIE HOME (current directory)" \
	Iso "ISO path you are going to create" \
	Exit "Go to FreeSBIE, FreeBSD Next Generation" 2> $TEMPFILE
    
    ret=`cat $TEMPFILE`

    case ${ret} in
	LiveFS)
	   INPUTBOXLIVEFS
	;;
	Home)
	   INPUTBOXLOCALDIR
	;;  
	Iso)
	   INPUTBOXPATHISO
	;;
 	Exit)
       if [ ! -z $FREESBIEBASEDIR ] && [ ! -z $LOCALDIR ] && [ ! -z $PATHISO ]; then
		# OK
	   	OUTPUT
	   	exit 0 
           else
		/usr/bin/dialog --title "FreeSBIE LiveCD startup configurations" --msgbox "ATTENTION PLEASE!\n You have not set all the paths needed by FreeSBIE." 10 60
	   fi	
	;;
	*)
	   exit 1 
	;;
    esac
    CONFIGMENU
}

CONFIGMENU
