#!/bin/bash

if [ "$1" = "-h" -o "$1" = "--help" -o -z "$1" ]; then cat <<EOF
appify v3.0.1 for Mac OS X - http://mths.be/appify
Creates the simplest possible Mac app from a shell script.

Appify takes a shell script as its first argument:

    `basename "$0"` my-script.sh

Note that you cannot rename appified apps. If you want to give your app
a custom name, use the second argument:

    `basename "$0"` my-script.sh "My App"

Copyright (c) Thomas Aylott <http://subtlegradient.com/>
Modified by Mathias Bynens <http://mathiasbynens.be/>
EOF
exit; fi

APPNAME=${2:-$(basename "$1" ".sh")}
DIR="$APPNAME.app/Contents/MacOS"

if [ -a "$APPNAME.app" ]; then
	echo "$PWD/$APPNAME.app already exists :("
	exit 1
fi

mkdir -p "$DIR"
cp "$1" "$DIR/$APPNAME"
chmod +x "$DIR/$APPNAME"

mkdir -p "$APPNAME.app/Contents/Resources"

cp "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns" "$APPNAME.app/Contents/Resources/$APPNAME.icns"

cat <<EOF > "$APPNAME.app/Contents/Info.plist"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>CFBundleExecutable</key>
  <string>$APPNAME</string>
  <key>CFBundleGetInfoString</key>
  <string>$APPNAME</string>
  <key>CFBundleIconFile</key>
  <string>$APPNAME</string>
  <key>CFBundleName</key>
  <string>$APPNAME</string>
  <key>CFBundlePackageType</key>
  <string>APPL</string>
</dict>
</plist>
EOF

tree "$PWD/$APPNAME.app"
