#! /bin/sh
#
#   Generate a frame source list for the nds2 server and for channel searches
#   A source list is built containing all frame types listed in the 
#   channel_history directory. There are two sources of frame locations used:
#
#     - data in the frame_cache_dump file
#
#     - source list entries in "<frame-type>-local-source_list.txt" files 
#       in the execution directory.
#
#     - remove entries that match the nds2-exclude-source_list.txt
#
#   the resulting list is stored in the channel tracker directory as: 
#   nds2-source-list.txt, or as specified by the --outfile command argument.
#
# --- functions
syntax() {
    echo ""
    echo "nds_sourcery generates a list of frame locations for all frame types"
    echo "currently defined in the channel_history directory. The output file"
    echo "is written to a temporary file and renamed to the path specified in"
    echo "in the --outfile argument or by default to: "
    echo ""
    echo "    \$NDS2_CHANNEL_TRACKER/nds2-source-list.txt"
    echo ""
    echo "The command line syntax is: "
    echo ""
    echo "    nds_sourcery [--help] [--outfile <path>]"
}
#
# --- Set up the environment
prefix=/usr
exec_prefix=/usr
export PATH=".:/usr/bin:/usr/bin:/bin"
export LD_LIBRARY_PATH=/usr/lib
#
# --> Set default environment names
myhome=$HOME
nds2pwd="$myhome/channel-tracker"
cache_root="channel_history"
url=http://ldas-cit.ligo.caltech.edu/ldas_outgoing/diskcacheAPI
exclude_list="nds2-exclude-source_list.txt"

if [ -f $HOME/.nds2rc ]
then
    . $HOME/.nds2rc
    [ -z $NDS2_CHANNEL_TRACKER ] || nds2pwd="$NDS2_CHANNEL_TRACKER"
    [ -z $NDS2_FRAME_CACHE_URL ] || url="$NDS2_FRAME_CACHE_URL"
    [ -z $NDS2_CACHE_ROOT_DIR ]  || cache_root="$NDS2_CACHE_ROOT_DIR"
fi
#
# --- To the tracker directory.
cd $nds2pwd
cache_pwd=`cd $cache_root; pwd`
#
#  Fix up any command line arguments
ofile=nds2-source-list.txt
while [ $# != 0 ]; do
    case $1 in 
    "--outfile") 
	ofile="$2"
	shift
	;;
    "--help")
        syntax
	exit
	;;
    "--version")
	echo "nds_sourcery (nds-index-utilities) 0.0.0"
	exit
	;;
    *)
	echo "Unrecognized argument $1"
	exit
	;;
    esac
    shift
done
#
# --- Fetch the frame cache file
dump_file="frame_cache_dump"
if [ `echo $url | grep -c -e ^http: -e ^https: -e ^ftp:` != 0 ]; then
    rm -f $dump_file
    wget -q $url/$dump_file
elif [ `echo $url | grep -c -e ^file://` != 0 ]; then
    fdir=`echo $url | sed -e s,^file://,,g`
    cp $fdir/$dump_file .
else
    echo "unknown frame cache file location specifier: $url"
    exit 1
fi
#
# --- Make a temporary source list file.
tmpfile="nds_sourcery_`date +%s`"
rm -f $tmpfile
touch $tmpfile
#
#  Loop over all the directories I like. Build the list.
for frtype in `ls -1 $cache_pwd`; do
    if [ ! -d $cache_pwd/$frtype ]; then
 	continue
    fi
    local_list="${frtype}-local-source_list.txt"
    [ -f $local_list ] && cat $local_list >> $tmpfile
    [ -f $dump_file ]  && nds_build_source_list $frtype 815000000 0 >> $tmpfile
done
#
# --- Clean up the frame cache file
rm -f $dump_file
#
# --- Remove excluded paths.
if [ -f $exclude_list ]; then
    grep -v -f $exclude_list ${tmpfile} > ${tmpfile}_x
    rm   -f ${tmpfile}
    mv ${tmpfile}_x ${ofile}
else
    mv ${tmpfile} ${ofile}
fi
