#!/bin/sh

# JAVA_HOME is not set
env_err() {
	echo JAVA_HOME variable must be set
	echo ---------------------------------------------
  	echo EXAMPLES:
	echo Sun Microsystem...... /j2sdk1.4.1_02 
	echo Oracle .............. /$ORACLE_HOME/jdk
	echo DB2 ................. /$DB2HOME/SQLLIB/java/java12/jdk
	echo ---------------------------------------------
   exit
}

# MAKE THE EAR FILE
make_ear() {
	
	cp com.systemcorp.pmo.auth.AuthListener.properties $WAR_DIR/WEB-INF/classes
	cp com.systemcorp.pmo.auth.jndi.JNDIController.properties $WAR_DIR/WEB-INF/classes
	cp com.systemcorp.pmo.SPListener.properties $WAR_DIR/WEB-INF/classes
	cp ConnectionManager.ini $WAR_DIR/WEB-INF/classes/ConnectionManager.ini
	cp ConnectionPool.ini $WAR_DIR/WEB-INF/classes/ConnectionPool.ini
	cp clients.html $WAR_DIR
	cp InstallIBMRPMPlgin.exe $WAR_DIR/client_installer/
	cp InstallIBMRPMPlgin.msi $WAR_DIR/client_installer/
	cp InstallIBMRPMStdln.exe $WAR_DIR/client_installer/
	cp InstallIBMRPMStdln.msi $WAR_DIR/client_installer/
	
	echo making $EAR_FILE.war 
	cd $WAR_DIR
	$JAVA_HOME/bin/jar cf $EAR_FILE.war *
	mv $WAR_DIR/$EAR_FILE.war $EAR_DIR
	
	echo making $EAR_FILE.ear 
	cd $EAR_DIR
	$JAVA_HOME/bin/jar cf $EAR_FILE.ear *
	
	echo removing the $EAR_FILE.war file from the $EAR_DIR directory 
	rm -r $EAR_DIR/$EAR_FILE.war
	mv $EAR_DIR/$EAR_FILE.ear $CUR_DIR
	echo you will find the $EAR_FILE.ear in $CUR_DIR
	cd $CUR_DIR
}

# Backup previous RPM Ear files
bkp() {
	if [ ! -d bkp ]; then
		mkdir bkp
	fi
	
	if [ -f ./bkp/$EAR_FILE.ear.bak5 ]; then
		echo "deleting ./bkp/$EAR_FILE.ear.bak5"
		rm -f ./bkp/$EAR_FILE.ear.bak5
	fi
		
	x=0
	if [ ! -f ./bkp/$EAR_FILE.ear.bak$x ]; then
	echo moving ./$EAR_FILE.ear to ./bkp/$EAR_FILE.ear.bak$x
	mv ./$EAR_FILE.ear ./bkp/$EAR_FILE.ear.bak$x
	else
	while [ $x -le 4 ]; 
	do
		if [ -f ./bkp/$EAR_FILE.ear.bak$x ]; then
			y=`expr $x + 1`
			echo moving ./bkp/$EAR_FILE.ear.bak$x to ./bkp/$EAR_FILE.ear.bak$y
			mv ./bkp/$EAR_FILE.ear.bak$x ./bkp/$EAR_FILE.ear.bak$y
		fi
		x=`expr $x + 1`
	done
	fi	
#	echo moving ./$EAR_FILE.ear to ./bkp/$EAR_FILE.ear.bak1
#	mv ./$EAR_FILE.ear ./bkp/$EAR_FILE.ear.bak1
}

# TEST EAR FILE NAME
name_file() {

	if [ "$1" = "" ];
	then EAR_FILE=IBMRPM61
	else EAR_FILE=$1
	fi
	
	if [ -f $EAR_FILE.ear ]; then
		bkp
	fi
}

main () {

	
	echo "*** Welcome to IBM Rational Portfolio Manager EARBuilder ***"
	
	if [ "$JAVA_HOME" = "" ]; then
		echo "$JAVE_HOME s required. Please set it and retry again..."
		exit 0
	fi

	echo "1: DB2"
	echo "2: ORACLE"
	echo "Select Which database you want IBMRPM61.ear to be deployed against?"
	
	read ans

case "$ans" in
    1)
        echo "copying db2java.zip..."
        if [ ! -f "$HOME/sqllib/java/db2java.zip" ]; then                                 
        	echo "No db2 JDBC Driver could be found in the $HOME/sqllib/java path"
		exit 0 
        fi
	cp $HOME/sqllib/java/db2java.zip war/WEB-INF/lib/.
	;;
    2)                                                                                        
        echo "copying java12.jar..." 
	if [ ! -f "$ORACLE_HOME/jdbc/lib/classes12.jar" ]; then
		echo "No ORACLE JDBC Driver could be found in the $ORACLE_HOME/jdbc/lib path"
		exit 0
	fi
	cp $ORACLE_HOME/jdbc/lib/classes12.jar war/WEB-INF/lib/. 
	;;
*)
        echo "Usage: $0 {1|2|}"
        exit 0 
        ;;
esac
echo " ***  Making the EAR  *** "
	PATH=$JAVA_HOME:$PATH
	CUR_DIR=$PWD
	WAR_DIR=$CUR_DIR/war
	EAR_DIR=$CUR_DIR/ear
	
	name_file
	make_ear
	
	exit	
}

main
