#!/bin/sh
#*************************************************************************
#
#   $RCSfile: create-bundle,v $
#
#   $Revision: 1.24 $
#
#   last change: $Author: mh $ $Date: 2002/04/23 20:40:23 $
#
#   The Contents of this file are made available subject to the terms of
#   either of the following licenses
#
#          - GNU Lesser General Public License Version 2.1
#          - Sun Industry Standards Source License Version 1.1
#
#   Sun Microsystems Inc., October, 2000
#
#   GNU Lesser General Public License Version 2.1
#   =============================================
#   Copyright 2000 by Sun Microsystems, Inc.
#   901 San Antonio Road, Palo Alto, CA 94303, USA
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License version 2.1, as published by the Free Software Foundation.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#   MA  02111-1307  USA
#
#
#   Sun Industry Standards Source License Version 1.1
#   =================================================
#   The contents of this file are subject to the Sun Industry Standards
#   Source License Version 1.1 (the "License"); You may not use this file
#   except in compliance with the License. You may obtain a copy of the
#   License at http://www.openoffice.org/license.html.
#
#   Software provided under this License is provided on an "AS IS" basis,
#   WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#   WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#   MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#   See the License for the specific provisions governing your rights and
#   obligations concerning the Software.
#
#   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#   Copyright: 2000 by Sun Microsystems, Inc.
#
#   All Rights Reserved.
#
#   Contributor(s): _______________________________________
#
#
#
#*************************************************************************

# Documentation
# -------------
#
# The purpose of this script to take Mac OS X executables and shared libraries
# and package them into the required Mac OS X bundle format.
#
# This script has the following usage:
# 	create-bundle file1 [file2] ... [fileN]
#
# Note that file1 through fileN can in either of the following formats:
# - A file name
# - A file name and a directory to look for missing files. To use this option,
#   use the following format:
#     filename=directory
#
# The file argument is the file that you want to package into a Mac OS X
# bundle. Currently, this script will only package executables and shared
# libraries.
#
# The output for each executable will be a bundle named <file>.app and
# the output for each shared library will be a symlink from libfoo.jnilib 
# back to libfoo.dylib.
# These output directories will be in the same directory as the executable or
# shared library.

# Code
# ----

# Parse command line arguments
if [ $# = 0 ]; then
	printf "create-bundle: error: incorrect number of arguments\n" >&2
	printf "Usage: create-bundle file1 [file2] ... [fileN]\n" >&2
	exit 1
fi

while [ $# != 0 ]; do
	inputfile=`echo "$1" | awk -F= '{print $1}'`
	sourcedir=`echo "$1" | awk -F= '{print $2}'`

	shift

	inputfilename=`basename "$inputfile"`
	outputdir=`dirname "$inputfile"`

	solverlibdir="$SOLARVERSION/$INPATH/lib"
	locallibdir="../../../../lib"

	solverbindir="$SOLARVERSION/$INPATH/bin"
	localbindir="../../.."

	# Determine file type
	filetype=`file -L "$inputfile"`

	# Since this script replaces executable with shell scripts, we may need to
	# search elsewhere if the file is a shell script.
	if printf "$filetype" | grep -q 'Bourne shell script text'; then
		realfile="$sourcedir/Contents/MacOS/$inputfilename"
		if [ -r "$realfile" ]; then
			filetype=`file "$realfile"`
		fi
	fi

	# Create bundle based on file type
	if printf "$filetype" | grep -q 'Mach-O executable ppc'; then
		# If the script has already been packaged into a bundle, recover the
		# packaged executable 
		if [ -s "$realfile" ]; then
			rm -Rf "$inputfile"
			cp -f "$realfile" "$inputfile"
		fi
		# Create bundle directories
		if [ -r "$outputdir/$inputfilename.app" ]; then
			rm -Rf "$outputdir/$inputfilename.app"
		fi
		mkdir -p "$outputdir/$inputfilename.app/Contents/MacOS"
		# Move executable into bundle directory
		mv -f "$inputfile" "$outputdir/$inputfilename.app/Contents/MacOS"
		# Create softlinks to StarOffice shared libraries so that we can run
		# this bundle from the Mac OS X Finder
		targetdir="$outputdir/$inputfilename.app/Contents/MacOS"
		for i in `cd "$targetdir" ; ls -d1 "$solverlibdir/"*.dylib* 2>/dev/null | grep -v -E 'lib\w+static'`
		do
			targetfile="$targetdir"/`basename "$i"`
			if [ ! -L "$targetfile" -a -r "$targetfile" ]; then
				rm -Rf "$targetfile"
			fi
			ln -sf "$i" "$targetfile"
		done
		# Note the hack where we filter out libstatic*.dylib in addition 
		# instead of only lib\w+static files. This is because the local
		# libstatic*.dylib is not getting updated like the libstatic*.dylib in
		# $solverlibdir
		for i in `cd "$targetdir" ; ls -d1 "$locallibdir/"*.dylib* 2>/dev/null | grep -v 'lib.*static'`
		do
			targetfile="$targetdir"/`basename "$i"`
			if [ ! -L "$targetfile" -a -r "$targetfile" ]; then
				rm -Rf "$targetfile"
			fi
			ln -sf "$i" "$targetfile"
		done
		# Create softlinks to any other files in the executable directories
		# so that we can run this bundle from the Mac OS X Finder
		targetdir="$outputdir/$inputfilename.app/Contents/MacOS"
		for i in `cd "$targetdir" ; ls -d1 "$solverbindir/"* 2>/dev/null`
		do
			if [ -d "$i" -o `basename "$i"` = "$inputfilename" ]; then
				continue
			fi
			targetfile="$targetdir"/`basename "$i"`
			if [ ! -L "$targetfile" -a -r "$targetfile" ]; then
				rm -Rf "$targetfile"
			fi
			ln -sf "$i" "$targetfile"
		done
		for i in `cd "$targetdir" ; ls -d1 "$localbindir/"* 2>/dev/null`
		do
			if [ -d "$targetdir/$i" -o "$i" = "$inputfilename" ]; then
				continue
			fi
			targetfile="$targetdir"/`basename "$i"`
			if [ ! -L "$targetfile" -a -r "$targetfile" ]; then
				rm -Rf "$targetfile"
			fi
			ln -sf "$i" "$targetfile"
		done
		# Create stub PkgInfo file
		printf "APPL????" > "$outputdir/$inputfilename.app/Contents/PkgInfo"
		# Replace executable with shell script
		rm -Rf "$outputdir/$inputfilename"
		printf "#!/bin/sh\n" > "$outputdir/$inputfilename"
		printf "exec \`dirname \$0\`/$inputfilename.app/Contents/MacOS/$inputfilename \"\$@\"\n" >> "$outputdir/$inputfilename"
		chmod a+x "$outputdir/$inputfilename"
		printf "create-bundle: $outputdir/$inputfilename.app successfully created\n"
	elif printf "$filetype" | grep -q 'Mach-O dynamically linked shared library ppc'; then
		# Screen out lib\w+static libraries as they are not used directly
		if ! printf "$inputfilename" | grep -q -x -E 'lib\w+static.*\.dylib'; then
			# Create jnilib link
			inputjnilibname="`basename $inputfilename .dylib`.jnilib"
			if [ ! -L "$outputdir/$inputjnilibname" ]; then
				rm -Rf "$outputdir/$inputjnilibname"
			fi
			# Link jnilib
			ln -sf "$inputfilename" "$outputdir/$inputjnilibname"

			printf "create-bundle: $outputdir/$inputjnilibname successfully created\n"
		fi
	else
		printf "create-bundle: error: file is not an executable or shared library.\n" >&2
		exit 1
	fi
done
