#!/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 -j2\"" >> $LOCALDIR/config.sh
    echo "MAKEOPT=\"\"" >> $LOCALDIR/config.sh
    echo "if [ \"\`uname -r | cut -d '.' -f1\`\" = \"5\" ];" >> $LOCALDIR/config.sh
    echo "then RELEASE=5" >> $LOCALDIR/config.sh
    echo "fi" >> $LOCALDIR/config.sh
    echo "CLOOP=\"NO\""
    
}

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 configuration" --msgbox "ATTENTION PLEASE!\n You have not set all the paths needed by FreeSBIE." 10 60
	   fi	
	;;
	*)
	   exit 1 
	;;
    esac
    CONFIGMENU
}

CONFIGMENU
