#!/bin/sh

DM_SVN_URL=https://subversion.xray.aps.anl.gov/DataManagement

execute() {
    echo "Executing: $@"
    eval "$@"
}

# load tools
execute svn export $DM_SVN_URL/trunk/tools/ExperimentSynchronizer ExperimentSynchronizer
EXPSYNC_DIR=ExperimentSynchronizer
cd $EXPSYNC_DIR/lib

# load java 7
execute svn export $DM_SVN_URL/support/src/jdk-7u51-linux-x64.tar.gz 
JAVA_VERSION=7u51
tar zxf jdk-${JAVA_VERSION}*.tar.gz

cd ../resources
read -p "Enter Data Acquisition Service Host: " DAQ_SERVICE_HOST
read -p "Enter Experiment Data Root Directory (on DAQ): " DAQ_DATA_DIR
read -p "Enter Data Processing Host: " DAQ_REC_SERVICE_HOST
read -p "Enter Experiment Data Root Directory (on data processing node): " DAQ_REC_DATA_DIR

echo "Generating config file"
cmd="cat config.properties.template \
    | sed 's?DAQ_CONNECTION?https://$DAQ_SERVICE_HOST:33336/dm?g' \
    | sed 's?DATA_DIR?$DAQ_DATA_DIR?g' \
    | sed 's?DAQ_REC_CONNECTION?https://$DAQ_REC_SERVICE_HOST:33336/dm?g' \
    | sed 's?DATA_REC_DIR?$DAQ_REC_DATA_DIR?g' \
    > config.properties"
eval $cmd || exit 1

cd ..
mkdir bin
execute lib/jdk1.7.0_51/bin/javac -classpath lib/*:resources/* -d bin/ src/gov/anl/dm/esafsync/*.java src/gov/anl/dm/esafsync/serviceconn/*.java
mkdir target
execute lib/jdk1.7.0_51/bin/jar cfm target/ExperimentSynchronizer.jar manifest.txt -C bin/ . 

chmod 775 run_expSync.sh
cd ..

