#!/bin/sh
#
# Copyright (c) 2012 PostgreSQL Global Development Group
#
# Bourne shell script to build the various Java version jar
# files for the PosgtreSQL JDBC.
#
# Author Dana M. Proctor, danap@dandymadeproductions.com
# Version 1.0 10/31/2012
#         1.1 11/05/2012 Add -s Switch Argument to Build tar.gz Source File.
#         1.2 11/14/2012 Correction for jdbc3 for 1.4, jdbc3g 1.5, jdbc4 
#                        1.6, jdbc41 1.7/1.8. Added PGJDBC_BUILD_VERSION &
#                        Output During Building. 
#
# Requirements:
#
# 1. To be used from the root pgjdbc source code directory.
# 2. At lease one of the Java SDKs, the JRE is not sufficient.
# 3. Apache-Ant.
#
# Arugments: -s, Build Source File.
#

#
# Setup some default system variables.
#
ORGINAL_PATH=$PATH
LOCAL_BIN=$HOME/pgjdbc_bin
BUILD_PATH=$LOCAL_BIN:$PATH
PGJDBC_BUILD_VERSION=1.2

#
# Set your local installation system variables.
#
JAVA_SDK_PATH=/usr/lib/java
ANT_INSTALL_HOME=/usr/local/lib/apache-ant-1.8.4
ANT_JAR=jars/postgresql.jar
BUILD_JARS=build-jars

SOURCE_BASE_NAME=postgresql-jdbc
SOURCE_DIRS=doc\\nMETA-INF\\norg
SOURCE_FILES=build.properties\\nbuild.xml\\nLICENSE\\nREADME\\nupdate-translations.sh

# Can not check everything, but at least
# see if Java SDK path and ant path good.

if [ ! -d $JAVA_SDK_PATH ]; then
   echo "The Java SDK path does not appear to be valid!"
   exit 1 
fi

echo "JAVA_SDK_PATH="$JAVA_SDK_PATH

if [ -z $ANT_HOME ]; then
   ANT_HOME=$ANT_INSTALL_HOME
   export ANT_HOME
fi

echo "ANT_HOME="$ANT_HOME

if [ ! -d $ANT_HOME ]; then
   echo "The Apache-Ant variable ANT_HOME does not appear to be valid!"
   exit 1 
fi     

# Collect an input from the user to indicate the version
# build for the jars.

echo
echo -n "Please input a version of the build, ex(9.2-1001): "
read VERSION
echo

# Main part of script that loops through the directories in
# the JAVA_SDK_PATH to build the required jar file(s) for each
# version of the SDK encountered.

mkdir $LOCAL_BIN

if [ ! -d $BUILD_JARS ]; then
mkdir $BUILD_JARS
fi

PATH=$BUILD_PATH
export PATH

for contents in `ls $JAVA_SDK_PATH` ; do

   # Create complete path for contents
   # derived and see if appears to be a
   # valid JDK.
   
   JAVA_SDK=$JAVA_SDK_PATH/$contents
   
   if [ -d $JAVA_SDK ] && [ -f $JAVA_SDK/bin/javac ]; then
      
      #echo $JAVA_SDK
      
      # Valid JDK so set the JAVA_HOME, create links in
      # LOCAL_BIN to the JAVA_SDK.

      JAVA_HOME=$JAVA_SDK
      export JAVA_HOME 
      ln -s $JAVA_SDK/bin/java $LOCAL_BIN/java
      ln -s $JAVA_SDK/bin/javac $LOCAL_BIN/javac
     
      # Collect the appropriate Java Version for
      # composing the jar name extension. Since
      # the output from many java programs use
      # System.err instead of System.out then need
      # capture that instead to get SDK_VERSION.
      # 2> means redirect stderr. &1 means to the
      # same place as stdout.

      SDK_VERSION="$(java -version 2>&1)"
      echo
      echo "pgjdbc-build v$PGJDBC_BUILD_VERSION Building....."
      echo "SDK_VERSION="$SDK_VERSION

      case "$SDK_VERSION" in
      *\ 1.1*)
         JAVA_VERSION="jdbc1"
         ;;
      *\ 1.2* | *\ 1.3*)
         JAVA_VERSION="jdbc2"
         ;;
      *\ 1.4*)
         JAVA_VERSION="jdbc3"
         ;;
      *\ 1.5*)
         JAVA_VERSION="jdbc3g"
         ;;
      *\ 1.6*)
         JAVA_VERSION="jdbc4"
         ;;
      *\ 1.7* | *\ 1.8*)
         JAVA_VERSION="jdbc41"
         ;;
      *)
         JAVA_VERSION="jdbc?"
         ;;
      esac

      #echo $JAVA_VERSION

      # Build the jar file now that everthing has
      # been setup and collected as needed.

      $ANT_HOME/bin/ant clean
      $ANT_HOME/bin/ant

      # Check to see if build was successful. If so
      # then move the created jar to the appropriate
      # name so the next build can take place and
      # cleanup links.

      if [ -f $ANT_JAR ]; then
         mv $ANT_JAR $BUILD_JARS/"postgresql-"$VERSION"."$JAVA_VERSION".jar"
      fi

      if [ -f $LOCAL_BIN/java ]; then
         rm $LOCAL_BIN/java 
      fi

      if [ -f $LOCAL_BIN/javac ]; then
         rm $LOCAL_BIN/javac 
      fi
   fi
done

#
# Build source provided the -s switch argument was given.
#

if [ $# = 1 ] && [ "$1" = "-s" ]; then

   # Create the source directory based on the base
   # name and the input given for the version.

   SOURCE_DIRECTORY=$SOURCE_BASE_NAME-$VERSION.src
   #echo "SOURCE_DIRECTORY:$SOURCE_DIRECTORY"

   if [ ! -d $SOURCE_DIRECTORY ]; then
      mkdir $SOURCE_DIRECTORY
   fi

   # Move the required contents to be source directory
   # for compression.

   for directory in `printf "$SOURCE_DIRS"` ; do
      if [ -d $directory ]; then
         cp -R $directory $SOURCE_DIRECTORY
      fi
   done
   
   for file in `printf "$SOURCE_FILES"` ; do
      if [ -f $file ]; then
         cp $file $SOURCE_DIRECTORY
      fi
   done

   # Create the compressed source file.

   tar -czvf $BUILD_JARS/$SOURCE_DIRECTORY.tar.gz $SOURCE_DIRECTORY > /dev/null 2>&1
fi

# Cleanup, the path shouldn't of changed,
# on exit.
 
if [ -d $LOCAL_BIN ]; then
   rm -rf $LOCAL_BIN
fi
if [ -d $SOURCE_DIRECTORY ]; then
   rm -rf $SOURCE_DIRECTORY
fi

PATH=$ORIGINAL_PATH
export PATH

exit 0
