#! /bin/bash
#
#  Print out the blather
#
blab_out() {
  if [ $noblahblah = 0 ]; then
    $formatter $formatpar 
  fi
}
#
#  Get default configuration from install symbols
prefix=/usr
exec_prefix=/usr
bindir=/usr/bin
sysconfdir=//etc
datarootdir=/usr/share
datadir=/usr/share/nds-index-utilities
#
noblahblah=0
formatter=cat
formatpar=
#
# Check that the install directory is correct.
nds2_prefix=$prefix
if [ ! -e $bindir/nds_metadata_server ]; then
   nds2_prefix=`which nds_metadata_server 2>/dev/null | sed -e 's,/bin/nds_metadata_server$,,g'`
   if [ -z $nds2_prefix ]; then
      echo "Can not find nds_metadata_server installed on this host"
      exit 1
   fi
fi
#
#  Set default configuration directories
nds2_config=$HOME/nds2-`uname -n`
nds2_tracker=$HOME/channel-tracker
nds2_cache_url=
#
#  -- get config and channel tracker directories
blab_out <<EOF

The nds2 config directory contains all the configuration file for the nds2 
server.
EOF
read -p "Enter config directory [$nds2_config]: " cdir
[ -n "$cdir" ] && nds2_config="$cdir"
#
blab_out <<EOF

The channel tracker directory contains a history of channel lists for each 
frame type to be served. These are combined to produce the channel lists that
are read in by the server at run time.
EOF
read -p "Enter tracker directory [$nds2_tracker]: " cdir
[ -n "$cdir" ] && nds2_tracker="$cdir"
#
# -- create directories
echo
echo "Creating directories: "
echo "   $nds2_config"
mkdir -p "$nds2_config"
echo "   $nds2_tracker"
mkdir -p "$nds2_tracker"
echo "   $nds2_tracker/channel_history"
mkdir -p "$nds2_tracker/channel_history"
cd $nds2_tracker
#
# -- Get a frame_cache_file url
blab_out <<EOF

The channel tracker can get source frame lists from a local frame cache file
(generated by the diskCacheApi). If a frame cache file is available for your
server host, enter the url of the frame cache file. Note that to use this
url, the wget program must be installed.
EOF
unset cdir
read -p "Enter frame_cache_file URL [file:///var/lib/diskcache]: " cdir
if [ "q$cdir" != "q" ]; then
    if [ `echo $cdir | grep -c -e ^file://` != 0 ]; then
        nds2_cache_url=$cdir
        cache_file=frame_cache_file
    else
        WGET=`which wget 2>/dev/null`
        if [ -z $WGET ]; then
            echo "wget is not available, $cdir will not be used"
        else
        cache_file=frame_cache_file
        nds2_cache_url=`echo cdir | sed -e 's,/frame_cache_file$,,'`
        rm -f $cache_file
        $WGET $nds2_cache_url/$cache_file
        if [ -f $cache_file ]; then
            echo "There is no frame cache file at URL: $nds2_cache_url"
            nds2_cache_url=""
        else
            rm -f $cache_file
        fi
        fi
    fi
fi
#
#   Write the channel tracker resource file.
url=$nds2_cache_url
if [ "q$url" = "q" ]; then
    url=file:///var/lib/diskcache
    dsource=none
else
    dsource=frame_cache_file
fi

rcfile=$HOME/.nds2rc
echo
if [ -f $rcfile ]; then
    echo "Warning: file $rcfile exists, it will not be replaced"
else
    echo "Creating nds2 resource file ($rcfile)..."
    cat > $rcfile <<EOF
NDS2_SERVER_CONFDIR=$nds2_config
NDS2_CHANNEL_TRACKER=$nds2_tracker
NDS2_FRAME_CACHE_URL=$url
EOF
fi
#
#  Check for frame types already defined
if [ `ls -1d channel_history/* 2>/dev/null | wc -l` != 0 ]; then
    cat <<EOF

Warning: The following frame types have already been defined in the channel
history directory in $nds2_tracker. These frame types will 
be included in channel list generations and will be served by 
the nds2 server. 
EOF
    cd channel_history
    ls -1d *
    cd ..
fi
#
#  Build the list
echo
ftyp="feedme"
while [ "x$ftyp" != "x" ]; do
    read -p "Enter the frame type [exit] " ftyp
    if [ "q$ftyp" != "q" ]; then
	read -p "Enter $ftyp source directory [$dsource]: " sdir
	if [ "x$sdir" = "x" -a "$dsource" = "none" ]; then
	    echo "Frame type $ftyp must have a source directory"
	    continue
	elif [ "x$sdir" != "x" ]; then
	    cat >> ${ftyp}-local-source_list.txt <<EOF
$ftyp 0 1999999999 0 cache - $sdir
EOF
	fi
	mkdir -p channel_history/$ftyp
	touch channel_history/$ftyp/$ftyp-800000000-0.cls
	touch channel_history/$ftyp/.active-type
    fi
done
#
wizlog="$nds2_tracker/nds2-setup_wizard.log"
blab_out <<EOF
The nds2 setup wizard will now start building the channel lists for the 
configured frame types and available source data. The output of this process
can be found in:

   $wizlog,

the source list it generates can be found in 

   $nds2_tracker/nds2-source-list.txt

and the individual channel list build steps produce log files called:

   $nds2_tracker/build-<frame-type>-list.txt.

If you wish to add more frame types for this server, you can use

   nds_add_frame_type <frame-type>

EOF
"$nds2_prefix/bin/nds_nightly" >> "$wizlog" 2>&1 &
#
#  --- Build up the configuration file
#
cd "$nds2_config"
rundate=`date`
cfile=channel_lists.txt

#
#  Fill in the channel files.
for ftdir in $nds2_tracker/channel_history/*; do
    if [ -d $ftyp ]; then
        ftyp=`echo $ftdir | sed -e 's,.*/,,g'`
	listf="$nds2_config/${ftyp}-ChanList.txt"
        echo "channel.file  \"$listf\"" >> $cfile
    fi
done

#
#  Done
cat <<EOF

The nds2 server setup is complete. Check that the channel list build process
has completed after which you can start the server with:

    "$nds2_prefix/bin/nds2" -d "$nds2_config/nds2.conf"

EOF
