#!/bin/bash
###----------------------------------------------------###
### If there is already a websys.txt then remove it ###
###----------------------------------------------------###
if [ -e /nusdata/staff/NUS/NUS/Systems/websys.txt ]
then
echo doing some cleanup
rm /nusdata/staff/NUS/NUS/Systems/websys.txt
fi
###----------------------------------------------------###
### Change directory so that this script can be placed ###
### anywhere on the computer and it will still work ###
###----------------------------------------------------###
cd /nusdata/staff/NUS/NUS/Systems/
###----------------------------------------------------###
### This will output the names of the directories for ###
### each system. We will use this to create a variable ###
### in an array for each system directory. The output ###
### will be put in kiskasys.txt to be used later ###
###----------------------------------------------------###
echo writing directory names to kiskasys.txt
ls -F | grep '/$' | grep -v "^[^0-9]" | grep -v '^100' | sed -e "s/\///" | sed -e s/^/\'/ | sed -e s/$/\'/ | cat > kiskasys.txt
###----------------------------------------------------###
### This will do the same as before only create a list ###
### of the system directories on the web server. The ###
### output will be written to websys.txt ###
###----------------------------------------------------###
ssh -p 50014 nusak@webserver 'echo I am now $USER at $HOSTNAME' #Un-comment this line to test if the SSH is working
ssh -p 50014 nusak@webserver 'cd /home/www/html/systems/; ls -F | grep '/$' | grep -v "^[^0-9]" | sed -e "s/\///" | cat > websys.txt'
echo just wrote directory names to websys.txt
###----------------------------------------------------###
### Use scp to copy the file we just created on remote ###
### system to the Systems directory ###
###----------------------------------------------------###
scp -P 50014 nusak@webserver:/home/www/html/systems/websys.txt /nusdata/staff/NUS/NUS/Systems/websys.txt
echo Just coppied websys.txt from webserver to $HOSTNAME
###----------------------------------------------------###
### Create two arrays, one that holds the directory ###
### names on the kiska server and one that holds the ###
### directory names on the web server. The variables ###
### are kiskasys and websys respectively ###
###----------------------------------------------------###
old_IFS=$IFS
IFS=$'\n'
websys=($(cat /nusdata/staff/NUS/NUS/Systems/websys.txt)) #array
IFS=$old_IFS
old_IFS=$IFS
IFS=$'\n'
kiskasys=($(cat /nusdata/staff/NUS/NUS/Systems/kiskasys.txt)) #array
IFS=$old_IFS
###----------------------------------------------------###
### Use if statement to check to make sure that the ###
### two directory lists have the same number of ###
### entries. If they do not it returns an error ###
### instead of continuing. ###
###----------------------------------------------------###
if [ "${#websys[@]}" != "${#kiskasys[@]}" ]
then
echo ERROR!! kiskasys.txt and websys.txt do not have an equal number of entries
else
###----------------------------------------------------###
### For every line in the websys.txt, this will rsync ###
### from the directory in line X of the kiskasys.txt ###
### to the directory in line X of websys.txt ###
###----------------------------------------------------###
for ((i=0; i<${#websys[@]}; i++))
do
localpath="/nusdata/staff/NUS/NUS/Systems/"${kiskasys[$i]}"/"
remotepath="/home/www/html/systems/${websys[$i]}"
rsync -avz -s -e "$localpath" "ssh -p 50014" "nusak@webserver:$remotepath/"
done
fi
exit