#!/bin/bash
### $Id: install,v 1.1 2008-09-09 08:37:52 sfd Exp $

# install:  A script to create the working version of the Matrix
#           customization system.  It takes several flags and a
#           single target directory as arguments:
#      -l:  install locally (on this machine)
#      -r:  install remotely (on homer.u.washington.edu)
#      -m:  (with -l or -r) also install matrix-core
#      -c:  don't install, but create a copy of matrix-core locally
#    -lkb:  also copy the necessary parts of the lkb from given directory
#
# Examples:
#      To install to www.delph-in.net/matrix:
#           ./install -r -m matrix/customize
#      To install to a local directory:
#           ./install -l -m [root directory of installation]
#      To install to a local directory without copying matrix-core:
#           ./install -l [root directory of installation]
#      To create a copy of matrix-core:
#           ./install -c [parent directory of matrix-core]

# parse command line
while [ "$1" = -l -o "$1" = -r -o "$1" = -c -o "$1" = -m -o "$1" = -lkb ]
do
    if [ "$1" = -l ]
    then
        installlocal=true
    elif [ "$1" = -r ]
    then
        installremote=true
    elif [ "$1" = -c ]
    then
        copymatrix=true
    elif [ "$1" = -m ]
    then
        installmatrix=true
    elif [ "$1" = -lkb ]
    then
        shift
        lkbdir="$1"
    fi
    shift
done

if [ $# != 1 -o ! \( "$installlocal" -o "$installremote" -o "$copymatrix" \) ]
then
    echo "Usage: $0 [-l|-r|-c] [-m] <directory name>"
    exit 1
fi

# check that Customization Root is defined
if [ -z $CUSTOMIZATIONROOT ]
then
    echo "Please: export CUSTOMIZATIONROOT=<your gmcs file>"
    exit 1
fi


# create temporary staging directory
tempdir=/tmp/matrix.$$
mkdir $tempdir

# create datestamp, matrix-types, and head-types files
date -u > $tempdir/datestamp
cat ${CUSTOMIZATIONROOT}/../matrix-core/matrix.tdl | grep '^[^;].*:=' | sed 's/ *:=.*//g' | sort | uniq > $tempdir/matrix-types
cat ${CUSTOMIZATIONROOT}/../matrix-core/head-types.tdl | grep '^[^;].*:=' | sed 's/ *:=.*//g' | sort | uniq > $tempdir/head-types

# copy files to the temp directory
if [ "$installremote" ]
then
    # Redirect the root directory to matrix.cgi
    echo "Redirect /uwcl/$1/index.html /uwcl/$1/matrix.cgi" >> \
        $tempdir/.htaccess

    # Don't allow directory browsing
    echo "IndexIgnore */*" >> \
        $tempdir/.htaccess

    # Don't allow access to the saved choices.  (Even with directory
    # browsing turned off, somebody could guess the filename.)
    mkdir $tempdir/saved-choices
    echo "Option -Indexes" >> \
        $tempdir/saved-choices/.htaccess
fi

# main files
cp ${CUSTOMIZATIONROOT}/{matrix.cgi,matrix.css,matrix.js,matrixdef} $tempdir

# gmcs files
mkdir $tempdir/gmcs
cp ${CUSTOMIZATIONROOT}/{__init__.py,customize.py,deffile.py,choices.py,tdl.py,utils.py,validate.py,generate.py} $tempdir/gmcs/

# TbG template files
mkdir $tempdir/gmcs/templates
#mkdir $tempdir/templates
cp ${CUSTOMIZATIONROOT}/templates/* $tempdir/gmcs/templates
#cp ${CUSTOMIZATIONROOT}/templates/* $tempdir/templates

# lib files
mkdir $tempdir/gmcs/lib
cp ${CUSTOMIZATIONROOT}/lib/*.py $tempdir/gmcs/lib/

# linglib files
mkdir $tempdir/gmcs/linglib
cp ${CUSTOMIZATIONROOT}/linglib/*.py $tempdir/gmcs/linglib/

# util files
mkdir $tempdir/gmcs/util
cp ${CUSTOMIZATIONROOT}/util/*.py $tempdir/gmcs/util/

cp -R ${CUSTOMIZATIONROOT}/sample-choices $tempdir/
rm -rf $tempdir/sample-choices/CVS $tempdir/sample-choices/.svn
mkdir $tempdir/tmp

# create matrix-core if necessary
if [ "$installmatrix" -o "$copymatrix" ]
then
    mkdir $tempdir/matrix-core
    mkdir $tempdir/matrix-core/lkb
    mkdir $tempdir/matrix-core/pet

    matrixfiles=`ls -d ${CUSTOMIZATIONROOT}/../matrix-core/*.* ${CUSTOMIZATIONROOT}/../matrix-core/README | sed 's/[^ ]*my_language.tdl//g' | sed 's/[^ ]*Version.lsp//g' | sed 's/[^ ]*CVS//g' | sed 's/[^ ]*~//g'`

    cp $matrixfiles $tempdir/matrix-core

    lkbfiles=`echo ${CUSTOMIZATIONROOT}/../matrix-core/lkb/* | sed 's/..\/lkb\/CVS//g'`
    cp $lkbfiles $tempdir/matrix-core/lkb

    petfiles=`echo ${CUSTOMIZATIONROOT}/../matrix-core/pet/* | sed 's/..\/pet\/CVS//g'`
    cp $petfiles $tempdir/matrix-core/pet
fi


#If necessary, copy the lkb from the source directory, taking only what is needed
if [ "$lkbdir" ]
then
  mkdir $tempdir/delphin
  cp -rL $lkbdir/bin $tempdir/delphin
  lkbfiles=`ls -1 $lkbdir/lkb | sed '/^src$/ d'`
  echo $lkbfiles
  pushd $lkbdir/lkb
  cp -r $lkbfiles $tempdir/delphin
  popd
fi


# copy the files to the appropriate target
if [ "$installlocal" ]
then
    cp -p -v -r $tempdir/* $1
elif [ "$copymatrix" ]
then
    cp -p -v -r $tempdir/matrix-core $1
elif [ "$installremote" ]
then
    #shopt -s dotglob
    pushd $tempdir
    tar cj * | ssh uwcl@homer.u.washington.edu "tar xjv -C public_html/$1"
    popd
    #shopt -u dotglob
fi

# clean up
rm -rf $tempdir
