#!/bin/bash

STAGING_DIR=~/AKDBUILD

if [ ! "$1" ]; then
    echo "USAGE:"
    echo "  `basename $0` <releasenumber>"
    echo
    echo "  Builds release tarballs for AppKiDo and AppKiDo-for-iPhone, using"
    echo "  $STAGING_DIR as a staging area."
    echo
    echo "  DO NOT use this for sneakypeeks -- use akdsneakypeek instead."
    echo
    echo "  Make sure <releasenumber> is a nicely formed release number."
    echo "  This script doesn't check for whitespace or any other characters"
    echo "  that are special to the shell."
    echo
    echo "EXAMPLE:"
    echo "  `basename $0` 0.95"
    exit
fi

AKD_VERSION="$1"
RELEASE_TAG="release-$AKD_VERSION"
GIT_REPO=http://github.com/aglee/appkido

CHECKOUT_DIR="$STAGING_DIR/appkido"
XCODE_PROJECT_DIR="$CHECKOUT_DIR/src"
XCODE_RELEASE_BUILD_DIR="$XCODE_PROJECT_DIR/build/Release"

AKD_EXECUTABLE="$XCODE_RELEASE_BUILD_DIR/AppKiDo.app/Contents/MacOS/AppKiDo"
AKD_APP_TARBALL="$STAGING_DIR/AppKiDo-$AKD_VERSION.tgz"

AKD_FOR_IPHONE_EXECUTABLE="$XCODE_RELEASE_BUILD_DIR/AppKiDo-for-iPhone.app/Contents/MacOS/AppKiDo-for-iPhone"
AKD_FOR_IPHONE_APP_TARBALL="$STAGING_DIR/AppKiDo-for-iPhone-$AKD_VERSION.tgz"

echo
echo "===== BUILDING APPKIDO AND APPKIDO-FOR-IPHONE ====="

#---------------------------------------------------------------------------
# Create/clear the staging area
#---------------------------------------------------------------------------

echo
echo "This script DELETES and recreates the directory $STAGING_DIR"
## The "-n 1" allows hitting just y or n without Return.
## REPLY seems to be the default var, don't need to specify it.
##read -p "  Okay to proceed? (y/n)? " -n 1 REPLY
read -p "  Okay to proceed? (y/n)? "
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi

##case "$REPLY" in 
##  y|Y ) echo "yes";;
##  n|N ) echo "no";;
##  * ) echo "invalid";;
##esac

#---------------------------------------------------------------------------
# Create/clear the staging area
#---------------------------------------------------------------------------

echo
echo "Deleting and recreating staging area $STAGING_DIR..."
rm -rf "$STAGING_DIR"
mkdir -p "$STAGING_DIR"
cd "$STAGING_DIR"

#---------------------------------------------------------------------------
# Check out from github
#---------------------------------------------------------------------------

echo
echo "Cloning the repository $GIT_REPO..."
##git clone --depth 1 --branch master $GIT_REPO || exit
git clone "$GIT_REPO" || exit

echo
echo "Checking out the tag $RELEASE_TAG..."
pushd "$CHECKOUT_DIR" ; git checkout "tags/release-$AKD_VERSION" ; popd

#---------------------------------------------------------------------------
# Build
#---------------------------------------------------------------------------

echo
echo "Building the apps..."
cd "$XCODE_PROJECT_DIR"
xcodebuild -target All -configuration Release || exit
##xcodebuild -activetarget -configuration Release || exit

#---------------------------------------------------------------------------
# Strip the binaries, unless we're making a sneakypeek
#---------------------------------------------------------------------------

if [ "$SNEAKYPEEK" == "" ] ; then

	echo
	echo "Stripping the executables"
	echo "  -- size of AppKiDo BEFORE is `stat -n -f "%z" "$AKD_EXECUTABLE"`"
	strip "$AKD_EXECUTABLE"
	echo "  -- size of AppKiDo AFTER  is `stat -n -f "%z" "$AKD_EXECUTABLE"`"
	echo "  -- size of AppKiDo-for-iPhone BEFORE is `stat -n -f "%z" "$AKD_FOR_IPHONE_EXECUTABLE"`"
	strip "$AKD_FOR_IPHONE_EXECUTABLE"
	echo "  -- size of AppKiDo-for-iPhone AFTER  is `stat -n -f "%z" "$AKD_FOR_IPHONE_EXECUTABLE"`"

else

	echo
	echo "This is a sneakypeek, so won't strip the executables"

fi

#---------------------------------------------------------------------------
# Create tarballs of the binaries
#---------------------------------------------------------------------------

echo
echo "Creating tarballs of the application binaries"
tar -C "$XCODE_RELEASE_BUILD_DIR" -czf "$AKD_APP_TARBALL" AppKiDo.app
tar -C "$XCODE_RELEASE_BUILD_DIR" -czf "$AKD_FOR_IPHONE_APP_TARBALL" AppKiDo-for-iPhone.app

#---------------------------------------------------------------------------
# Print sizes
#---------------------------------------------------------------------------

echo
echo "Sizes:"

echo "  -- AppKiDo tarball is $AKD_APP_TARBALL"
##echo "  -- size of app tarball is `stat -n -f "%z" "$AKD_APP_TARBALL"`"
echo "  -- size of AppKiDo.app tarball is `du -h "$AKD_APP_TARBALL" | cut -f 1`"

echo "  -- AppKiDo-for-iPhone tarball is $AKD_FOR_IPHONE_APP_TARBALL"
echo "  -- size of AppKiDo-for-iPhone.app tarball is `du -h "$AKD_FOR_IPHONE_APP_TARBALL" | cut -f 1`"

echo
echo "Done!"
echo

