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

# install [OPTIONS] DEST

# install:  A script to create the working version of the Matrix
#           customization system.
# DEST:     The destination directory of the install
# OPTIONS:
#      -r: install remotely on homer.u.washington.edu
#    -lkb: prepare the LKB files for TbG (only needs to be done once;
#          if you want to refresh the files, delete the delphin directory)
#    -iso: get the iso table file from iso that allows iso validation
#
# Examples:
#      To install to a local directory:
#           ./install path/to/dest/
#      To install to a remote directory:
#           ./install user@host:/path/to/dest/
#      To install remotely on homer:
#           ./install -r my_branch
#       * Note that this is merely short for:
#           ./install uwcl@homer.u.washington.edu:~/public_html/my_branch
#      To install to www.delph-in.net/matrix:
#           ./install -r matrix/customize
#      To prepare the LKB files in an install to delph-in.net/matrix
#           ./install -r -lkb matrix/customize

lkb_url="http://lingo.ling.washington.edu/latest/lkb_linux.x86." #32|64.tgz
delphin_dir="${CUSTOMIZATIONROOT}/../delphin"

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

# parse command line

echo "Please enter your patas username:"
read username

while [ "$1" = -r -o "$1" = -lkb -o "$1" = -iso ]
do
    if [ "$1" = -r ]
    then
        dest_prefix="$username@patas.ling.washington.edu:/home/www-matrix/html/"
	enable_captcha=1
        #dest_prefix="uwcl@homer.u.washington.edu:~/public_html/"
    elif [ "$1" = -iso ]
    then
        get_iso=1
    elif [ "$1" = -lkb ]
    then
        install_lkb=1
        if [ ! -d "$delphin_dir" ]
        then
            if [ -z $LOGONROOT ]
            then
                echo "Error: LOGONROOT not set. Exiting."
                exit 1
            fi
            mkdir -p $delphin_dir/bin
            mkdir -p $delphin_dir/lkb
            pushd $delphin_dir/lkb
            echo "\n\nDownloading LKB files. This could take a minute.\n\n"
            wget -O - "${lkb_url}32.tgz" | tar -xzf -
            wget -O - "${lkb_url}64.tgz" | tar -xzf -
            popd
            cp ${LOGONROOT}/lingo/lkb/etc/lkb $delphin_dir/bin/lkb
        fi
    fi
    shift
done

if [ $# != 1 ]
then
    echo "Usage: $0 [-r] <directory name>"
    exit 1
fi

dest="${dest_prefix}$1"

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

# the following will create the tempdir directory and fill it with
# with everything in the directories besides what's excluded.
rsync -a -o --exclude={CVS/,.svn/,delphin/,regression_tests/,tests/,sql_profiles/,gmmt/,lisp/,*.pyc,.*,*.tmp~,local_testing,Version.lsp,install,matrix.py,def_check.py,profiles.py,randgram.py,tdltest.py} ${CUSTOMIZATIONROOT}/../ $tempdir

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

# Redirect directory index to cgi script (rather than index.html)
echo "DirectoryIndex 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

if [ $install_lkb ]
then
    rsync -a --exclude=.svn/ $delphin_dir $tempdir --omit-dir-times
fi

if [ $get_iso ]
then
    echo "Downloading iso code table file from sil.org."
    echo ""
    wget http://www.sil.org/iso639-3/iso-639-3_20120206.tab -O "$tempdir/iso.tab"
fi

if [ $enable_captcha ]
then
    sed -i 's/^disable_captcha = True/disable_captcha = False/' $tempdir/matrix.cgi
fi

#Finally rsync the tempdir to the destination
#rsync -K maintain destination symlinks
#      -v verbose
#      -z compress before sending
#      -r recursive
#      -l copy symlinks as symlinks
#      -t timestamps (may cause error messages during copy, but they
#                     are apparently necessary for web-tests)
rsync -Kvzrlt $tempdir/ $dest --omit-dir-times
# The following command will also delete files at the destination that
# do not exist in tempdir. Not using for now because an incorrect
# destination could delete more than expected. This means that files
# such as sample-choices deleted locally won't be deleted remotely unless
# you do it manually.
#rsync -az --delete $tempdir/ $dest

# fix permissions:
echo "Fixing permissions (you will be prompted for your password once more)."

ssh $username@patas.ling.washington.edu 'sudo /home/www-matrix/fixperm.sh'

# and clean up
rm -rf $tempdir
