#!/bin/bash

basename=`basename $PWD`
if [ $basename != "package" ]; then
    echo "This script must be ran from within the Packager directory."
    exit 1
fi

version=`date "+%Y-%m-%d"`
include_optionals=false
if [ `uname` = 'Darwin' ]; then
	package_type="osx"
else
	package_type="source"
fi

while getopts ":v:os" Option
do
  case $Option in
    v ) version=$OPTARG
        ;;
    o ) include_optionals=true
        ;;
    s ) package_type="source"
        ;;
  esac
done
shift $(($OPTIND - 1))

revision=`git rev-list HEAD -1 | sed -E 's/([[:alnum:]]{1,10}).*/\1/'`
if [ "`git status -s -uno`" != "" ]; then
    echo "WARNING: The working copy has uncommitted changes which will NOT be included in the package."
fi

if [ $package_type == "source" ]; then
    if [ -d SuperCollider-Source ]; then
        echo "Please remove the ./SuperCollider-Source directory before running this script."
        exit 1
    fi
    returndir=`pwd`
    cd ../
    python package/git-archive-all.py --prefix SuperCollider-Source/ "$returndir/SuperCollider-Source.tmp.tar"

    cd "$returndir"
    # NB we only need one instance of boost, so we exclude one of its two appearances as a submodule in the following
    tar -x --exclude ".gitignore" --exclude ".gitmodules" \
        --exclude "SuperCollider-Source/external_libraries/nova-tt/boost_lockfree" \
        -f SuperCollider-Source.tmp.tar

    if $include_optionals; then
        cp -Rp optional SuperCollider-Source/optional_installs
        cp OPTIONALS_README_SOURCE.txt SuperCollider-Source/optional_installs/README.txt
        filename="SuperCollider-$version-Source-With-Extras.tar.bz2"
        filenamelinux="SuperCollider-$version-Source-With-Extras-linux.tar.bz2"
    else
        filename="SuperCollider-$version-Source.tar.bz2"
        filenamelinux="SuperCollider-$version-Source-linux.tar.bz2"
    fi

    # Here we build a list of (many) files that are useless on linux, so as to build a slimline source.tar.bz2
    find SuperCollider-Source -iname windows -or -iname osx -or -name "*.xcodeproj" -or -name scide_scapp -or -iname "iPhone*" \
		| grep -v "external_libraries/boost/boost/asio/windows" > LinuxExclusions.txt
	echo 'SuperCollider-Source/README_OS_X.md
SuperCollider-Source/README_WINDOWS.txt
SuperCollider-Source/README_IPHONE.txt
SuperCollider-Source/external_libraries/libsndfile
SuperCollider-Source/external_libraries/curl
SuperCollider-Source/external_libraries/icu
SuperCollider-Source/platform/mac
SuperCollider-Source/platform/iphone
SuperCollider-Source/platform/windows
SuperCollider-Source/lang/LangPrimSource/HID_Utilities
SuperCollider-Source/lang/LangPrimSource/HID_Utilities_10_4
SuperCollider-Source/lang/LangPrimSource/WiiMote_OSX
SuperCollider-Source/editors/scapp' >> LinuxExclusions.txt

    tar cfj "$filename" SuperCollider-Source
    tar cfjX "$filenamelinux" LinuxExclusions.txt SuperCollider-Source
    rm -rf SuperCollider-Source SuperCollider-Source.tmp.tar
    exit
else
    if [ -d SuperCollider ]; then
        echo "Please remove the ./SuperCollider directory before running this script."
        exit 1
    fi
    if $include_optionals; then
        opt_options='--copy dmg_with_optionals.ds_store:/.DS_Store --copy optional/:/Optional\ Installs --copy OPTIONALS_README_OSX.rtf:/Optional\ Installs/README.rtf'
        filename="SuperCollider-$version-With-Extras.dmg"
    else
        opt_options='--copy dmg_without_optionals.ds_store:/.DS_Store'
        filename="SuperCollider-$version.dmg"
    fi

    about_version="$version (Revision $revision)"
    echo "About box version string:" $about_version

    mkdir -p SuperCollider/plugins
    returndir=`pwd`
    cd ../platform/mac/build
    git archive $revision | tar -x -C "$returndir/SuperCollider"
    cp -R SuperCollider.app scsynth sclang "$returndir/SuperCollider/"
    cp plugins/* "$returndir/SuperCollider/plugins/"
    cd $returndir
    cd ../
    cp -R ChangeLog COPYING examples Help SCClassLibrary README sounds "$returndir/SuperCollider/"
    cd $returndir
    find SuperCollider/Help/ \( -name "*.htm" -or -name "*.html" \) -exec /Developer/Tools/SetFile -c SCjm {} \;
    defaults write $PWD/SuperCollider/SuperCollider.app/Contents/Info CFBundleVersion -string "$about_version"
    defaults write $PWD/SuperCollider/SuperCollider.app/Contents/Info CFBundleGetInfoString -string "$version"
    plutil -convert xml1 $PWD/SuperCollider/SuperCollider.app/Contents/Info.plist

    # use eval to force any escapes or quotes in $opt_options to be evaluated
    eval './pkg-dmg --verbosity 0 --source ./SuperCollider --target "$filename" --sourcefile --volname SuperCollider --mkdir /.background --copy background.png:/.background/background.png --symlink /Applications:/Applications '$opt_options

    rm -rf ./SuperCollider
fi
