#!/bin/bash

# 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 export latest built elements from /developer to /bin and /lib

ROOT=`pal-dev-root`
case $OSTYPE in
cygwin*)
   PLATFORM="Cygwin"
   VMDST="vm/pal-vm-cygwin"
   EXE=".exe"
   ;;
darwin*)
   PLATFORM="MacOSX"
   VMDST="vm/pal-vm-macosx"
   ;;
linux*)
   PLATFORM="Linux"
   VMDST="vm/pal-vm-linux"
   ;;
esac

BIN=${ROOT}/bin
LIB=${ROOT}lib

VMBASE="${ROOT}developer/runtime-environment/engine/pal-vm"
BYTEBASE="${ROOT}developer/tools/bytecode/pal-bytecodes"
ASMBASE="${ROOT}developer/tools/bytecode/pal-asm"
DISBASE="${ROOT}developer/tools/bytecode/pal-dis"
STBASE="${ROOT}developer/tools/compilers/pal-st"
JBASE="${ROOT}developer/tools/compilers/pal-j"
DEPBASE="${ROOT}developer/tools/reflect/pal-dep"
DEVLIB="${ROOT}developer/dev-lib"

answer_to_all=0
goto_svn=0
set -f  # disable filename expansion by default

function ask ()
{
    question="$1"
    if [ $goto_svn -eq 1 ]; then
	return 0
    fi
    while [ 1=1 ] 
    do
      echo -n "$question? (y/n/Y/N/C/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 "  C  - Skip next questions until SVN commit"
	     echo "  q  - quit script"
	     echo "  h  - display this help"
             echo ""
             continue
             ;;
          n)
             return 0
             ;;
          q)
             exit
             ;;
          Y)
             answer_to_all=1
             return 1
             ;;
          C)
	     goto_svn=1
             return 0
             ;;
          N)
             answer_to_all=-1
             return 0
             ;;
      esac 
      return 1
    done 
}

function ask_export ()
{
    # FIXME: allow undo (remove + svn update) 

    src="$1"
    dst="$2"

    ask "Export $src"
    if [ $? -eq 1 ]; then
	echo "    -> $dst"
	return 1
    fi
}

function ask_cp_glob ()
{
    echo ""
    set +f
    dir="$2"
    files="$1"
    for f in $files; do
	if [ `echo "$f" | sed -e 's%.*\.test\.prc$%TEST%g'` == "TEST" ]; then
	    echo Ignored test program $f
	else
	    ask_export "$f" "$dir"
	    if [ $? -eq 1 ]; then
		cp "$f" "$dir"
	    fi
	fi 
    done
    set -f
}

function ask_cp ()
{
    echo ""
    dir="$2"
    file="$1"
    ask_export "$file" "$dir"
    if [ $? -eq 1 ]; then
	cp "$file" "$dir"
    fi
}

function ask_jar ()
{
    echo ""
    dir="$1"
    jar="$2"
    ask_export "$dir" "$jar"
    if [ $? -eq 1 ]; then
        cd "$dir"
        jar cf "$jar" .
    fi
}

function ask_jam ()
{
    echo ""
    dir="$1"
    jar="$2"
    man="$3"
    ask_export "$dir" "$jar"
    if [ $? -eq 1 ]; then
        cd "$dir"
        jar cfm "$jar" "$man" .
    fi
}


function get_version_using_strings ()
{
    # Works with exe files, class files, but not jar files :-(
    file="$*"
    versionstring=`strings "$file" | grep version:`
    if [ "$versionstring" != "" ]; then
	echo "$versionstring" | sed -e 's/^.*version: //g'
    else
	echo "0.0.0"
    fi 
}

function get_version ()
{
    program="$1"
    versionstring=`"$program" --version 2>&1 | grep version: `
    if [ "$versionstring" != "" ]; then
	echo "$versionstring" | sed -e 's/^.*version: //g'
    else
	echo "0.0.0"
    fi 
}

function get_major ()
{
    echo "$1" | sed -e 's/^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\1/g'
}
function get_minor ()
{
    echo "$1" | sed -e 's/^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\2/g'
}
function get_build ()
{
    echo "$1" | sed -e 's/^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\3/g'
}

function compare_versions ()
{
    # returns 1 or 2 if $1 has higher minor/major than $2, otherwise 0
    newversion=`get_version "$1"`
    oldversion=`get_version "$2"`

    newmajor=`get_major $newversion`
    oldmajor=`get_major $oldversion`
    if [ $newmajor -gt $oldmajor ]; then return 2; fi

    newminor=`get_minor $newversion`
    oldminor=`get_minor $oldversion`
    if [ $newminor -gt $oldminor ]; then return 1; fi

    # Only difference in build
    return 0
}

function ask_export_version ()
{
    if [ $goto_svn -eq 1 ]; then
	return 0
    fi

    echo ""
    name="$1"
    oldname=`echo $name | sed -e 's/-dev//g'`
    newversion=`get_version "$name"`
    oldversion=`get_version "$oldname"`
    echo New \"$name\" is version \"$newversion\", current \"$oldname\" is version \"$oldversion\"
    compare_versions "$name" "$oldname"
    case $? in
    2) echo Export is NEEDED
    ;;
    1) echo Export is recommended
    ;;
    0) echo Export should not be needed
    ;;
    esac
}

###################   main   ####################

echo "Exporting PalCom developer tree."

echo Local svn update
cd $ROOT
svn update

# Export pal-vm
# developer/runtime-environment/engine/pal-vm/src/VM/Version.hpp
if [ -f "${VMBASE}/$PLATFORM-Release/pal-vm${EXE}" ]; then
    ask_export_version "pal-dev-vm" 
    ask_cp "${VMBASE}/$PLATFORM-Release/pal-vm${EXE}" "${BIN}/${VMDST}${EXE}"
    chmod a+x "${BIN}/${VMDST}${EXE}"
else
    echo VM not found: "${VMBASE}/$PLATFORM-Release/pal-vm${EXE}"
fi
if [ -f "${VMBASE}/$PLATFORM-Debug/pal-vm-debug${EXE}" ]; then
    ask_export_version "pal-dev-vm -d" 
    ask_cp "${VMBASE}/$PLATFORM-Debug/pal-vm-debug${EXE}" "${BIN}/${VMDST}-debug${EXE}"
    chmod a+x "${BIN}/${VMDST}-debug${EXE}"
else
    echo VM not found: "${VMBASE}/$PLATFORM-Debug/pal-vm-debug${EXE}"
fi
if [ -f "${VMBASE}/$PLATFORM-Checked/pal-vm-checked${EXE}" ]; then
    ask_export_version "pal-dev-vm -c" 
    ask_cp "${VMBASE}/$PLATFORM-Checked/pal-vm-checked${EXE}" "${BIN}/${VMDST}-checked${EXE}"
    chmod a+x "${BIN}/${VMDST}-checked${EXE}"
else
    echo VM not found: "${VMBASE}/$PLATFORM-Checked/pal-vm-checked${EXE}"
fi
if [ -f "${VMBASE}/$PLATFORM-Profiling/pal-vm-profiling${EXE}" ]; then
    ask_export_version "pal-dev-vm -p" 
    ask_cp "${VMBASE}/$PLATFORM-Profiling/pal-vm-profiling${EXE}" "${BIN}/${VMDST}-profiling${EXE}"
    chmod a+x "${BIN}/${VMDST}-profiling${EXE}"
else
    echo VM not found: "${VMBASE}/$PLATFORM-Profiling/pal-vm-profiling${EXE}"
fi

# Export assembler, disassembler
# developer/tools/bytecode/pal-asm/src/ist/palcom/assembler/Version.java
ask_export_version "pal-dev-asm" 
ask_jar     "${BYTEBASE}/bin" "${LIB}/jars/pal-bytecodes.jar"
ask_jam     "${ASMBASE}/bin"  "${LIB}/jars/pal-asm.jar" "${ASMBASE}/src/ist/palcom/assembler/manifest.mt"
ask_export_version "pal-dev-dis" 
ask_jam     "${DISBASE}/bin"  "${LIB}/jars/pal-dis.jar" "${DISBASE}/src/ist/palcom/disassembler/manifest.mt"

# Export dependency analyzer
# developer/tools/reflect/pal-dep/src/ist/palcom/reflect/dependency/Version.java
ask_export_version "pal-dev-dep" 
ask_jar     "${DEPBASE}/bin" "${LIB}/jars/pal-dep.jar" 

# Export smalltalk compiler
# developer/tools/compilers/pal-st/src/ist/palcom/st/Version.java
ask_export_version "pal-dev-st" 
ask_cp      "${STBASE}/lib/antlr.jar" "${LIB}/jars/antlr.jar"
ask_jam     "${STBASE}/bin" "${LIB}/jars/pal-st.jar" "${STBASE}/src/ist/palcom/st/manifest.mt"

# Export Java compiler
# developer/tools/compilers/pal-j/Backend/version/Version.java
ask_export_version "pal-dev-j" 
ask_cp      "${JBASE}/lib/pal-j.jar" "${LIB}/jars/pal-j.jar"

# Export dev-lib
ask_cp_glob "${DEVLIB}/*.prc" "${LIB}"
ask_cp "${DEVLIB}/jars/generated/palcom.jar" "${LIB}/jars"
ask_cp "${DEVLIB}/jars/generated/palcom-applications.jar" "${LIB}/jars"

# SVN
echo ""
goto_svn=0
for dir in "${BIN}" "${LIB}" 
do
  echo SVN status in $dir:
  cd "$dir"
  svn stat
  ask "SVN commit in $dir"
  if [ $? -eq 1 ]; then
      echo -n "Log message: "
      read svnmsg
      svn commit -m "$svnmsg"
  fi
done


# Create zip files
pal-dev-export-zip


