#!/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 "VERSION: $VERSION"
ARCHIVE=LWDAQ_$VERSION.zip
echo "ARCHIVE: $ARCHIVE"

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

# Replace some files with dedicated distribution files.
echo "Exchanging LWDAQ files for distribution..."
mv $LIB_DIR/Toolmaker.tcl $MASTER_DIR/Toolmaker.tcl
mv $LIB_DIR/Toolmaker_Distribute.tcl $LIB_DIR/Toolmaker.tcl

# Make the zip archive. The archive will appear beside the 
# LWDAQ directory.
cd $MASTER_DIR
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 LWDAQ_$VERSION.zip with ditto compression utility..."
	rsync -a \
	  --exclude='.git' \
	  --exclude='.gitignore' \
	  --exclude='Tools/Data/*' \
	  --exclude='LWDAQ.app/Contents/LWDAQ/Configuration/*.tcl' \
	  --exclude='LWDAQ.app/Contents/LWDAQ/Temporary/*.tcl' \
	  "LWDAQ/" Distribution/LWDAQ_$VERSION
	cd Distribution
	ditto -c -k --sequesterRsrc --keepParent LWDAQ_$VERSION ../$ARCHIVE
	cd ..
	rm -rf Distribution
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 LWDAQ_$VERSION.zip with zip compression utility..."
	mv LWDAQ LWDAQ_$VERSION
	zip -rqX $ARCHIVE LWDAQ_$VERSION \
		-x "LWDAQ_$VERSION/.git" \
		"LWDAQ_$VERSION/.gitignore" \
		"LWDAQ_$VERSION/Tools/Data/*" \
		"LWDAQ_$VERSION/LWDAQ.app/Contents/LWDAQ/Configuration/*.tcl" \
		"LWDAQ_$VERSION/LWDAQ_app/Contents/LWDAQ/Temporary/*.tcl"
	mv LWDAQ_$VERSION LWDAQ
fi

# Restore files.
echo "Restoring LWDAQ files with originals..."
mv $LIB_DIR/Toolmaker.tcl $LIB_DIR/Toolmaker_Distribute.tcl
mv $MASTER_DIR/Toolmaker.tcl $LIB_DIR/Toolmaker.tcl

# 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 $ARCHIVE..."
		rm $SITE/LWDAQ_$VERSION.zip
	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