#!/bin/bash

installPerlCGI()
{

  if [ "x$FULL_PERL_EXE" != "x" ] ; then
    echo "  Using FULL_PERL_EXE = $FULL_PERL_EXE"
  elif [ "x$PERL_HOME" != "x" ] ; then
    echo "  Constructing FULL_PERL_EXE from PERL_HOME=$PERL_HOME"
    export FULL_PERL_EXE="$PERL_HOME/bin/perl"
  else
    echo "  Neither FULL_PERL_EXE or PERL_HOME set."
    echo "  Constructing FULL_PERL_EXE from 'which perl'"
    export FULL_PERL_EXE=`which perl`
  fi

  echo "  Substituting @FULL_PERL_EXE@ -> $FULL_PERL_EXE"

  cat src/cgi-bin/html-to-expeditee.pl.in \
   | sed "s%@FULL_PERL_EXE@%$FULL_PERL_EXE%g" \
   > ../../web/WEB-INF/cgi/html-to-expeditee.pl

   cat src/cgi-bin/export-individual-expeditee.pl.in \
    | sed "s%@FULL_PERL_EXE@%$FULL_PERL_EXE%g" \
	> ../../web/WEB-INF/cgi/export-individual-expeditee.pl

}

if [ ! -d ../../web/ext/html-to-expeditee ] ; then
  echo "Making supporting html-to-expeditee directory in web/ext folder"
  mkdir ../../web/ext/html-to-expeditee
fi

if [ ! -d ../../web/ext/html-to-expeditee/jquery ] ; then
  echo "Copying jquery into web/ext/html-to-expeditee folder"
  /bin/cp -r packages/jquery ../../web/ext/html-to-expeditee/.
fi

if [ ! -d ../../web/ext/html-to-expeditee/js ] ; then
  echo "Copying src/js into web/ext/html-to-expeditee/js"
  /bin/cp -r src/js ../../web/ext/html-to-expeditee/.
elif [ "src/js/html-to-expeditee.js" -nt "../../web/ext/html-to-expeditee/js/html-to-expeditee.js" ] ; then
  echo "Updating src/js/*.js to web/ext/html-to-expeditee/js/."
  /bin/cp -r src/js/*.js ../../web/ext/html-to-expeditee/js/.
fi


if [ ! -f ../../web/WEB-INF/cgi/html-to-expeditee.pl ] ; then
  echo "Installing html-to-expeditee.pl to cgi-bin directory"
  installPerlCGI
elif [ "src/cgi-bin/html-to-expeditee.pl.in" -nt "../../web/WEB-INF/cgi/html-to-expeditee.pl" ] ; then
  echo "Installing latest version of html-to-expeditee.pl to cgi-bin directory"
  installPerlCGI
fi

if [ ! -f ../../web/WEB-INF/cgi/export-individual-expeditee.pl ] ; then
	echo "Installing export-individual-expeditee.pl to cgi-bin directory"
	installPerlCGI
elif [ "src/cgi-bin/export-individual-expeditee.pl.in" -nt "../../web/WEB-INF/cgi/export-individual-expeditee.pl" ] ; then
	echo "Installing latest version of export-individual-expeditee.pl to cgi-bin directory"
	installPerlCGI
fi