#!/bin/sh
# This script builds a bunch of files needed for techrep
# 

# Don't change this!!
HELP_URL=http://www.ncstrl.org/Dienst/htdocs/Lite/Techrep/doc/web
ERROR_LOG=lib/error-log

TR_REPORT_DIR=`grep TrReportDir techrep.conf | awk '{print $2}'`
if [ ! "$TR_REPORT_DIR" ]; then
   echo "TrReportDir not set."
   exit 1
elif [ ! -d $TR_REPORT_DIR ]; then
   echo "$TR_REPORT_DIR does not exist or is inaccessible from this machine."
   exit 1
fi
echo TrReportDir is $TR_REPORT_DIR
TR_CITATION_FILE=$TR_REPORT_DIR/bibs.refer

TR_HOME_DIR=`grep TrHomeDir techrep.conf | awk '{print $2}'`
if [ ! "$TR_HOME_DIR" ]; then
   echo "TrHomeDir not set."
   exit 1
elif [ ! -d $TR_HOME_DIR ]; then
   echo "$TR_HOME_DIR does not exist or is inaccessible from this machine."
   exit 1
fi
echo TrHomeDir is $TR_HOME_DIR

#TrSeriesName
ORG_FILE=$TR_HOME_DIR/lib/TrSeriesNames
grep TrSeriesName techrep.conf | sed -e /TrSeriesName/s/TrSeriesName[\ ]*// >$ORG_FILE
if [ ! -s $ORG_FILE ]; then
   echo "TrSeriesName not set."
   exit 1
fi
echo This is the file of TR Series
cat $ORG_FILE

#OrgName
ORG=`grep OrgName techrep.conf | sed -e /OrgName/s/OrgName[\ ]*//`
if [ ! "$ORG" ]; then
   echo "OrgName not set."
   exit 1
fi
echo OrgName is $ORG

#ReportUrlPrefix
REPORT_URL_PREFIX=`grep ReportUrlPrefix techrep.conf | awk '{print $2}'`
if [ ! "$REPORT_URL_PREFIX" ]; then
   echo "ReportUrlPrefix not set."
   exit 1
fi
echo "ReportUrlPrefix is $REPORT_URL_PREFIX"

#InterfaceUrl
INTERFACE_URL=`grep InterfaceUrl techrep.conf | awk '{print $2}'`
if [ ! "$INTERFACE_URL" ]; then
   echo "InterfaceUrl not set."
   exit 1
fi
echo "InterfaceUrl is $INTERFACE_URL"
MASTERFORM=`basename $INTERFACE_URL`

#ReportAdmin
TR_ADMIN=`grep ReportAdmin techrep.conf | awk '{print $2}'`
if [ ! "TR_ADMIN" ]; then
   echo "ReportAdmin not set."
   exit 1
fi
echo "ReportAdmin is $TR_ADMIN"

#PerlPath
PERL_PATH=`grep PerlPath techrep.conf | awk '{print $2}'`
if [ ! "$PERL_PATH" ]; then
   echo "PerlPath not set."
   exit 1
fi
echo "PerlPath is $PERL_PATH"

#MailPath
MAIL_PATH=`grep MailPath techrep.conf | awk '{print $2}'`
if [ ! "$MAIL_PATH" ]; then
   echo "MailPath not set."
   exit 1
fi
echo "MailPath is $MAIL_PATH"

# command line techrep. 
COM_TARGET=$TR_HOME_DIR/src/com-conf.h
echo "#define TR_REPORT_DIR		\"$TR_REPORT_DIR\"" > $COM_TARGET
echo "#define TR_ERROR_LOG_FILE		\"$TR_HOME_DIR/$ERROR_LOG\"" >> $COM_TARGET
echo "#define TR_URL_PREFIX		\"$REPORT_URL_PREFIX\"" >> $COM_TARGET
echo "#define TR_CITATION_DATA_FILE	\"$TR_CITATION_FILE\"" >> $COM_TARGET
echo "#define GENHTML_PATH		\"$TR_HOME_DIR/bin/genHTML\"" >> $COM_TARGET
echo "#define TR_INDEX_HEADER_FILE	\"$TR_HOME_DIR/lib/README.header\"" >> $COM_TARGET
echo "#define TR_SEMAPHORE_FILE		\"$TR_HOME_DIR/lib/semaphore\"" >> $COM_TARGET
COMPRESS_CMD=`which compress`
echo "#define TR_COMPRESS_CMD		\"$COMPRESS_CMD\"" >> $COM_TARGET

# web based forms interface to command line techrep
WWW_TARGET=$TR_HOME_DIR/src/web-conf.h
echo "#define TR_HOME \"$TR_HOME_DIR\"" > $WWW_TARGET
echo "#define HELP_URL \"$HELP_URL\"" >> $WWW_TARGET
echo "#define TR_URL_PREFIX \"$REPORT_URL_PREFIX\"" >> $WWW_TARGET
echo "#define TR_REPORT_DIR \"$TR_REPORT_DIR\"" >> $WWW_TARGET
echo "#define MASTERFORM		\"$MASTERFORM\"" >> $WWW_TARGET
echo "#define TR_ADMIN \"$TR_ADMIN\"" >> $WWW_TARGET
echo "#define MAIL_PATH \"$MAIL_PATH\"" >> $WWW_TARGET

# html generation script
HTML_TARGET=$TR_HOME_DIR/bin/genHTML
echo "#!$PERL_PATH" >$HTML_TARGET
echo "# Convert the techrep bib file to a README html file.">> $HTML_TARGET
echo >> $HTML_TARGET
echo "\$outFilename = \"$TR_REPORT_DIR/README.html\";" >> $HTML_TARGET
echo "\$bibFilename = \"$TR_CITATION_FILE\";" >> $HTML_TARGET
echo "\$siteTitle = \"$ORG\";" >> $HTML_TARGET
echo >> $HTML_TARGET
cat lib/genHTML.body >> $HTML_TARGET
chmod 755 $HTML_TARGET

## scripts for posting, deleting, updating
## We're just adding a path to these scripts
TR_BIN=$TR_HOME_DIR/bin
awk '{if ($1 == "#SETPATH") \
        { printf "PATH='$TR_BIN':$PATH\n"} \
      else \
        { print }\
     }' bin/deleteTR.distrib > bin/deleteTR

awk '{if ($1 == "#SETPATH") \
        { printf "PATH='$TR_BIN':$PATH\n"} \
      else \
        { print }\
     }' bin/postTR.distrib > bin/postTR

awk '{if ($1 == "#SETPATH") \
        { printf "PATH='$TR_BIN':$PATH\n"} \
      else \
        { print }\
     }' bin/updateTR.distrib > bin/updateTR
chmod 700 bin/updateTR bin/deleteTR bin/postTR

## create the error log file
touch $ERROR_LOG
chmod 666 $ERROR_LOG

## create an empty citation database file if it doesn't exist
touch $TR_CITATION_FILE
chmod 644 $TR_CITATION_FILE




