#! /bin/bash

set -x

echo "creating package"
rm -f revproxy.tgz
#tar cfvz revproxy.tgz revproxy/scripts revproxy/bin revproxy/data revproxy/etc revproxy/css revproxy/templates
(cd ..; tar cfvz revproxy/revproxy.tgz revproxy/bin revproxy/data revproxy/css revproxy/templates)
if [ "$?" != "0" ] ; then
    echo "unable to create package" 1>&2
   exit 1
fi

echo "cleaning up old package on remote"
ssh morrisb9@uw1-320-lab.uwb.edu <<EOF
    set -x
    if cd css490 ; then
        # If a package already-exists, save it as a backup adding a -
        # to the name, deleting the old backup (if there is one).
        # If there is no existing package, the command will fail, but
        # we don't need to care.
        rm -f revproxy.tgz-
        mv revproxy.tgz revproxy.tgz-
        # Delete the install directory (if there is one).
        # To make this robust, we'd want to make sure the directory
        # really gets deleted (or wasn't there in the first place).
        rm -rf revproxy
        sleep 4
        exit 0
    else
        echo "target directory missing: manual intervention required" 1>&2
        exit 1
    fi
EOF
if [ "$?" != "0" ] ; then
   exit 1
fi

echo "transfering package to remote machines & starting up authserver"
# Step 1: file transfer
scp revproxy.tgz  morrisb9@uw1-320-lab.uwb.edu:css490/
if [ "$?" != "0" ] ; then
    echo "unable to transfer package" 1>&2
   exit 1
fi
# Step 2: Unpack & run.
ssh morrisb9@uw1-320-lab.uwb.edu  <<EOF
    set -x
    cd css490 || exit 1
    tar xfvz revproxy.tgz || exit 1
    cd revproxy || exit 1

    mkdir -p out etc || exit 1

    host=\`hostname\`
    ./bin/mklogconfig "auth-\${host}"
    [ -f "etc/auth-\${host}.xml" ] || exit 1

    # save "host:port" into a "known" file so the timeservers will
    # know where to find the authserver
    echo "\${host}.uwb.edu:9000" > etc/auth.info

    # todo: daemonize authserver
    ./bin/authserver \\
        --authport=9000 \\
        --log=etc/auth-\${host}.xml \\
        > out/auth-\${host}.out \\
        2> out/auth-\${host}.err &

    authpid=\$!
    sleep 2
    # check if its running:
    # kill -0 \$authpid
    exit \$?
EOF
if [ "$?" != "0" ] ; then
    echo "unable to unpack package & run authserver" 1>&2
   exit 1
fi

echo "firing off some timeservers"
for port in 9030 9031 9032 9033 9034 9035; do
    ssh morrisb9@uw1-320-lab.uwb.edu <<EOF
        cd css490/revproxy || exit 1

        # Look in the auth.info file to find the host: port of the
        # authserver.  Use backquotes to set the value of the shell
        # variables from output of the command pipeline.  The sed
        # command (stream editor) does the string processing.
        # authhost is everything _before_ the colon and
        # authport is everything _after_ the colon.
        authhost=\`cat etc/auth.info | sed -e s/:.*//\`
        authport=\`cat etc/auth.info | sed -e s/.*://\`

        host=\`hostname\`
        ./bin/mklogconfig "ts-\${host}-${port}"

        ./bin/timeserver \\
            --port=$port \\
            --authhost=\$authhost \\
            --authport=\$authport \\
            --log="etc/ts-\${host}-${port}.xml" \\
            > out/\${host}-${port}.out \\
            2> out/\${host}-${port}.err &

        exit 0
EOF
done

echo "firing off the proxyserver"
ssh morrisb9@uw1-320-lab.uwb.edu <<EOF
    set -x
    cd css490/revproxy || exit 1

    host=\`hostname\`
    ./bin/mklogconfig "revproxy-\${host}"

    ./bin/revproxy \\
        --log="etc/revproxy-\${host}.xml" \\
        > out/revproxy-\${host}.out \\
        2> out/revproxy-\${host}.err &

    # Register the timeservers: figure out where the timeservers are
    # running by looking at the names of the log configuration files.
    #
    # The perl command is an alternative to sed command.  here we use
    # it to strip off everything except for host-port and replace it
    # with "host.uwb.edu/port".
    for i in etc/ts-*; do
        path=\`echo \$i | perl -ple 's/.*(uw1-320-[0-9]+)-([0-9]+).*/\$1.uwb.edu\\/\$2/'\`
        curl "http://localhost:9090/register/\$path"
    done

    # Finally, tell us where the proxyserver is running:
    echo "proxyserver: http:\${host}.uwb.edu:9090/"
EOF