#!/bin/bash

# purpose : create bundles for Mac OS X.
# 
# author : Julien Eychenne <jeychenne@free.fr>
# 
# version : 0.2 (January 13, 2005)


### detect mode
if [ $1 == "bind" ]; then
	STANDALONE=1
	BIND=1
	STRIP=0
elif [ $1 == "bind-strip" ]; then
	STANDALONE=1
	BIND=1
	STRIP=1
elif [ $1 == "embed" ]; then
	STANDALONE=1
	BIND=0
	STRIP=0
elif [ $1 == "applet" ]; then
	STANDALONE=0
	BIND=0
	STRIP=0
else
	echo "
*********************************************************
*                                                       *
* USAGE : bundle.sh mode file1 file2...                 *
*                                                       *
* You MUST choose one mode. Available modes are :       *
*                                                       *
* bind : binding a script to the interpreter            *
* appends the script to wxbasic producing a             *
* standalone file.                                      *
* Your source code is not directly readable, but a      *
* motivated user will be able to extract it.            *
*                                                       *
* bind-strip : same as bind, but produces a smaller     *
* file (about 1Mb smaller), without debugging symbols   *
*                                                       *
* embed : embeds the interpreter and produces a         *
* standalone app. While the source code remains         *
* human-readable, embedding is particularly interesting *
* since it allows you to produce standalone apps for    *
* Mac OS X from ANY OS with bash installed (e.g. Linux  *
* on x86).                                              *
*                                                       *
* applet : produces a light-weight mac-style app from   *
* any OS with bash installed (like embed). These        *
* applets require wxbasic.                              *
*                                                       *
*********************************************************
"
fi

if [ $BIND == 1 ] && [ ! -f ./bind.wxb ]; then
	echo "cannot find \"bind.wxb\" in the current directory"
	exit 1
fi


### locate wxbasic
# The script first assumes wxbasic is in the current directory
# (on any platform). If it can't find it, it looks in /Applications.
# It dies if it's still not there.
if [ -d ./wxbasic.app ] && [ -x ./wxbasic.app/Contents/MacOS/wxbasic ]; then
	WXBASIC_APP=./wxbasic.app
elif [ -d /Applications/wxbasic.app ] \
	&& [ -x /Applications/wxbasic.app/Contents/MacOS/wxbasic ]; then 
	WXBASIC_APP=/Application/wxbasic.app
else
	echo "could not find wxbasic"
	exit 1
fi


### set up variables
SCRIPT=$2
BUNDLE=`echo $SCRIPT| sed s/\.wxb$/.app/`
VERSION="1.0" # script version
WXBASIC_BIN=${WXBASIC_APP}/Contents/MacOS/wxbasic
CONTENTS_DIR=${BUNDLE}/Contents
MACOS_DIR=${CONTENTS_DIR}/MacOS
RESOURCES_DIR=${CONTENTS_DIR}/Resources


### create the bundle structure
for DIR in $BUNDLE $CONTENTS_DIR $MACOS_DIR $RESOURCES_DIR; do
	mkdir $DIR
done


### write PkgInfo
PKGINFO_FILE=${CONTENTS_DIR}/PkgInfo
echo "APPL????" > $PKGINFO_FILE


### write Info.plist
PLIST_FILE=${CONTENTS_DIR}/Info.plist

cat > $PLIST_FILE <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleIdentifier</key>
	<string>net.sourceforge.wxbasic</string>
	<key>CFBundleDevelopmentRegion</key>
	<string>English</string>
	<key>CFBundleExecutable</key>
	<string>wxbrun</string>
	<key>CFBundleName</key>
	<string>wxbrun</string>
	<key>CFBundlePackageType</key>
	<string>APPL</string>
	<key>CFBundleSignature</key>
	<string>????</string>
	<key>CFBundleVersion</key>
	<string>%VERSION</string>
	<key>CFBundleShortVersionString</key>
	<string>%VERSION</string>
	<key>CFBundleGetInfoString</key>
	<string>wxbasic, (c) David Cuny</string>
	<key>CFBundleLongVersionString</key>
	<string>2.4.2, (c) 2002 wxWindows</string>
	<key>NSHumanReadableCopyright</key>
	<string>Copyright David Cuny</string>
	<key>LSRequiresCarbon</key>
        <true/>
	<key>CSResourcesFileMapped</key>
	<true/>
</dict>
</plist>
EOF


### include the resource files
for RESOURCE in $2 $3 $4 $5 $6 $7 $8 $9; do
	cp $RESOURCE ${RESOURCES_DIR}/${RESOURCE}
done


### bundle the interpreter
PREFIX="" # whether the embedded interpreter is called or not (default is not)

if [ $STANDALONE == 1 ]; then
	PREFIX="\`pwd\`/wxbasic.app/Contents/MacOS" # used to call wxbasic from wxbrun
	
	echo "copying the interpreter"
	cp -r $WXBASIC_APP ${RESOURCES_DIR}/

	if [ $STRIP == 1 ]; then
		echo "stripping the interpreter"
		strip ${RESOURCES_DIR}/wxbasic.app/Contents/MacOS/wxbasic
	fi
	
	if [ $BIND == 1 ]; then
		# the bound file replaces wxbasic
		echo "binding the script : it may take some time..."
		$WXBASIC_BIN ./bind.wxb ${RESOURCES_DIR}/wxbasic.app/Contents/MacOS/wxbasic $SCRIPT ./wxbasic
		rm ${RESOURCES_DIR}/wxbasic.app/Contents/MacOS/wxbasic
		chmod +x ./wxbasic
		mv ./wxbasic ${RESOURCES_DIR}/wxbasic.app/Contents/MacOS/
		SCRIPT=""
	fi
fi


### write the wxbrun script which is used to run the wxbasic script
WXBRUN_FILE=${MACOS_DIR}/wxbrun

cat > $WXBRUN_FILE <<EOF
#!/bin/bash

SCRIPT=$SCRIPT

CWD=\`dirname \$0\`

cd \$CWD
cd ../Resources

EOF

if [ $STANDALONE == 1 ]; then
	echo "WXBASIC=\`pwd\`/wxbasic.app/Contents/MacOS/wxbasic" >> $WXBRUN_FILE
	echo "exec \$WXBASIC \$SCRIPT \"\$@\"" >> $WXBRUN_FILE
else
	echo "exec wxbasic \$SCRIPT \"\$@\"" >> $WXBRUN_FILE
fi

chmod +x $WXBRUN_FILE


### end
echo
echo "Enjoy ;)"
echo

