#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Promote to Production</title></head>"
echo "<body>"
sourceroot=/qa_webroot
targetroot=/t3psolarweb202
lines=0
#-- Define table, log, and error files.
#-- All file operation commands should redirect stdout
#-- to LOG and stderr to ERROR
#-- i.e. command 1>>$LOG 2>>$ERROR
#
#-- Magic CGI stuff here... Get the path from the users
#-- promote web form...
SEED=`date +%s%N`
ERROR=/tmp/$SEED.err
LOG=/tmp/$SEED.xfer
TABLE=${sourceroot}`echo -n $QUERY_STRING | sed -n 's/^.*tablepath=\([^&]*\).*$/\1/p' | sed 's/%2F/\//g'`
echo "<h2>Promoting $TABLE contents to production now</h2>"
#-- Ensure the tablefile is workable
dos2unix -q $TABLE 1>>$LOG 2>>$ERROR
#chown solarweb:sysadmin $TABLE 1>>$LOG 2>>$ERROR
#-- Start main loop through of table
for line in `cat $TABLE`
do
source=$sourceroot`echo $line | awk -F \| '{ print $1 }';`
target=$targetroot`echo $line | awk -F \| '{ print $2 }';`
#-- DEBUG
echo "Source from $TABLE is $source"
echo "Target from $TABLE is $target"
#-- END DEBUG
echo >> $LOG
echo `date` - $source to $target >> $LOG
echo >> $LOG
#-- Make destianation if source is a directory and target doesn't exist
[ -d $source -a ! -e $target ]&& mkdir -p $target 1>>$LOG 2>>$ERROR
#-- Now copy the matching files from source to target
# rsync -vrultD --min-size=10 --delete-after --exclude=Thumbs.db --exclude=promote/ $source $target 1>>$LOG 2>>$ERROR
rsync -vrultD --min-size=10 --exclude=Thumbs.db --exclude=promote/ --exclude=tid-cgi-bin/ $source $target 1>>$LOG 2>>$ERROR
#-- Set permissions on target
chown -R solarweb:solarweb $target 1>>$LOG 2>>$ERROR
find $target -type d -exec chmod 775 {} \; 1>>$LOG 2>>$ERROR
if [ `echo $line | grep -c cgi` -gt 0 ];then
find $target -type f -exec chmod 775 {} \; 1>>$LOG 2>>$ERROR
else
find $target -type f -exec chmod 664 {} \; 1>>$LOG 2>>$ERROR
fi
find $target -name Thumbs.db -exec rm -f {} \; 1>>$LOG 2>>$ERROR
done
echo "<b>Transferred</b><br>"
echo "<hr>"
unix2dos -q $LOG 1>>$LOG 2>>$ERROR
echo "<pre>"
cat $LOG
echo "<hr>"
echo "<b>Errors<b><br>"
unix2dos -q $ERROR 2>>$ERROR
cat $ERROR
echo "</pre>
echo "<hr>"
rm $ERROR $LOG
echo "</body></html>"