#!/bin/bash

# This shell script creates the LWDAQ.zip archive in which we distribute 
# the LWDAQ software.

# Mount point for remote disk.
RMT="/Users/kevan/OSI-AWS.localized"

# Determine operating system. Default is Unknown. 
OS=Unknown
if [ "`uname -a | grep -i Darwin`" != "" ]
then 
	OS=MacOS
fi
if [ "`uname -a | grep -i Windows`" != "" ]
then 
	OS=Windows
fi
if [ "`uname -a | grep -i Linux`" != "" ]
then 
	OS=Linux
fi

# Get rid of platform-specific meta-data files.
if [ "$OS" == "MacOS" ] 
then
	find .. -name ".DS_Store" -print | xargs -n1 rm
fi

# We assume this archive script is in the LWDAQ/Build directory. We start
# by obtaining a relative path to the Build directory, then we obtain a 
# global path.
BUILD_DIR=`dirname $0`
cd $BUILD_DIR
BUILD_DIR=`pwd`
echo "BUILD_DIR: $BUILD_DIR"

# Look for the LWDAQ directory structure. If we don't find it, we exit.
if [ -d "../LWDAQ.app" ]
then
	echo "FOUND: LWDAQ directory structure."
else
	echo "ERROR: No LWDAQ directory structure around script file."
	exit
fi

# Define directories.
cd ..
LWDAQ_DIR=`pwd`
echo "LWDAQ_DIR: $LWDAQ_DIR"
cd ..
MASTER_DIR=`pwd`
echo "MASTER_DIR: $MASTER_DIR"
LIB_DIR="$LWDAQ_DIR/LWDAQ.app/Contents/LWDAQ"
echo "LIB_DIR: $LIB_DIR"
GIT_DIR="$LWDAQ_DIR/.git"
echo "GIT_DIR: $GIT_DIR"

# Check that the remote directory exists.
if [ -d $RMT ] 
then
	echo "FOUND: Remote directory $RMT"
else
	echo "WARNING: Cannot find remote directory $RMT"
fi

# Determine software version number.
VERSION=`grep -o "\"[0-9]\+\.[0-9]\+\.[0-9]\+\"" $LIB_DIR/Init.tcl | grep -o "[^\"]\+" `
if [ -z "$VERSION" ] 
then
	echo "ERROR: could not find version number in Init.tcl."
	exit
fi
echo "Software Version: $VERSION"

# Return to build directory and clean.
cd $BUILD_DIR
make clean

# Prepare to make archive.
echo "Preparing LWDAQ files for archive..."
rm $LWDAQ_DIR/Tools/Data/*.*
rm $LIB_DIR/Configuration/*.tcl
rm $LIB_DIR/Temporary/*.tcl
mv $LIB_DIR/Toolmaker.tcl $MASTER_DIR/Toolmaker.tcl
mv $LIB_DIR/Toolmaker_Distribute.tcl $LIB_DIR/Toolmaker.tcl

# We move the .git library out of the LWDAQ 
# package because it is large.
mv $GIT_DIR $MASTER_DIR/GIT

# Move to master directory and define archive name.
cd $MASTER_DIR
ARCHIVE=LWDAQ_$VERSION.zip

# Make the zip archive. The archive will appear beside the 
# LWDAQ directory.
if [ $OS == "MacOS" ]
then
# On MacOS we use the ditto command on MacOS because it 
# preserve MacOS resource information, so that the LWDAQ
# program icon will be intact when we unzip on another MacOS
# machine.
	echo "Creating $ARCHIVE with MacOS compression utility..."
	ditto -c -k --sequesterRsrc --keepParent `basename $LWDAQ_DIR` $ARCHIVE
else
# We use the zip command on other platforms. It does not 
# preserve the MacOS icon. We try to remove resource information
# to make a more generic archive with the X option.
	echo "Creating $ARCHIVE with zip compression utility..."
	zip -rqX $ARCHIVE `basename $LWDAQ_DIR`
fi

# Restore LWDAQ files to former places.
echo "Restoring LWDAQ files."
mv $LIB_DIR/Toolmaker.tcl $LIB_DIR/Toolmaker_Distribute.tcl
mv $MASTER_DIR/Toolmaker.tcl $LIB_DIR/Toolmaker.tcl
mv $MASTER_DIR/GIT $GIT_DIR

# Copy documents to website.
DOCS="../Active/OSI/Software/LWDAQ"
if [ -d "$DOCS" ]
then 
	echo "FOUND: Local document folder."
	RMTDOCS="$RMT/Software/LWDAQ"
	if [ -d "$RMTDOCS" ] 
	then
		echo "FOUND: Remote document folder."
		echo "Deleting documents in remote folder..."
		rm $RMTDOCS/Manual.html
		rm $RMTDOCS/Commands.html
		echo "Uploading Manual.html..."
		cp $DOCS/Manual.html $RMTDOCS/Manual.html
		echo "Generating new Commands.html"
		GENERATOR_SCRIPT="$LWDAQ_DIR/LWDAQ.app/Contents/LWDAQ/Configuration/Gen.tcl"
		echo "LWDAQ_command_reference; exit" | cat > $GENERATOR_SCRIPT
		$LWDAQ_DIR/lwdaq --no-gui
		rm $GENERATOR_SCRIPT
		echo "Uploading Commands.html..."
		cp $LWDAQ_DIR/Commands.html $RMTDOCS/Commands.html
		mv $LWDAQ_DIR/Commands.html $DOCS/Commands.html
	else
		echo "NOT FOUND: Remote document folder."
	fi
else
	echo "NOT FOUND: Local document folder."
fi

# Copy new distribution archive to the remote repository.
SITE="$RMT/Software/Download"
if [ -d "$SITE" ]
then
	echo "FOUND: Remote software download directory."
	if [ -d "$SITE/$ARCHIVE" ] 
	then
		echo "Deleting pre-existing $SITE/$ARCHIVE..."
		rm $SITE/$ARCHIVE
	fi
	echo "Uploading new $ARCHIVE..."
	cp $ARCHIVE $SITE/$ARCHIVE
	echo "Deleting local copy of archive..."
	rm $ARCHIVE
else
	echo "WARNING: Cannot find remote download directory."
	echo "Keeping local copy of $ARCHIVE."
fi

# Finish.
echo "Done."
exit