#!/bin/sh
icnsfile=etc/macstuff/wxmac.icns
infofile=etc/macstuff/Info.plist
bundlename=GenI
rezcomp="/Developer/Tools/Rez -t APPL Carbon.r $rezfile -o"

#------------------------------------------------------------------------
#  Helper script to create a MacOS X application from a binary.
#  Hacked up with lots of GenI-specific stuff.
#  Meant to be run from the bin directory directly.
#
#  Daan Leijen and Arthur Baars.
#
#  Copyright (c) 2003,2004 Daan Leijen, Arthur Baars
#------------------------------------------------------------------------

# $Id: macosx-app-template,v 1.4 2005/04/29 14:16:51 dleijen Exp $
arg=""

# variables
program=""
verbose="yes"


# Parse command-line arguments
while : ; do
  # put optional argument in the $arg variable
  case "$1" in
   -*=*) arg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
   *)    arg= ;;
  esac

  # match on the arguments
  case "$1" in
    "") break;;
    -\?|--help)
        echo "usage:"
        echo "  macosx-app [options] <program (a.out)>"
        echo ""
        echo "options: [defaults in brackets]"
        echo "  --help | -?         show this information"
	echo "  --verbose | -v      be verbose"
        echo ""
        exit 1;;
    -v|--verbose)
        verbose="yes";;
    -*) echo "error: Unknown option \"$1\". Use \"--help\" to show valid options." 1>&2
        echo "" 1>&2
        exit 2;;
    *)  if test "$program"; then
         echo "error: [program] is specified twice. Use \"--help\" to show valid options." 1>&2
	 echo ""1>&2
	 exit 2
	fi
	program="$1";;
  esac
  shift
done

# default program
if test -z "$program"; then
  echo "error: you need to specify a program. Use \"--help\" to show valid options." 1>&2
  echo "" 1>&2
  exit 2
fi

# show when verbose is true.
show()
{
  if test "$verbose" = "yes"; then 
    echo "$1"
  fi
}

# link with default resources
# this is neccesary only to run the GUI from the command line 
if test "$rezcomp"; then
 show "creating resource:" 
 show " > $rezcomp $program"
 $rezcomp $program
fi

# create a bundle
bundle="$program.app/Contents"

# create bundle directories
show "creating app directories:"
show " - $program.app"
mkdir -p $program.app
show " - $bundle"
mkdir -p $bundle
show " - $bundle/MacOS"
mkdir -p $bundle/MacOS
show " - $bundle/Resources"
mkdir -p $bundle/Resources

cp -f $program $bundle/MacOS/

# copy the icon 
cp -f ${icnsfile} $bundle/Resources

# package info
show "creating package info:"
show " - $bundle/PkgInfo"
echo -n "APPL????" > $bundle/PkgInfo

# create program information file
cp ${infofile} $bundle/Info.plist 

# tell finder that there's an icon 
/Developer/Tools/SetFile -a C $bundle

show "done."
show ""
