#! /bin/bash
# $Id: ejudge-upgrade-web.in 6417 2011-09-07 05:21:04Z cher $
# Copyright (c) 2008-2011 Alexander Chernov <cher@ejudge.ru>

# This script upgrades the web installation of the ejudge system

prefix="/usr"
exec_prefix="/usr"
bindir="/usr/bin"
datarootdir="${prefix}/share"
datadir="/usr/share"
includedir="/usr/include"
libdir="/usr/lib64"
libexecdir="/usr/lib"

dflt_httpd_cgi_bin_dir="/var/www/cgi-bin"
dflt_httpd_htdocs_dir="/var/www/html"
style_prefix="/ejudge/"
ejudge_cgibindir="/usr/lib/ejudge/cgi-bin"
cgi_suffix=""

dojoarchive="dojo-release-1.2.2.tar.gz"
site="http://www.ejudge.ru/download"

write_help() {
    echo "ejudge-upgrade-web: ejudge web upgrade utility"
    echo "Copyright (c) 2008-2011 Alexander Chernov <cher@ejudge.ru>"
    echo "usage: ejudge-upgrade-web [HTDOCROOT] [CGIBINDIR] [DOJOPATH/SAVEDIR]"
    exit 0
}

die() {
    echo "ejudge-upgrade-web:" "$@" 2>&1
    exit 1
}

checked_sln() {
    if ! ln -sf "$1" "$2"
    then
        die "cannot symlink $1 to $2"
    fi
}

if [ "$1" = "--version" ]; then exec "${bindir}/ejudge-config" --version; fi
if [ "$1" = "--help" ]; then write_help; fi

htdocsroot="$1"
cgibindir="$2"
dojodir="$3"

current_dir=`pwd`

[ "${htdocsroot}" != "" ] || htdocsroot="${dflt_httpd_htdocs_dir}"
[ "${cgibindir}" != "" ] || cgibindir="${dflt_httpd_cgi_bin_dir}"
[ "${dojodir}" != "" ] || dojodir="${HOME}"

[ -d "${htdocsroot}" ] || die "${htdocsroot} is not a directory"
[ -d "${cgibindir}" ] || die "${cgibindir} is not a directory"

# download the archive
if [ -f "${dojodir}" ]
then
    # assume, that this is a tar archive
    dojofile="${dojodir}"
elif [ -d "${dojodir}" ]
then
    dojofile="${dojodir}/${dojoarchive}"
    if [ -f "${dojofile}" ]
    then
        echo "${dojoarchive} is already downloaded" >& 2
    else
        # download dojo-release-1.2.1.tar.gz from the ejudge web site
        # try wget
        if ! wget -P "${dojodir}" "${site}/${dojoarchive}"
        then
            die "cannot download ${dojoarchive} from ${site}"
        fi
        [ -f "${dojofile}" ] || die "${dojofile} still nonexistant"
    fi
else
    die "${dojodir} is neither a directory nor a file"
fi

target="${htdocsroot}${style_prefix}"

if [ ! -e "${target}" ]
then
    if ! mkdir "${target}"
    then
        die "cannot create ${target} directory"
    fi
fi

[ -d "${target}" ] || die "${target} is not a directory"

if ! cp -pf "${dojofile}" "${target}"
then
    die "cannot copy ${dojofile} to ${target}"
fi

# remove old stuff
# actions.js dojo.js* Storage_* flash6_* storage_* src
cd "${target}"
if ! rm -fr actions.js dojo.js* storage_* flash6_* storage_* src dojo dojox dijit
then
    die "cannot remove old files from ${target}"
fi

# symlink the necessary style files
for i in logo.gif priv.css unpriv.css priv.js unpriv.js filter_expr.html icons sprintf.js
do
    if ! ln -sf "${datadir}/ejudge/style/${i}" "${i}"
    then
        die "cannot symlink ${datadir}/ejudge/style/${i} to ${i}"
    fi
done

# extract and install dojo-1.2.* archive

if ! tar xf "${dojoarchive}"
then
    die "cannot extract files from ${dojoarchive}"
fi

dojobase=`basename "${dojoarchive}"`
dojobase=${dojobase/.tar.gz/}
for i in dojo dojox dijit
do
    if ! mv "${dojobase}/${i}" .
    then
        die "cannot move ${dojobase}/${i} to ."
    fi
done

rm -fr "${dojobase}" "${dojoarchive}"

# symlink the CGI programs
cd "${cgibindir}"

checked_sln "${ejudge_cgibindir}/serve-control${cgi_suffix}" "serve-control${cgi_suffix}"
checked_sln "${ejudge_cgibindir}/users${cgi_suffix}" "users${cgi_suffix}"
checked_sln "${ejudge_cgibindir}/new-client${cgi_suffix}" "new-client${cgi_suffix}"
checked_sln "${ejudge_cgibindir}/new-client${cgi_suffix}" "client${cgi_suffix}"
checked_sln "${ejudge_cgibindir}/new-client${cgi_suffix}" "team${cgi_suffix}"
checked_sln "${ejudge_cgibindir}/new-client${cgi_suffix}" "new-register${cgi_suffix}"
checked_sln "${ejudge_cgibindir}/new-client${cgi_suffix}" "register${cgi_suffix}"
checked_sln "${ejudge_cgibindir}/new-client${cgi_suffix}" "new-master${cgi_suffix}"
checked_sln "${ejudge_cgibindir}/new-client${cgi_suffix}" "master${cgi_suffix}"
checked_sln "${ejudge_cgibindir}/new-client${cgi_suffix}" "new-judge${cgi_suffix}"
checked_sln "${ejudge_cgibindir}/new-client${cgi_suffix}" "judge${cgi_suffix}"

echo "upgrade complete"
