#! /bin/bash 

##
#  Copyright 2007-2015 University Of Southern California
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
##
#
#
# This is a script that creates a Pegasus worker package out of an existing pegasus
# installation on the submit host.
#
# Author: Karan Vahi <vahi@isi.edu>
#


set -e

OUTPUT_DIR=$1

if [ "X${OUTPUT_DIR}" = "X" ];then
    OUTPUT_DIR=`pwd`
fi

WORKER_PACKAGE_EXECUTABLES=(  "pegasus-cluster"  "pegasus-config"    
                              "pegasus-keg"   "pegasus-kickstart"
			     "pegasus-s3" "pegasus-transfer")

# call out to pegasus config and setup appropriate
# PEGASUS shell variables to discover other tools
BIN_DIR=`dirname $0`
PEGASUS_CONFIG="$BIN_DIR/pegasus-config"
eval `$PEGASUS_CONFIG --sh-dump`
. $PEGASUS_SHARE_DIR/common.sh

# get system version
. $PEGASUS_SHARE_DIR/sh/pegasus-lite-common.sh
SYSTEM=$(pegasus_lite_get_system)
if [ $? != 0 ]; then
    echo "Unable to determine the system identifier. pegasus_lite_get_system returned: $SYSTEM"
    exit 1
fi
echo $SYSTEM

PEGASUS_VERSION=`$BIN_DIR/pegasus-version`
WORKER_PACKAGE_NAME=pegasus-worker-${PEGASUS_VERSION}-${SYSTEM}.tar.gz
echo $WORKER_PACKAGE_NAME

# create a temp work directory directory
# where we gather various things for the worker package
BASE_DIR=`mktemp -d pegasus.XXXXX`
WORKER_DIR=${BASE_DIR}/pegasus-${PEGASUS_VERSION}
echo $WORKER_DIR
mkdir -p ${WORKER_DIR}/bin

echo `pegasus-version -f` > ${WORKER_DIR}/stamp

for EXECUTABLE in "${WORKER_PACKAGE_EXECUTABLES[@]}"; do
    cp -a ${PEGASUS_BIN_DIR}/${EXECUTABLE} ${WORKER_DIR}/bin/
done

# copy both pegasus python dir and boto from python externals dir
# keep similar directory structure to on submit host
# determine a sudo PEGASUS_HOME from PEGASUS_BIN and use that for
# determining relative paths
PEGASUS_HOME=`cd $PEGASUS_BIN_DIR/.. && pwd`
echo "Root of Pegasus install is $PEGASUS_HOME"
for PYTHON_DIR in $PEGASUS_PYTHON_DIR/Pegasus $PEGASUS_PYTHON_EXTERNALS_DIR/boto; do
    RELATIVE_DIR=`echo $PYTHON_DIR | sed -E "s@.*${PEGASUS_HOME}\/(.*)@\1@"`
    echo "Relative pegasus python directory is $RELATIVE_DIR"
    mkdir -p ${WORKER_DIR}/${RELATIVE_DIR}
    cp -a -R ${PYTHON_DIR}/* ${WORKER_DIR}/${RELATIVE_DIR}/     
done

# "access" the files ones to make sure tar does not give us read errors
# on distributed filesystems
find ${BASE_DIR} -exec ls -l {} \; >/dev/null 2>&1

# create place the worker package in the output directory
(
cd ${BASE_DIR}
tar zcf ${OUTPUT_DIR}/$WORKER_PACKAGE_NAME pegasus-${PEGASUS_VERSION}
)

#clean 
rm -rf ${BASE_DIR}

# the planner looks for this string
echo "# PEGASUS_WORKER_PACKAGE=${WORKER_PACKAGE_NAME}"

exit 0

