#!/bin/bash

# temp dir for packaging
PKGTMP=${TMP}/package
ZIP=$1

rm -rf $PKGTMP
mkdir -p $PKGTMP
chmod 755 $PKGTMP

echo -n "Copying app files to temp directory..."
rsync \
	-aL \
	./ $PKGTMP 2>&1 \
	--exclude='.git' \
	--exclude='Makefile' \
	--exclude='vars.mk' \
	--exclude='release/'
echo "done."

EXTDIR=`pwd -P`
pushd $PKGTMP/> /dev/null

echo "Packaging app."

echo "Copying live config over local."
mv config.live.js config.js
echo "Rename project from Turtl-dev to Turtl."
sed -i 's/"name": "Turtl-dev"/"name": "Turtl"/g' package.json

echo -n "Zipping..."
zip -qr -9 -X \
	turtl.zip \
	. \
	-x *.git* \
	-x release/* \
	-x node_modules
echo "done."

mv turtl.zip $EXTDIR/$ZIP || {
	echo "Problem moving $PKGTMP/turtl.zip to $EXTDIR/$ZIP. Exiting."
	exit 1
}
popd > /dev/null

echo "Clearing tmp dir ($PKGTMP/)"
rm -rf $PKGTMP

echo "Complete. Created fully-packaged app: $ZIP"
