#!/bin/bash

PACK_NAME=${1%%/}
if [ -z $PACK_NAME ]; then
        echo "Usage $0 PACK_NAME [FTP_UPLOAD]"
        exit 1
fi

TMPD=`mktemp -d`
PACK_CONF=$TMPD/pack-config
#shared config between pack* scripts
rm -rf $PACK_CONF 
mkdir $PACK_CONF

#CONFIGURE - common settings
PACK_MOUNT_DIR=$TMPD/mnt
mkdir -p $PACK_MOUNT_DIR

DESTINATION_PATH=/opt/imaxa/package_source/GAMES_v7/Packs
echo $PACK_MOUNT_DIR > $PACK_CONF/mount.point
echo $DESTINATION_PATH > $PACK_CONF/out.dir 
echo 0 > $PACK_CONF/create.folder.in.package 
echo 0 > $PACK_CONF/encrypt.pack
echo 0 > $PACK_CONF/wipe.device

#REBUILD INNER PACKAGE?
REBUILD_INNER_PACKS=1


./pack-umount.sh $PACK_MOUNT_DIR  > /dev/null 2>&1
#mkdir $PACK_MOUNT_DIR #if mount poind does not exist, script will create a temporary mount point!

	echo
        echo -e "\e[44m \e[30m ------- Building OUTER $PACK_NAME ------- \e[0m" #Violet Background

	#CONFIGURE - per package settings
	echo $PACK_NAME > $PACK_CONF/name
	echo \""$PACK_NAME"/*\" > $PACK_CONF/source.paths

	if [ $REBUILD_INNER_PACKS -eq 0 ]; then
		echo ""> $PACK_CONF/pre.copy.command
	else
		echo "cd ../Inner; ./pack-make-inner-single.sh $PACK_NAME;"> $PACK_CONF/pre.copy.command
	fi


	if [ -f $PACK_NAME/additional.pack.size.kb ]; then
		echo "Got additional.pack.size.kb, will increment pack size for "`cat $PACK_CONF/additional.pack.size.kb`" KB."
        	cat $PACK_NAME/additional.pack.size.kb > $PACK_CONF/additional.pack.size.kb
	else
	        echo "WARNING: $PACK_NAME/additional.pack.size.kb not found!!! will use default value 0kB."
        	echo 0 > $PACK_CONF/additional.pack.size.kb
	fi
	if [ -f $PACK_NAME/reduced.pack.size.kb ]; then
        	cat $PACK_NAME/reduced.pack.size.kb > $PACK_CONF/reduced.pack.size.kb
		echo "Got reduced.pack.size.kb, will decrement pack size for "`cat $PACK_CONF/reduced.pack.size.kb`" KB."
	else
        	echo "WARNING: $PACK_NAME/reduced.pack.size.kb not found!!! will use default value 0kB."
	        echo 0 > $PACK_CONF/reduced.pack.size.kb
	fi

	if [ -f $PACK_NAME/encrypt.files ]; then
		cat $PACK_NAME/encrypt.files > $PACK_CONF/encrypt.files
		echo "Got encrypt.files, will encrypt the files in that list"
	fi

	#INVOKE MAKE
	echo "Starting pack-make for: "$PACK_NAME
	echo
        ./pack-make.sh $PACK_CONF
	RESULT=$?

	#remove inner pack from folder
	if [ $REBUILD_INNER_PACKS -gt 0 ]; then
		rm -f $PACK_NAME/*.cpack 
	fi

#exit
if [ $RESULT -ne 0 ]; then
        echo -e "\e[41m \e[30m ------- OUTER $PACK_NAME FAILED ------- \e[0m" #Yellow Background
	echo

        #clean
	rm -rf $TMPD
    exit $RESULT
else

        echo -e "\e[44m \e[30m ------- OUTER $PACK_NAME COMPLETED ------- \e[0m" #Yellow Background
	echo

        #clean
	rm -rf $TMPD

	if [[ $@ == *"FTP_UPLOAD"* || $@ == *"FTP_SYNC_UPLOAD"* || $@ == *"PUBLISH"* ]]; then
		cp -v $DESTINATION_PATH"/"$PACK_NAME".pack" "/mnt/liar/FTP_HOME/charrcom/FTP.roulette.install/PACKAGES_IGP/DEFAULT/V7.0/Games"
		md5sum "/mnt/liar/FTP_HOME/charrcom/FTP.roulette.install/PACKAGES_IGP/DEFAULT/V7.0/Games/"$PACK_NAME".pack" > "/mnt/liar/FTP_HOME/charrcom/FTP.roulette.install/PACKAGES_IGP/DEFAULT/V7.0/Games/"$PACK_NAME".pack.md5"
		rm $DESTINATION_PATH"/"$PACK_NAME".pack"
		echo "moved to ftp packages, md5 added"
	else
		echo "move to ftp packages was not requested."
	fi
    exit 0
fi

