#!/bin/sh

#
# Bootstrap configurator
#

ask() {
    # $1 is the variable to set
    # $2 is the question
    # $3 is the answer range
    # $4 is the default

    printf "%s[%s]? " "$2" "$3"
    eval read $1
    if eval [ x\$$1 = x ]
    then
	eval $1=\"$4\"
    fi
}

getnewdbpath() {
    ndbs=`expr $ndbs + 1`
    ask pathdb$ndbs \
	"What is the path to the database" /fnal/ups/db /fnal/ups/db

    if [ "x$dblist" = "x" ]
    then
	eval dblist="\"\$pathdb$ndbs\""
    else
	eval dblist="\"$dblist
\$pathdb$ndbs\""
    fi
}

#
# Initial conditions
#
ndbs=0			# no databases yet
installcoreFUE=y	# we should install coreFUE somewhere
installfullFUE=n	# we should not install fullFUE
writeul=y		# we should not write in /usr/local
dblist=""		# databases we know about
mkprdlist=""		# product areas we should create

#
# default to using AFS if we see it...
#

ask writeul "Should we put symlinks and login shells in /usr/local" Yn y

if [ -d /afs/fnal.gov/ups/db ] 
then
    ask useafs "Do you want to use the existing AFS products area" Yn y
else
    ask useafs "Do you want to use the existing AFS products area" yN n
fi


if [ $useafs = y ]
then
    ndbs=`expr $ndbs + 1`
    dblist="/afs/fnal.gov/ups/db"
    pathdb1=$dblist
fi


ask moredbs "Do you want to use any other existing products areas" yN n

while [ $moredbs = y ]
do
    getnewdbpath 
    ask moredbs "Do you want to use any other existing products areas" yN n
done

ask localdb "Do you want to create a database local to this system" Yn y

if [ $localdb = y ]
then
    getnewdbpath
    eval localpath="\$pathdb$ndbs"
fi

printf "Of the following databases:\n"
printf "%s\n" $dblist | pr -t -n
ask whichcoreFUE \
	"From which one do you want to get coreFUE (ups, upd, etc.)" 1-$ndbs 1

if [ $ndbs = $whichcoreFUE ]
then
    installcoreFUE=y
else
    installcoreFUE=n
fi


if [ $localdb = y ]
then
    ask installfullFUE "Should we install the full FUE environment in the local database" yN n
    if [ $installfullFUE = y -a $writeul = n ]
    then
	echo "Warning -- installing fullFUE without writing in /usr/local may not work"
	ask writeul "Can we write in /usr/local after all" Yn y
        if [ $writeul = n ]
        then
	    ask installfullFUE "Are you sure we should do fullFUE" yN n
        fi
    fi
fi

flatdblist=""
sep=""
for db in $dblist
do
   flatdblist="$db$sep$flatdblist"
   sep=" "
done

eval coreFUEpath="\$pathdb$whichcoreFUE"
if [ "$coreFUEpath" = /afs/fnal.gov/ups/db ]
then
    for db in $flatdblist
    do
	if [ 0 = `grep "^$db\$" /afs/fnal.gov/ups/etc/upsdb_list | wc -l` ]
        then
	    echo "Sorry, database $db is not in the /afs/fnal.gov/ups/etc/upsdb_list"
	    echo "This configuration is not feasable"
	    exit 1
        fi
    done
fi


# ---------------------------- Write config file --------------------------

printf "Writing config.custom\n"


cat > config.custom <<EOF
# bootstrap configuration file

#---------------------------------
# variables to be used later
#
set_variable bootbase="file://localhost/var/tmp/upsupdbootstrap/prd/bootstrap/.v2_2"

#---------------------------------
# variables required by bootstrap
#
set_variable upsdb_list="$flatdblist"

#---------------------------------
# Space checks
#
EOF

if [ $localdb = y ]
then
echo "check_space $localpath       70000" >> config.custom
fi

if [ $writeul = y ]
then
echo "check_space /usr/local/bin    2000" >> config.custom
fi

cat >> config.custom <<EOF
#---------------------------------
# files to download
#	      remote					local
#             -----					-----
download_file \$bootbase/downloads/updconfig.generic 	updconfig

#---------------------------------
# things to run before installs
#
#pre_install_command "sh localmount.sh"
EOF

cat >> config.custom <<EOF

#---------------------------------
# user-id's to create
#		login		uid	gid	home
#		-----		---	---	----
EOF

if [ $writeul = y -a $installfullFUE = y ]
then
    echo "create_user	products	1342	4525	$localpath" >> config.custom
fi

cat >> config.custom <<EOF

#---------------------------------
# ups databaes to create
#	  database		dbconfig	updconfig	owner
#         --------		--------	---------	-----
EOF

if [ $writeul = y -a $installfullFUE = y ]
then
    owner=products
else
    owner=-
fi

if [ $localdb = y ]
then
echo "create_db $localpath		-	-	$owner" >> config.custom
fi

cat >> config.custom <<EOF

#---------------------------------
# if installing coreFUE, where?
#
EOF
if [ $localdb = y ]
then
echo "install_coreFUE $coreFUEpath" >> config.custom
fi


cat >> config.custom <<EOF

#---------------------------------
# other product installs
#
EOF
if [ $installfullFUE = y ]
then
echo "install_as	$owner 	fullFUE" >> config.custom

fi


cat >> config.custom <<EOF

#---------------------------------
# ups current, configure, insall_as_root commands
#
EOF
if [ $writeul = y -a $installfullFUE = y ]
then
echo "do_ups installasroot systools" >> config.custom
fi


cat >> config.custom <<EOF

#---------------------------------
# courtesy links in /usr/local/etc?
#
EOF
if [ $writeul = y ]
then
echo "make_courtesy_links" >> config.custom
fi

ask extraproducts "Would you like any other product installs included" yN n
while [ $extraproducts = y ]
do
	ask prodname "Product name" 
        echo "install_as	$owner 	$prodname" >> config.custom
        ask extraproducts "Add another product" yN n
done
