#!/usr/bin/env bash

# Based on https://gist.githubusercontent.com/oubiwann/453744744da1141ccc542ff75b47e0cf/raw/a28ece126d1ff7ec0bf851acce15175e4deceb7d/appify.sh

set -eu
set -o pipefail

version=5.0
script=$(basename "$0")
appname=''
appicons="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns"
appscript=""


function usage {
    retval="$1"
    case "$retval" in
        0)
            ;;
        *)
            exec 1>&2
            ;;
    esac
    cat << EOF
$script v${version} for for Mac OS X
This version available at: https://stromberg.dnsalias.org/~strombrg/mactools/
Originally from: https://gist.github.com/oubiwann/453744744da1141ccc542ff75b47e0cf

Usage:

  $script [options]

Options:

  -h, --help         Prints this help message, then exits
  --script           Name of the script to 'appify' (required)
  --name             Name of the application (required)
  --icons            Name of the icons file to use when creating the app
                         (defaults to $appicons)
  --version          Prints the version of this script, then exits

Description:

  Creates the simplest possible Mac app from a shell script.

  Appify has one required parameter, the script to appify:

    $script --script my-app-script.sh

  Note that you cannot rename appified apps. If you want to give your app
  a custom name, use the '--name' option

    $script --script my-app-script.sh --name "Sweet"

Copyright:

  Copyright (c) Thomas Aylott <http://subtlegradient.com/>
  Modified by Mathias Bynens <http://mathiasbynens.be/>
  Modified by Andrew Dvorak <http://OhReally.net/>
  Rewritten by Duncan McGreggor <http://github.com/oubiwann/>
  Modified and adopted by Dan Stromberg <https://stromberg.dnsalias.org/~strombrg/mactools/>

EOF
    exit "$retval"
}

function version {
    echo "v${version}"
    exit 0
}

while [ "$#" -ge 1 ]
do
    case "$1" in
        -h|--help)
            usage 0
            ;;
        --script)
            appscript="$2"
            shift
            ;;
        --name)
            appname="$2"
            shift
            ;;
        --icons)
            appicons="$2"
            shift
            ;;
        --version)
            version
            ;;
        *)
            echo "$0: unrecognized option: $1" 1>&2
            usage 1
            ;;
    esac
    shift
done

all_good=True

if [ "$appscript" = "" ]
then
    echo "$0: --script /where/ever is a required option" 1>&2
    all_good=False
elif [ ! -f "$appscript" ]
then
    echo "Can't find the script '$appscript'"
    all_good=False
fi

if [ "$appname" = "" ]
then
    echo "$0: --name is a required option" 1>&2
    all_good=False
fi

if [ -d "$appname.app" ]
then
    echo "The bundle '$appname.app' already exists"
    all_good=False
fi

if ! [ -f "$appicons" ]
then
    echo "The icons file $appicons does not exist"
    all_good=False
fi

case "$all_good" in
    True)
        ;;
    False)
        echo "$0: one or more preflight checks failed.  Exiting without creating an app bundle." 1>&2
        exit 1
        ;;
    *)
        echo "$0: Internal Error: \$all_good had a strange value: $all_good" 1>&2
        exit 1
        ;;
esac

appdir="$appname.app/Contents"

mkdir -p "$appdir"/{MacOS,Resources}
cp "$appicons" "$appdir/Resources/$appname.icns"
cp "$appscript" "$appdir/MacOS/$appname"
chmod +x "$appdir/MacOS/$appname"

cat << EOF > "$appdir/Info.plist"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//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>
    <key>CFBundleSignature</key>
    <string>4242</string>
  </dict>
</plist>
EOF

echo "Things appear to have finished happy.  Please move or copy $appname.app to /Applications now."