#!/bin/bash

# FIXME: waiting for daimi staff reorganization of /download

# Copyright (c) 2004-2007, The PalCom Project consortium.
# See http://www.ist-palcom.org/consortium.
# 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
# 
#     * Neither the name of the PalCom Project nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# Used in BASH to pack out-of-box and developer zips for download.

ROOT=`pal-dev-root`
DOWNLOAD=`echo "${ROOT}../download" | sed -e "s%palcom/\.\./%%g"`

if [ ! -d ${DOWNLOAD} ]; then
   echo Directory
   echo "   $DOWNLOAD"
   echo does not exist.
   echo You should check out
   echo "   http://svn.ist-palcom.org/svn/palcom/download"
   echo as 
   echo "   ${DOWNLOAD}"
   exit
fi

answer_to_all=0
all_committed=0
global_revision=""

function commit_all ()
{
    if [ $all_committed -eq 0 ]; then
	echo ""
	echo "It necessary first to commit your entire workspace"
	echo "to get global revision number correct."
	ask "OK to commit from $ROOT (if in doubt quit and check your tree)"
	if [ $? -eq 1 ]; then
	    cd $ROOT
	    svn commit -m "Committing entire tree for pal-dev-export-zip"
	    all_committed=1
	fi
    fi
    if [ $all_committed -eq 0 ]; then
	ask "Continue without doing a global commit"
	if [ $? -eq 0 ]; then
	    exit
	fi 
    fi 
    echo ""
    echo "It necessary next to update your entire workspace"
    echo "to get global revision number correct."
    ask "OK to update from $ROOT"
    if [ $? -eq 1 ]; then
	cd $ROOT
	svn update
    fi

}

function write_global_revision_file ()
{
    root="$1"
    revisionfile="${root}REVISION.txt"
    echo ""
    echo "Generating $revisionfile."
    if [ "$global_revision" == "" ]; then
	global_revision=`cd $ROOT; svnversion -n`
    fi 
    echo "Revision $global_revision" > $revisionfile
}

function write_info_file ()
{
    file="$1"
    info_file=`echo $file | sed -e 's/\.zip/_info.txt/'`
    /bin/ls -l --human-readable --time-style=+"%b-%d-%Y" $file | awk "{print \$5\"b, \"\$6\", revision $global_revision\""} > $info_file;
}

function ask ()
{
    question="$1"
    while [ 1=1 ] 
    do
      echo -n "$question? (y/n/Y/N/q/h) "
      if [ $answer_to_all -eq 1 ]; then
	  echo "Y"
	  return 1
      fi
      if [ $answer_to_all -eq -1 ]; then
	  echo "N"
	  return 0
      fi
      read answer
      case $answer in
          h)
             echo Possible answers:
	     echo "  y  - yes (to this question)"
	     echo "  n  - no  (to this question)"
	     echo "  Y  - yes (to this and all remaining questions)"
	     echo "  N  - no  (to this and all remaining questions)"
	     echo "  q  - quit script"
	     echo "  h  - display this help"
             echo ""
             continue
             ;;
          n)
             return 0
             ;;
          q)
             exit
             ;;
          Y)
             answer_to_all=1
             return 1
             ;;
          N)
             answer_to_all=-1
             return 0
             ;;
      esac 
      return 1
    done 
}

echo ""
echo "Creating the following ZIP files:"
echo "  1. $DOWNLOAD/palbox.zip from bin + lib + doc in your workspace"
echo "  2. $DOWNLOAD/palcom.zip from a complete checkout from SVN"
ask "OK to proceed"
if [ $? -eq 1 ]; then

    # palbox.zip

    echo ""
    echo "Creating $DOWNLOAD/palbox.zip from bin + lib + doc in your workspace"
    commit_all
    write_global_revision_file $ROOT
    echo Creating file list.
    cd "$ROOT/.."
    files=`find palcom/bin palcom/lib palcom/doc -type f | grep -v '\.svn' | grep -v 'palcom/doc/developer' | grep -v 'palcom/doc/tex'`
    files="palcom/REVISION.txt $files palcom/doc/tex/companion.pdf"

    echo Zipping
    /bin/rm -f ${DOWNLOAD}/palbox.zip
    echo $files | xargs zip -r ${DOWNLOAD}/palbox.zip
    echo "  --> ${DOWNLOAD}/palbox.zip"
    write_info_file "${DOWNLOAD}/palbox.zip" 

    # palcom.zip

    echo ""
    ask "Also create $DOWNLOAD/palcom.zip from complete SVN checkout"
    if [ $? -eq 1 ]; then

	echo Checking out to /tmp/palcom
	/bin/rm -rf /tmp/palcom
	cd /tmp
	svn export http://svn.ist-palcom.org/svn/palcom/trunk palcom
	write_global_revision_file /tmp/palcom/
	
	echo Zipping
	/bin/rm -f ${DOWNLOAD}/palcom.zip
	zip -r ${DOWNLOAD}/palcom.zip palcom
	echo "  --> ${DOWNLOAD}/palcom.zip"
	write_info_file "${DOWNLOAD}/palcom.zip" 
	/bin/rm -rf /tmp/palcom
	
    fi 
fi 


echo ""
echo Remember manual svn commit in ${DOWNLOAD}.
