#!/bin/sh
#
# Script for executing mcrun / mxrun --test across a set of virtual machines
#
# Build-machine requirements:
#
# 1) Common to all client machines:
#    * ssh-keys installed for password-less logins (Linux machine key needs to be installed @ Windows as well)
#    * All required "support software" for installation and succesful execution of mcrun --test (Perl)
#    * For safety, the clients ought to have static IP's
#
# 2) Special to the Windows platform:
#    * BitVise sshd needs to be installed
#    * Firewall has to be opened on port 22 UDP+TCP
#    * Add ssh keys from the Linux and the main build machine
#    * Security needs to be lowered to a minimum, no tests for "third party software" etc, otherwise we will be prompted for passwords etc.
#    * It seems we should NOT have MPICH available on the Windows host as our current "hack" to start smpd causes lots of junk in the logfiles
#
# Next step in the process will be to scp all of the log files to mcc-01 for processing and putting on the web.

# Explicitly set PATH as cron seems not to have a login shell
export PATH=/Users/mccode/bin:/usr/local/bin:$PATH

# Cd to Git area on buildfarm-master
cd /Users/mccode/McCode/

VER=git_`date "+%Y-%m-%d"`
GITREV=`git rev-list -n 1 HEAD`

WINHOST=172.16.97.10
LINHOST=172.16.97.11
OSXHOST=localhost

WINUSER=mcstas
LINUSER=mccode
OSXUSER=mccode

LINPATH=McCode
OSXPATH=McCode

echo Clean, build, install Linux \& Windows
# Build the Linux (deb) and Windows packages at the Linux host
# Step 0 - cleanup
echo Step 0
ssh ${LINUSER}@${LINHOST} cd ${LINPATH} \&\& rm -rf dist 
ssh ${WINUSER}@${WINHOST} del Downloads\\\*.exe
# Step 1 - McStas
echo Step 1
ssh ${LINUSER}@${LINHOST} cd ${LINPATH} \&\& git pull \&\& ./build_debs_mcstas ${VER} \&\& ./build_windows_mcstas ${VER} 
ssh ${LINUSER}@${LINHOST} cd ${LINPATH}/dist \&\& tar cfz mccode-linux-${VER}.tgz \*.deb  \&\& tar cfz mccode-windows-${VER}.tgz \*.exe
echo Step 2
# Step 2 - McXtrace
ssh ${LINUSER}@${LINHOST} cd ${LINPATH} \&\& ./build_debs_mcxtrace ${VER} \&\& ./build_windows_mcxtrace ${VER}
echo Step 3
# Step 3 Install McStas & McXtrace
ssh ${LINUSER}@${LINHOST} cd ${LINPATH}/dist \&\& sudo dpkg -i mcstas-${VER}-linux64.deb mcstas-comps-${VER}-linux64.deb mcstas-tools-perl-${VER}-linux64.deb 
ssh ${LINUSER}@${LINHOST} cd ${LINPATH}/dist \&\& sudo dpkg -i mcxtrace-${VER}-linux64.deb mcxtrace-comps-${VER}-linux64.deb mcxtrace-tools-perl-${VER}-linux64.deb 
echo Step 4
# Step 4 Transfer .exes to the Windows host
scp ${LINUSER}@${LINHOST}:${LINPATH}/dist/\*${VER}\*32.exe ${WINUSER}@${WINHOST}:Downloads/
echo Step 5
# Step 5 install .exes on the Windows host
ssh ${WINUSER}@${WINHOST} .\\Downloads\\mcstas-NSIS-${VER}-mingw32.exe /S
ssh ${WINUSER}@${WINHOST} .\\Downloads\\mcxtrace-NSIS-${VER}-mingw32.exe /S
# Are backgrounded immediately, hence a sleep before next cmds
sleep 20
ssh ${WINUSER}@${WINHOST} .\\Downloads\\mcstas-comps-NSIS-${VER}-mingw32.exe /S
ssh ${WINUSER}@${WINHOST} .\\Downloads\\mcxtrace-comps-NSIS-${VER}-mingw32.exe /S
# Are backgrounded immediately, hence a sleep before next cmds
ssh ${WINUSER}@${WINHOST} .\\Downloads\\mcstas-tools-perl-NSIS-${VER}-mingw32.exe /S
ssh ${WINUSER}@${WINHOST} .\\Downloads\\mcxtrace-tools-perl-NSIS-${VER}-mingw32.exe /S
# Compilation of the various extrac c-libs on windows
ssh ${WINUSER}@${WINHOST} c:\\mcstas-${VER}\\lib\\libs\\libnxs\\compile.bat
ssh ${WINUSER}@${WINHOST} c:\\mcstas-${VER}\\lib\\libs\\mcpl\\compile.bat

echo Clean, build, install Mac
# Build the OSX packages at the Mac host
# Step 0 - cleanup
cd $HOME/${OSXPATH}
rm -rf dist 
# Step 1 - McStas
git pull
./build_macos_Yosemite_mcstas ${VER} 
# Step 2 - McXtrace
./build_macos_Yosemite_mcxtrace ${VER} 
cd dist
# Step 3 Install McStas & McXtrace
sudo /usr/sbin/installer -pkg mcstas-OSXpkg-${VER}*.pkg -target /
sudo /usr/sbin/installer -pkg mcstas-comps-OSXpkg-${VER}*.pkg -target /
sudo /usr/sbin/installer -pkg mcstas-tools-perl-OSXpkg-${VER}*.pkg -target /
sudo /usr/sbin/installer -pkg mcxtrace-OSXpkg-${VER}*.pkg -target /
sudo /usr/sbin/installer -pkg mcxtrace-comps-OSXpkg-${VER}*.pkg -target /
sudo /usr/sbin/installer -pkg mcxtrace-tools-perl-OSXpkg-${VER}*.pkg -target /
tar cfz mccode-OSX-${VER}.tgz *.pkg

# Go to home dir
cd
echo Running the tests - McStas!
echo ...Linux
ssh ${LINUSER}@${LINHOST} mcrun --test  &> mcstas_${VER}_linux.log &
echo ...Mac OS X
mcrun --test  &> mcstas_${VER}_osx.log &
echo ...Windows
ssh ${WINUSER}@${WINHOST} c\:\\\\mcstas-${VER}\\\\bin\\\\mccodetest.bat &> mcstas_${VER}_windows.log &

wait

echo Running the tests - McXtrace!
echo ...Linux
ssh ${LINUSER}@${LINHOST} mxrun --test  &> mcxtrace_${VER}_linux.log &
echo ...Mac OS X
mxrun --test  &> mcxtrace_${VER}_osx.log &
echo ...Windows
ssh ${WINUSER}@${WINHOST} c\:\\\\mcxtrace-${VER}\\\\bin\\\\mccodetest.bat &> mcxtrace_${VER}_windows.log &

wait

/usr/local/bin/dos2unix mcxtrace_${VER}_windows.log
/usr/local/bin/dos2unix mcstas_${VER}_windows.log

wait
# This is where we wait for all the processes to end and copy results to mcc-01...

echo Copying files to the web box...

# Copy back selftest directories
SELFTEST_LIN_MCSTAS=`grep selftest mcstas_${VER}_linux.log |grep Installing | cut -f2 -d\' `
SELFTEST_LIN_MCXTRACE=`grep selftest mcxtrace_${VER}_linux.log |grep Installing | cut -f2 -d\' `
SELFTEST_OSX_MCSTAS=`grep selftest mcstas_${VER}_osx.log |grep Installing | cut -f2 -d\' `
SELFTEST_OSX_MCXTRACE=`grep selftest mcxtrace_${VER}_osx.log |grep Installing | cut -f2 -d\' `
SELFTEST_WIN_MCSTAS=`grep selftest mcstas_${VER}_windows.log |grep Installing | cut -f2 -d\' `
SELFTEST_WIN_MCXTRACE=`grep selftest mcxtrace_${VER}_windows.log |grep Installing | cut -f2 -d\' `
scp -rp ${LINUSER}@${LINHOST}:${SELFTEST_LIN_MCSTAS} ${SELFTEST_LIN_MCSTAS}
scp -rp ${LINUSER}@${LINHOST}:${SELFTEST_LIN_MCXTRACE} ${SELFTEST_LIN_MCXTRACE}
scp -rp ${WINUSER}@${WINHOST}:${SELFTEST_WIN_MCSTAS} ${SELFTEST_WIN_MCSTAS}
scp -rp ${WINUSER}@${WINHOST}:${SELFTEST_WIN_MCXTRACE} ${SELFTEST_WIN_MCXTRACE}
mv $SELFTEST_LIN_MCSTAS mcstas_${VER}_linux
mv $SELFTEST_LIN_MCXTRACE mcxtrace_${VER}_linux
mv $SELFTEST_OSX_MCSTAS mcstas_${VER}_osx
mv $SELFTEST_OSX_MCXTRACE mcxtrace_${VER}_osx
mv $SELFTEST_WIN_MCSTAS mcstas_${VER}_windows
mv $SELFTEST_WIN_MCXTRACE mcxtrace_${VER}_windows
tar cfz mcstas_${VER}_linux.tgz mcstas_${VER}_linux && rm -rf mcstas_${VER}_linux
tar cfz mcxtrace_${VER}_linux.tgz mcxtrace_${VER}_linux && rm -rf mcxtrace_${VER}_linux
tar cfz mcstas_${VER}_osx.tgz mcstas_${VER}_osx && rm -rf mcstas_${VER}_osx
tar cfz mcxtrace_${VER}_osx.tgz mcxtrace_${VER}_osx && rm -rf mcxtrace_${VER}_osx
tar cfz mcstas_${VER}_windows.tgz mcstas_${VER}_windows && rm -rf mcstas_${VER}_windows
tar cfz mcxtrace_${VER}_windows.tgz mcxtrace_${VER}_windows && rm -rf mcxtrace_${VER}_windows

scp *${VER}* mcstas@mcc-01.risoe.dk:Builds/Nightly/Logs
ssh mcstas@mcc-01.risoe.dk mkdir /srv/web/mccode.org/nightly/Current/OSX
ssh mcstas@mcc-01.risoe.dk mkdir /srv/web/mccode.org/nightly/Current/Linux
ssh mcstas@mcc-01.risoe.dk mkdir /srv/web/mccode.org/nightly/Current/Windows
scp ${LINUSER}@${LINHOST}:${LINPATH}/dist/mccode-windows-${VER}.tgz mcstas@mcc-01.risoe.dk:/srv/web/mccode.org/nightly/Current/Windows
scp ${LINUSER}@${LINHOST}:${LINPATH}/dist/mccode-linux-${VER}.tgz  mcstas@mcc-01.risoe.dk:/srv/web/mccode.org/nightly/Current/Linux
scp ${OSXPATH}/dist/mccode-OSX-${VER}.tgz mcstas@mcc-01.risoe.dk:/srv/web/mccode.org/nightly/Current/OSX

echo \<a href="https://github.com/McStasMcXtrace/McCode/commit/${GITREV}" \>${GITREV}\</a\>  > gitrev.txt
scp gitrev.txt mcstas@mcc-01.risoe.dk:Builds/Nightly/Logs
ssh mcstas@mcc-01.risoe.dk touch /usr/local/build/Nightly

ssh mcstas@mcc-01.risoe.dk touch /usr/local/build/Nightly

echo End of script!
