#! /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/iOS project
# 
# USAGE
#   ./create <path_to_new_project> <package_name> <project_name>
#
# EXAMPLE
#  ./create ~/Desktop/radness org.apache.cordova.radness Radness
#
set -e

function usage() {
  echo "Usage: $0 <path_to_new_project> <package_name> <project_name>"
  echo "	<path_to_new_project>: Path to your new Cordova iOS project"
  echo "	<package_name>: Package name, following reverse-domain style convention"
  echo "	<project_name>: Project name"
  exit 1
}

# check whether it is a proper create command (at least 3 arguments)
if [ $# -lt 3 ]; then
	usage
fi

BINDIR=$( cd "$( dirname "$0" )" && pwd )
PROJECT_PATH=$1
PACKAGE=$2
PROJECT_NAME=$3

# check whether the project path exists and is not empty
if [ -d "$PROJECT_PATH" ]; then
	if [ "$(ls -1A "$PROJECT_PATH")" ]; then
		echo "\033[31mError: $PROJECT_PATH is not empty. Please specify an empty folder.\033[m"
		exit 1
	fi
fi

# copy the files in; then modify them
cp -r $BINDIR/templates/project/ $PROJECT_PATH

# I've tried to be thorough in my documentation of the manual actions below...
# on first brush it would seem that the right solution would be to brute force
# recurse the directory tree renaming instances of __ID__ and __TESTING__ ...however this is a little blunt
# and while so the below is too being very manual it was/is easier to test, fucking manually, and sort out the automation

# rename the folders/files:
#
# - ./__TESTING__.xcodeproj/
# - ./__TESTING__/
# - ./__TESTING__/__TESTING__-info.plist
# - ./__TESTING__/__TESTING__-Prefix.plist
mv $PROJECT_PATH/__TESTING__.xcodeproj $PROJECT_PATH/$PROJECT_NAME.xcodeproj
mv $PROJECT_PATH/__TESTING__ $PROJECT_PATH/$PROJECT_NAME
mv $PROJECT_PATH/$PROJECT_NAME/__TESTING__-Info.plist $PROJECT_PATH/$PROJECT_NAME/$PROJECT_NAME-Info.plist
mv $PROJECT_PATH/$PROJECT_NAME/__TESTING__-Prefix.pch $PROJECT_PATH/$PROJECT_NAME/$PROJECT_NAME-Prefix.pch

# lets store a reference to the root
R=$PROJECT_PATH/$PROJECT_NAME

# replace __TESTING__ and --ID-- with ACTIVITY and ID strings, respectively, in:
#
# - ./__TESTING__.xcodeproj/project.pbxproj
# - ./__TESTING__/Classes/AppDelegate.h
# - ./__TESTING__/Classes/AppDelegate.m
# - ./__TESTING__/Resources/main.m
# - ./__TESTING__/Resources/__TESTING__-info.plist
# - ./__TESTING__/Resources/__TESTING__-Prefix.plist

$BINDIR/replaces $R.xcodeproj/project.pbxproj __TESTING__ $PROJECT_NAME
$BINDIR/replaces $R/Classes/AppDelegate.h     __TESTING__ $PROJECT_NAME
$BINDIR/replaces $R/Classes/AppDelegate.m     __TESTING__ $PROJECT_NAME
$BINDIR/replaces $R/Classes/MainViewController.h     __TESTING__ $PROJECT_NAME
$BINDIR/replaces $R/Classes/MainViewController.m     __TESTING__ $PROJECT_NAME
$BINDIR/replaces $R/main.m                    __TESTING__ $PROJECT_NAME
$BINDIR/replaces $R/$PROJECT_NAME-Info.plist      __TESTING__ $PROJECT_NAME
$BINDIR/replaces $R/$PROJECT_NAME-Prefix.pch      __TESTING__ $PROJECT_NAME

$BINDIR/replaces $R/$PROJECT_NAME-Info.plist --ID-- $PACKAGE