#! /bin/sh
#       Licensed to the Apache Software Foundation (ASF) under one
#       or more contributor license agreements.  See the NOTICE file
#       distributed with this work for additional information
#       regarding copyright ownership.  The ASF licenses this file
#       to you under the Apache License, Version 2.0 (the
#       "License"); you may not use this file except in compliance
#       with the License.  You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#       Unless required by applicable law or agreed to in writing,
#       software distributed under the License is distributed on an
#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#       KIND, either express or implied.  See the License for the
#       specific language governing permissions and limitations
#       under the License.
#
#
# create a cordova/android project
# 
# USAGE
#   ./create [path package activity]
#

# load up the config
. ./.cordova/config

PACKAGE_AS_PATH=$(echo $PACKAGE | sed 's/\./\//g')
ACTIVITY_PATH=./src/$PACKAGE_AS_PATH/$ACTIVITY.java
MANIFEST_PATH=./AndroidManifest.xml

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

# copy all the cordova scripts etc in there
cp -R ./cordova/templates/project/* .

# copy in cordova.js
cp ./.cordova/android/cordova-$VERSION.js ./assets/www

# copy in cordova.jar
cp ./.cordova/android/cordova-$VERSION.jar ./libs

# copy in res/xml
mkdir ./res/xml
cp ./.cordova/android/cordova.xml ./res/xml
cp ./.cordova/android/plugins.xml ./res/xml

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

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

find "$MANIFEST_PATH" | xargs grep '__ACTIVITY__' -sl | xargs -L1 sed -i -e "s/__ACTIVITY__/${ACTIVITY}/g"
find "$MANIFEST_PATH" | xargs grep '__PACKAGE__' -sl | xargs -L1 sed -i -e "s/__PACKAGE__/${PACKAGE}/g"
