#! /bin/sh
#
# create a phonegap/android project
# 
# USAGE
#   ./create [path package activity]
#
set -e

PROJECT_PATH=${1:-"./example"}
PACKAGE=${2:-"com.phonegap.example"}
ACTIVITY=${3:-"PhoneGapExample"}
PACKAGE_AS_PATH=$(echo $PACKAGE | sed 's/\./\//g')
ACTIVITY_PATH=$PROJECT_PATH/src/$PACKAGE_AS_PATH/$ACTIVITY.java
TARGET=$(android list targets | grep 'id: ' | sed 's/id: \([0-9]\).*/\1/g' | tail -1)
VERSION=$(cat ./VERSION)

# clobber any existing example
if [ $# -eq 0 ]
then
    rm -rf $PROJECT_PATH
fi

# compile phonegap.js and phonegap.jar
cd ./framework && ant jar && cd ../

# create the project
android create project --target $TARGET --path $PROJECT_PATH --package $PACKAGE --activity $ACTIVITY

# copy in www
mkdir -p $PROJECT_PATH/assets && cp -r ./bin/templates/www $PROJECT_PATH/assets/www

# copy in phonegap.js
cp ./framework/assets/www/phonegap-$VERSION.js $PROJECT_PATH/assets/www/phonegap-$VERSION.js

# copy in phonegap.jar
mkdir -p $PROJECT_PATH/libs && cp ./framework/phonegap-$VERSION.jar $PROJECT_PATH/libs/phonegap-$VERSION.jar

# copy in default activity
cat ./bin/templates/Activity.java > $ACTIVITY_PATH

# interpolate the acivity name and package
find "$ACTIVITY_PATH" | xargs grep '__ACTIVITY__' -sl | xargs -L1 sed -i "" "s/__ACTIVITY__/${ACTIVITY}/g"
find "$ACTIVITY_PATH" | xargs grep '__ID__' -sl | xargs -L1 sed -i "" "s/__ID__/${PACKAGE}/g"

# copy in uses-permission elements
sed '$d' < $PROJECT_PATH/AndroidManifest.xml > tmpFile ; mv tmpFile $PROJECT_PATH/AndroidManifest.xml
cat ./bin/templates/uses-permission-elements >> $PROJECT_PATH/AndroidManifest.xml
echo '</manifest>' >> $PROJECT_PATH/AndroidManifest.xml

# copy in plugins.xml
mkdir -p $PROJECT_PATH/res/xml
cp ./bin/templates/plugins.xml $PROJECT_PATH/res/xml/plugins.xml

# leave the id for launching
touch $PROJECT_PATH/package-activity
echo $PACKAGE/$PACKAGE.$ACTIVITY >  $PROJECT_PATH/package-activity