--- /dev/null
+#!/bin/bash
+
+#TODO decide about sql files and other odd files
+#TODO make sure access_pathmap.ini is fixed so Open-ILS/xul/staff_client/ files map properly
+
+PROD_SERVERS=" app1-1 app1-2 app2-1 app2-2 app3-1 app3-2 stanns bibc-prod "
+REPO='.'
+
+function usage()
+{
+ cat <<EOF
+USAGE:
+./git-deploy.sh -g ~/evergreen/.git -r origin -b HEAD host1 [ host2 ... ]
+./git-deploy.sh -r origin -b user/lwhalen/rt15288 -P
+
+This script is currently in testing. By default it will echo the commands it would run to the screen.
+If you wan to deploy something you need to use the -d option.
+
+e.g. ./git-deploy.sh -r origin -b HEAD -d host1
+
+As well, this script will not deploy to our production servers currently. It will print out a message
+saying so if you try to use a production server as a host option or if you use the -P option to deploy
+to all production servers.
+
+OPTIONS:
+ -g The location of the git repo
+ -r The remote to deploy from
+ -b The branch on the remote to deploy from
+ -P Deploy to all production servers
+ -d Actually deploy. While testing this option is needed to force the commands to execute
+EOF
+}
+
+function create_tmp_file() {
+ TMP_DIR=$1
+ FILENAME=$2
+ touch $TMP_DIR/$FILENAME
+ echo "$TMP_DIR/$FILENAME"
+}
+
+function get_full_path() {
+ RELATIVE_PATH=$1
+
+ for COMPONENT in `access_pathmap.pl --config $PATH_MAP_FILE`
+ do
+ BASE_PATH=`access_pathmap.pl --config $PATH_MAP_FILE --component $COMPONENT --srcpath $RELATIVE_PATH`
+ if [[ $BASE_PATH ]]
+ then
+ #We extract the last directory in the base path so we can
+ #use parameter substitution to remove everything up to the
+ #last directory in the BASE_PATH from the RELATIVE_PATH
+ #this leaves us with the part of the git source tree path
+ #that needs to be appended to the base path in order to locate
+ #the file on the server
+ LAST_DIR_IN_BASE_PATH=`basename $BASE_PATH`
+ RELATIVE_PART=${RELATIVE_PATH/*$LAST_DIR_IN_BASE_PATH/}
+ echo $BASE_PATH$RELATIVE_PART
+ return;
+ fi
+ done
+}
+
+function deployfile() {
+ REMOTE=$1
+ BRANCH=$2
+ RELATIVE_PATH=$3
+ FILENAME=$4
+ TMP_DIR=$5
+ HOSTS=$6
+
+ PERL_FILE_RE='^.*.pm$'
+ SQL_FILE_RE='^.*.sql$'
+ TT2_FILE_RE='^Open-ILS/src/(templates)/(.*)$'
+ USER='opensrf'
+
+ if [[ $FILENAME =~ $PERL_FILE_RE ]]
+ then
+ USER='root'
+ elif [[ $FILENAME =~ $SQL_FILE_RE ]]
+ then
+ echo 'SQL file -- TAKE THE TIME TO LOAD THIS PROPERLY INTO THE DATABASE. DO NOT FORGET TO WRAP IT IN BEGIN AND COMMIT'
+ return
+ else
+ USER='opensrf'
+ fi
+
+ FULL_PATH=`get_full_path $RELATIVE_PATH`
+
+ TEMPFILE=`create_tmp_file $TMP_DIR $FILENAME`
+ git --git-dir $REPO show $REMOTE/$BRANCH:$RELATIVE_PATH/$FILENAME > $TEMPFILE
+ for HOST in $HOSTS
+ do
+ if [[ $PROD_SERVERS =~ " $HOST " ]]
+ then
+ echo "Cannot deploy `basename $TEMPFILE` to $HOST while testing. Please use another server"
+ else
+ COMMAND="deployfile.sh -f $TEMPFILE -p $FULL_PATH -u $USER -b $HOST"
+ if [[ -z "$DEPLOY" ]]
+ then
+ echo $COMMAND
+ else
+ $COMMAND
+ fi
+
+ fi
+ done
+}
+
+while getopts ":g:r:b:c:Pdh" opt
+do
+ case $opt in
+ g ) REPO=$OPTARG;;
+ r ) REMOTE=$OPTARG;;
+ b ) BRANCH=$OPTARG;;
+ c ) PATH_MAP_FILE=$OPTARG;;
+ P ) PROD="P";;
+ d ) DEPLOY="d";;
+ h ) usage && exit 0;;
+ esac
+done
+
+if [[ ! "$REPO" =~ .git$ ]]
+then
+ REPO=$REPO"/.git"
+fi
+
+shift $(($OPTIND - 1))
+
+if [[ -z "$PROD" ]]
+then
+ HOST_LIST=$@
+else
+ HOST_LIST=$@$PROD_SERVERS
+fi
+
+if [[ -z $HOST_LIST ]]
+then
+ echo 'You must specify a host to deploy to in order to use this script'
+ exit 1
+fi
+
+TMP_DIR=`mktemp -d`
+git --git-dir $REPO show $REMOTE/$BRANCH --name-only --oneline | awk '{if(NR!=1) {print}}' | \
+while read PATH_AND_FILENAME
+do
+ FILENAME=`basename $PATH_AND_FILENAME`
+ RELATIVE_PATH=`dirname $PATH_AND_FILENAME`
+ deployfile $REMOTE $BRANCH $RELATIVE_PATH $FILENAME $TMP_DIR "$HOST_LIST"
+done