#!/usr/bin/env bash
#
# Author: Gerwin Klein, NICTA
#
# publishes archive entry + main web pages on isa-afp
# 
# This script expects Isabelle version >= 2008-10-05 
# (uses isabelle instead of isatool)

## settings

VERSION=2016
AFP_VERSION=afp-$VERSION
ISABELLE_VERSION=Isabelle$VERSION

DEST=afpweb@isa-afp.org:
FILES=frs.sourceforge.net:/home/frs/project/afp/afp-$ISABELLE_VERSION/
SRC=ssh://hg@bitbucket.org/isa-afp/$AFP_VERSION

if [ -n "$SF_LOGIN" ]; then
    LN=$SF_LOGIN
else
    LN=$LOGNAME
fi

FILES=$LN@$FILES

EXPORT_PRE=afp
HG=hg
DATE=`date '+%Y-%m-%d'`
THYS=thys

TAR=tar

PRG="$(basename "$0")"
DIR="$(dirname "$0")"

## functions

function usage()
{
  echo
  echo "Usage: $PRG [options] <tag> [<entries>|-]"
  echo 
  echo "  Checks out web site and archive entries and publishes them on the isa-afp.org web site"
  echo
  echo "Options:"
  echo "  -f             do not ask before publishing"
  echo "  -t <isabelle>  use specified path to isabelle tool script"
  echo "  -r             use env variable ISABELLE_RELEASES to find isabelle"
  echo
  echo "Examples:"
  echo "  $PRG -t /usr/proj/Isabelle2009/bin/isabelle Isabelle2009 Example-Submission"  
  echo
  echo "  $PRG -r Isabelle2009 Example-Submission"  
  exit 1
}

function fail()
{
  echo "$1" >&2
  exit 2
}

function check_isabelle_tool()
{
  [ -x $ISABELLE_TOOL ] || fail "No isabelle found at [$ISABELLE_TOOL]."
  ML_IDENTIFIER=`$ISABELLE_TOOL getenv -b ML_IDENTIFIER` || fail "could not identify ML system"
  ISABELLE_HOME=`$ISABELLE_TOOL getenv -b ISABELLE_HOME` || fail "could not find Isabelle home"
  export ISABELLE_IMAGE_PATH="$ISABELLE_HOME/heaps/$ML_IDENTIFIER/"
}

## argument checking

[ "$#" -lt "2" -o "$1" = "-?" ] && usage

ISABELLE_TOOL=isabelle
INTERACTIVE="yes"

while getopts "ft:r" OPT
do
    case "$OPT" in
      r)
        [ "$ISABELLE_RELEASES" == "" ] && fail "ISABELLE_RELEASES not set"
        USE_RELEASE=1
        ;;
      f)
        INTERACTIVE="no"
        ;;
      t)
        ISABELLE_TOOL="$OPTARG"
        check_isabelle_tool
        ;;
    esac
done

shift $(($OPTIND - 1))

TAG="$1" 
shift

if [ "$USE_RELEASE" == "1" ]; then
    ISABELLE_TOOL="$ISABELLE_RELEASES/$TAG/bin/isabelle"
    check_isabelle_tool
fi


EXPORT=$EXPORT_PRE-$DATE

###

WD="$(pwd)"
cd "$DIR"
ROOT="$(hg root)" || fail "could not obtain repos root"
cd "$ROOT" || fail "could not cd to repos root"

echo "Checking sync with $SRC"
$HG outgoing $SRC && fail "Push changes to bitbucket first."

echo "Exporting from working copy $ROOT"
HG_EXPORT=afp-export-$DATE
rm -rf $HG_EXPORT
$HG archive -I thys -I web -I etc -I tools $HG_EXPORT || fail "hg archive failed."
cd $HG_EXPORT
echo -n $DATE > web/release-date.txt
mkdir $EXPORT
mv thys etc tools $EXPORT/

if [ "$1" != "-" ]; then
    echo "Cleaning up browser_info directory"
    BROWSER_INFO=`$ISABELLE_TOOL getenv -b ISABELLE_BROWSER_INFO` || fail "could not find browser info"
    [ -e "$BROWSER_INFO" ] && rm -rf $BROWSER_INFO

    HTML_THYS=web/browser_info/$TAG
    TARS=web/release
    mkdir -p $HTML_THYS
    ln -s $TAG web/browser_info/current 
    ln -s ../front.css web/entries/front.css
    mkdir -p $TARS

    echo "Tarring [$EXPORT]"
    $TAR -cf $EXPORT.tar $EXPORT
    gzip --best -f $EXPORT.tar
    ln -s $EXPORT.tar.gz $TARS/afp-current.tar.gz
    mv $EXPORT.tar.gz $TARS/

    echo "Generating HTML for [$@]"
    $ISABELLE_TOOL afp_build -- -v -c $@  || fail "isabelle afp_build failed on [$@]"

    cd $EXPORT/thys
    for ENTRY in $@; do
        if [ -d $ENTRY ]; then
            echo "Tarring [$ENTRY]"
            $TAR -cf $EXPORT_PRE-$ENTRY-$DATE.tar $ENTRY
            gzip --best -f $EXPORT_PRE-$ENTRY-$DATE.tar
            mv $EXPORT_PRE-$ENTRY-$DATE.tar.gz ../../$TARS/
            ln -s $EXPORT_PRE-$ENTRY-$DATE.tar.gz ../../$TARS/$EXPORT_PRE-$ENTRY-current.tar.gz
            echo "Finished [$ENTRY]"
        fi
    done
    cd ../..

    echo "Copying generated HTML"
    for DIR in $BROWSER_INFO/*; do
        if [ -d $DIR ]; then
            cp -r $DIR $HTML_THYS        
        fi
    done

    if [ "$INTERACTIVE" == "yes" ]; then
        echo "Web pages are prepared for publication under"
        echo "[`pwd`/web/]."
        echo "Please check content."
        read -n 1 -p "Type y if you want to publish. Any other key quits." RESPONSE
    else
        RESPONSE="y"
    fi
else
    RESPONSE="y"    
fi

if [ "$RESPONSE" == "y" ]; then
    if [ "$TARS" != "" ]; then
        echo
        echo "Pushing $EXPORT to [$FILES]"
        scp $TARS/$EXPORT.tar.gz $FILES
    fi
    echo
    echo "Publishing to [$DEST]"
    chmod -R g-w web
    chmod -R a+r web
    find web -type d | xargs chmod a+x
    chmod 755 web
    rsync -rplvz --links --rsh=ssh web/ $DEST && echo "Finished."
else
    echo
    echo "Aborted."
    exit 1;
fi

