#!/bin/bash
#
#	makePackage - Create installations for AppWeb
#
# 	This script creates several packages
#
#	${BLD_PRODUCT}-{version}-{build}-OS-i386.exe.zip
#
################################################################################
#
#	Copyright (c) Mbedthis Software LLC, 2003-2004. All Rights Reserved.
#	The latest version of this code is available at http://www.mbedthis.com
#
#	This software is open source; you can redistribute it and/or modify it 
#	under the terms of the GNU General Public License as published by the 
#	Free Software Foundation; either version 2 of the License, or (at your 
#	option) any later version.
#
#	This program is distributed WITHOUT ANY WARRANTY; without even the 
#	implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
#	See the GNU General Public License for more details at:
#	http://www.mbedthis.com/downloads/gplLicense.html
#	
#	This General Public License does NOT permit incorporating this software 
#	into proprietary programs. If you are unable to comply with the GPL, a 
#	commercial license for this software and support services are available
#	from Mbedthis Software at http://www.mbedthis.com
#
################################################################################

PACKS=${*-"release binary dev doc samples src"}

. ${BLD_TOP}/config.sh

home=`pwd`
PKG_DIR=${home}/pkgtmp			# Top level distribution / packaging directory
RPM_DIR=${PKG_DIR}/RPM			# RPM packaging area
DEFAULT_PERM=644				# Default permisions
BLD_DATE=`date '+%c'`
BLD_ROOT=`cygpath -au '\\'`

if [ -x ${BLD_TOOLS_DIR}/bldout ]
then BLDOUT=bldout
else BLDOUT=cat
fi

cd ${BLD_TOP} ; ABS_BLD_TOP=`pwd` ; cd -

#
#	Save the as-installed prefix values. We bend them to force cpmod to copy
#	into our local package area. PatchFile needs the original values.
#
INS_BLD_PREFIX=${BLD_PREFIX}
INS_BLD_ROOT_PREFIX=${BLD_ROOT_PREFIX}
INS_BLD_INC_PREFIX=${BLD_INC_PREFIX}
INS_BLD_LIB_PREFIX=${BLD_LIB_PREFIX}
INS_BLD_SBIN_PREFIX=${BLD_SBIN_PREFIX}
INS_BLD_WEB_PREFIX=${BLD_WEB_PREFIX}
INS_BLD_DOC_PREFIX=${BLD_DOC_PREFIX}

################################################################################
#
# Copy and set permissions. Usage: cpmod src dest perm owner.group
#

cpmod() {
	target="${2-$DestD}"
	perm=${3-$DEFAULT_PERM}

	dir="${target%/*}"
	if [ ! -z ${dir} ]
	then
		mkdir -p "${dir}"
		chmod 775 "${dir}"
	fi

	cp -pf "$1" "$target"
	if [ $? != 0 ] 
	then
		echo "# Can't copy: cp -pf $1 $target"
		exit 2
	fi
	if [ -d "$target" ] 
	then
		file=`basename "$1"`
		chmod $perm "$target/$file"
	else
		chmod $perm "$target"
	fi
}

################################################################################
#
#	Copy packages
#

copyPackage() {

	while read f
	do
		f=`eval echo ${f}`
		echo "    #     Installing ${f} ..."
		. ${f}
	done < ${1}
}

################################################################################
#
#	Patch the !!BLD_XXX!! keywords. Usage: patchFile files ...
#
patchFile() {

	#
	#	Restore prefixes while patching. Files require the "as-installed"
	#	values.
	#
	BLD_PREFIX=${INS_BLD_PREFIX}
	BLD_ROOT_PREFIX=${INS_BLD_ROOT_PREFIX}
	BLD_INC_PREFIX=${INS_BLD_INC_PREFIX}
	BLD_LIB_PREFIX=${INS_BLD_LIB_PREFIX}
	BLD_SBIN_PREFIX=${INS_BLD_SBIN_PREFIX}
	BLD_WEB_PREFIX=${INS_BLD_WEB_PREFIX}
	BLD_DOC_PREFIX=${INS_BLD_DOC_PREFIX}

	for f in $*
	do
		cp $f .pbldtmp
		sed -e < .pbldtmp "s/!!BLD_CPU!!/${BLD_CPU}/g; s/!!BLD_OSVER!!/${BLD_OSVER}/g; s/!!BLD_OS!!/${BLD_OS}/g; s/!!BLD_TYPE!!/${BLD_TYPE}/g; s/!!BLD_NUMBER!!/${BLD_NUMBER}/g; s/!!BLD_COMPANY!!/${BLD_COMPANY}/g; s/!!BLD_PRODUCT!!/${BLD_PRODUCT}/g; s/!!BLD_VERSION!!/${BLD_VERSION}/g; s/!!BLD_NAME!!/${BLD_NAME}/g; s/!!BLD_DATE!!/${BLD_DATE}/g; s^!!BLD_PREFIX!!^${BLD_PREFIX}^g; s^!!BLD_DOC_PREFIX!!^${BLD_DOC_PREFIX}^g; s^!!BLD_INC_PREFIX!!^${BLD_INC_PREFIX}^g; s^!!BLD_LIB_PREFIX!!^${BLD_LIB_PREFIX}^g; s^!!BLD_SBIN_PREFIX!!^${BLD_SBIN_PREFIX}^g; s^!!BLD_SRC_PREFIX!!^${BLD_SRC_PREFIX}^g; s^!!BLD_WEB_PREFIX!!^${BLD_WEB_PREFIX}^g;" >$f
		chmod $DEFAULT_PERM ${f}
	done
	rm -f .pbldtmp

	#
	#	Rebend the prefixes
	#
	setupPrefixes
}


################################################################################
#
#	Set the installation prefixes
#

setupPrefixes() {

	#
	#	Update key prefixes
	#
	BLD_PREFIX=${PKG_DIR}/binDist${BLD_PREFIX}
	BLD_DOC_PREFIX=${PKG_DIR}/docDist${BLD_DOC_PREFIX}
	BLD_ROOT_PREFIX=${PKG_DIR}/binDist${BLD_ROOT_PREFIX}
	BLD_INC_PREFIX=${PKG_DIR}/binDist${BLD_INC_PREFIX}
	BLD_LIB_PREFIX=${PKG_DIR}/binDist${BLD_LIB_PREFIX}
	BLD_SBIN_PREFIX=${PKG_DIR}/binDist${BLD_SBIN_PREFIX}
	BLD_WEB_PREFIX=${PKG_DIR}/binDist${BLD_WEB_PREFIX}
}

################################################################################

setup() {

	setupPrefixes

	echo -e "\n    #\n    # Clean the image area ...\n    #"
	echo "    rm -fr ${BLD_TOP}/images/${BLD_PRODUCT}/${BLD_PRODUCT}-*"
	rm -fr ${BLD_TOP}/images/${BLD_PRODUCT}/${BLD_PRODUCT}-*
	rm -fr ${BLD_TOP}/images/${BLD_PRODUCT}/md5-${BLD_PRODUCT}-*

	echo -e "\n    #\n    # Prepare packaging area ...\n    #"
	echo "    rm -fr ${PKG_DIR}"
	rm -fr ${PKG_DIR}
	mkdir -p ${PKG_DIR}
	echo
}

################################################################################

set -e
echo -e "\n    #"
echo "    # Building \"${BLD_NAME} ${BLD_VERSION} Build ${BLD_NUMBER}\""
env | grep BLD_ | sort | sed -e 's/^/    #     /'

setup

#
#	Copy packs
#
for pack in $PACKS
do
	f=${BLD_TOP}/${BLD_PRODUCT}/package/${pack}.pack
	echo -e "    #\n    # Copy package ${f} ..."
	if [ -f ${f} ]
	then
		copyPackage ${f}
	else
		echo "Can't find pack list ${f}"
	fi
done

#
#	Convert text files to dos format
#
find . -name '*.TXT' |
while read f
do
	unix2dos -D ${f} 2>/dev/null
done

####
#
#	Create file lists
#

echo -e "\n    #\n    # Create file lists for self-installs ...\n    #"
cd ${PKG_DIR}/binDist
echo "    #     Creating binary fileList.txt ..."
find . -type f -print >${BLD_PREFIX}/fileList.txt
cd ${home}

cd ${PKG_DIR}/docDist
echo "    #     Creating documentation fileList.txt ..."
find . -type f -print >${BLD_DOC_PREFIX}/fileList.txt
cd ${home}

####
#
# Create Inno setup spec file
#
echo -e "\n    #\n    # Building Windows Installer file lists ...\n    #"
cd ${BLD_PREFIX}
count=0
echo "    #	Creating binary file list"
find * -print | egrep -v "appWeb.iss" | while read f
do
	if [ ! -d $f ]
	then
		name=`basename $f`
		dir=`dirname $f`
		#
		# Get the dirname portion and change all "/" to "\"
		#
		dir=${dir//\//\\}
		flags=""
		if [ "$name" = "README.TXT" -a $count = 0 ]
		then
			#
			#	Now that we display the doc on installation, don't want to also
			#	display the readme.
			#
			#		flags=isreadme
			#
			count=1
		fi
		echo -n "Source: \"${f}\"; DestDir: \"{app}\\${dir}\"; "
		echo "DestName: \"${name}\"; Flags: \"$flags\"; Components: bin"
	fi
done >>appWeb.iss

####
#
# 	Add documentation to the spec file (keep "doc" in the path)
#
cd ${BLD_DOC_PREFIX}
echo "    #	Creating documentation file list"
find * -print | while read f
do
	if [ ! -d "$f" ]
	then
		name=`basename "$f"`
		dir=`dirname "$f"`
		dir=${dir//\//\\}
		flags=""
		echo -n "Source: \"..\\..\\docDist\\${BLD_PRODUCT}\\${f}\"; DestDir: \"{app}\\${dir}\"; "
		echo "DestName: \"${name}\"; Flags: \"$flags\"; Components: doc"
	fi
done >>${BLD_PREFIX}/appWeb.iss

cd ${home}


####
#
# 	Create self-install ZIP images
#
echo -e "\n    #\n    # Create zip image for the binary distribution ...\n    #"
cd ${PKG_DIR}/binDist
BIN_ZIP=${BLD_PRODUCT}-${BLD_VERSION}-${BLD_NUMBER}-${BLD_OS}-${BLD_CPU}
rm -f ${BIN_ZIP}.zip ${BIN_ZIP}.exe
FLIST=${BLD_PRODUCT}/fileList.txt
echo "    wzzip -P ../${BIN_ZIP}.zip @${FLIST} >zip.log"
wzzip -P ../${BIN_ZIP}.zip @${FLIST} >zip.log
if [ $? != 0 ]
then
	echo "wzzip error. Zip log:"
	cat zip.log
	exit 255
fi
rm -f zip.log
cd ${home}

####
#
#	Now a ZIP self-install for the doc distribution
#
cd ${PKG_DIR}/docDist
echo -e "\n    #\n    # Create zip image for the doc distribution ...\n    #"
DOC_ZIP=${BLD_PRODUCT}-doc-${BLD_VERSION}-${BLD_NUMBER}
rm -f ${DOC_ZIP}.zip ${DOC_ZIP}.exe
FLIST=${BLD_PRODUCT}/fileList.txt
echo "    wzzip -P ../${DOC_ZIP}.zip @${FLIST} >zip.log"
wzzip -P ../${DOC_ZIP}.zip @${FLIST} >zip.log
if [ $? != 0 ]
then
	echo "wzzip error. Zip log:"
	cat zip.log
	exit 255
fi
rm -f zip.log
cd ${home}

####
#
# Create an envelope zip file for binary and doc
#
echo -e "\n    #\n    # Create an envelope zip image ...\n    #"
ENV_SELF=${BLD_PRODUCT}-${BLD_VERSION}-${BLD_NUMBER}-${BLD_OS}-${BLD_CPU}.self
echo "    wzzip ${ENV_SELF}.zip ${DOC_ZIP}.zip ${BIN_ZIP}.zip README.TXT >zip.log"
cd ${PKG_DIR}
wzzip ${ENV_SELF}.zip ${DOC_ZIP}.zip ${BIN_ZIP}.zip README.TXT >zip.log
if [ $? != 0 ]
then
	echo "wzzip error. Zip log:"
	cat zip.log
	exit 255
fi
rm -f zip.log

####
#
# Run Inno Setup to create the image file
#
echo -e "\n    #\n    # Creating windows installer package ...\n    #"
cd ${BLD_PREFIX}
BIN_INO=${BLD_PRODUCT}-${BLD_VERSION}-${BLD_NUMBER}-${BLD_OS}-${BLD_CPU}.exe
unix2dos -D appWeb.iss 2>/dev/null
echo "    ${BLD_ROOT}/Program Files/Inno Setup 4/iscc" appWeb.iss
set +e
"${BLD_ROOT}/Program Files/Inno Setup 4/iscc" appWeb.iss >../ino.log
if [ $? != 0 ]
then
	echo "Inno Setup error. Log:"
	cat ../ino.log
	exit 255
fi
rm -f ../ino.log


####
#
#	Zip the exe install and move to the images directory.
#
echo -e "\n    #\n    # Create an envelope zip image for the EXE ...\n    #"
cd ${BLD_PREFIX}
mv Output/setup.exe .
ENV_EXE=${BLD_PRODUCT}-${BLD_VERSION}-${BLD_NUMBER}-${BLD_OS}-${BLD_CPU}.exe
echo "    wzzip ${ENV_EXE}.zip setup.exe README.TXT >zip.log"
wzzip ${ENV_EXE}.zip setup.exe README.TXT >zip.log
if [ $? != 0 ]
then
	echo "wzzip error. Zip log:"
	cat zip.log
	exit 255
fi
rm -f zip.log
cp ${ENV_EXE}.zip ${ABS_BLD_TOP}/images/${BLD_PRODUCT}
cd $home


####
#
#	Checksum the zipped exe and the source zip
#
echo -e "\n    #\n    # Computing checksums ...\n    #"
cd $BLD_TOP/images/${BLD_PRODUCT}
md5sum ${ENV_EXE}.zip >md5-${BLD_PRODUCT}-${BLD_VERSION}-${BLD_NUMBER}-${BLD_OS}-${BLD_CPU}.exe.txt

cd ${home}

echo -e "\n    # Building complete"

rm -fr ${PKG_DIR}

##
##  Local variables:
##  tab-width: 4
##  c-basic-offset: 4
##  End:
##  vim:tw=78
##  vim600: sw=4 ts=4 fdm=marker
##  vim<600: sw=4 ts=4
##
