#!/bin/sh

APPNAME=$1
APP_DESC=$2

if [ -z "$APPNAME" -o -z "$APP_DESC" ]
then
	echo "Usage:";
	echo "./initapp <CamelCaseAppName> <app_desc>"
	echo "Eg:";
	echo "./initapp MyNewApp \"This is my new app\""
	exit 1;
fi

APPNAME_LC=""
APPNAME_UC=""
HEADERS_GUID=""
SOURCES_GUID=""
PROJECT_GUID=""

initVars()
{
	APPNAME_LC=`echo "$APPNAME" |tr "[:upper:]" "[:lower:]"`;
	APPNAME_UC=`echo "$APPNAME" |tr "[:lower:]" "[:upper:]"`;
	HEADERS_GUID=`uuidgen`;
	SOURCES_GUID=`uuidgen`;
	PROJECT_GUID=`uuidgen`;
}

doStrReplacements()
{
	echo `echo $1|sed "s/#PROJECT_GUID#/$PROJECT_GUID/g"|sed "s/#SOURCES_GUID#/$SOURCES_GUID/g"|sed "s/#HEADERS_GUID#/$HEADERS_GUID/g"|sed "s/#APPNAME#/$APPNAME/g"|sed "s/#APPNAME_LC#/$APPNAME_LC/g"|sed "s/#APPNAME_UC#/$APPNAME_UC/g"|sed "s/#APP_DESC#/$APP_DESC/g"`;
}

doFileReplacements()
{
	cat $1|sed "s/#PROJECT_GUID#/$PROJECT_GUID/g"|sed "s/#SOURCES_GUID#/$SOURCES_GUID/g"|sed "s/#HEADERS_GUID#/$HEADERS_GUID/g"|sed "s/#APPNAME#/$APPNAME/g"|sed "s/#APPNAME_LC#/$APPNAME_LC/g"|sed "s/#APPNAME_UC#/$APPNAME_UC/g"|sed "s/#APP_DESC#/$APP_DESC/g">$2;
}

doWildcardReplacements()
{
	SRCDIR=$1
	WILDCARD=$2
	DSTDIR=$3

	mkdir -p $(doStrReplacements $DSTDIR)
	
	for fileName in `ls $SRCDIR/$WILDCARD`; do
		targetFileName=$(doStrReplacements "${DSTDIR}")"/"$(doStrReplacements $(basename "${fileName}"))
#		echo " $fileName $targetFileName"
		doFileReplacements "$fileName" "$targetFileName"
	done
}

createSources()
{
	doWildcardReplacements "templates/sources" "*.h" "../sources/applications/#APPNAME_LC#/include"
	doWildcardReplacements "templates/sources" "*.cpp" "../sources/applications/#APPNAME_LC#/src"
}

createCMakeProj()
{
	doWildcardReplacements "templates/builders/cmake" "*.txt" "../builders/cmake/applications/#APPNAME_LC#"
	echo "ADD_SUBDIRECTORY($APPNAME_LC $APPNAME_LC)">>../builders/cmake/applications/CMakeLists.txt
}

createNBOSXProj()
{
	doWildcardReplacements "templates/builders/netbeans/osx" "*.nb" "../builders/netbeans/osx/#APPNAME_LC#"
	doWildcardReplacements "templates/builders/netbeans/osx/nbproject" "*.mk" "../builders/netbeans/osx/#APPNAME_LC#/nbproject"
	doWildcardReplacements "templates/builders/netbeans/osx/nbproject" "*.bash" "../builders/netbeans/osx/#APPNAME_LC#/nbproject"
	doWildcardReplacements "templates/builders/netbeans/osx/nbproject" "*.xml" "../builders/netbeans/osx/#APPNAME_LC#/nbproject"
	doWildcardReplacements "templates/builders/netbeans/osx/nbproject" "*.properties" "../builders/netbeans/osx/#APPNAME_LC#/nbproject"
	doWildcardReplacements "templates/builders/netbeans/osx/nbproject/private" "*.xml" "../builders/netbeans/osx/#APPNAME_LC#/nbproject/private"
        doWildcardReplacements "templates/builders/netbeans/osx/nbproject/private" "*.properties" "../builders/netbeans/osx/#APPNAME_LC#/nbproject/private"
}

createWin32Proj()
{
	doWildcardReplacements "templates/builders/VS2010" "*.vcproj" "../builders/VS2010/#APPNAME_LC#"
}

updateServerConfig()
{
	doWildcardReplacements "templates" "crtmpserver.lua" "/tmp"

	awk '/--#INSERTION_MARKER#/ {system("cat /tmp/crtmpserver.lua")}; {print $0}' ../builders/cmake/crtmpserver/crtmpserver.lua > /tmp/final
	
	mv /tmp/final ../builders/cmake/crtmpserver/crtmpserver.lua
	rm /tmp/crtmpserver.lua
}

echo "Initialize variables..."
initVars

echo "Create sources..."
createSources

echo "Create cmake project..."
createCMakeProj

echo "Create NB project for OSX..."
createNBOSXProj

echo "Create win32 project..."
createWin32Proj

echo "Update server's config file..."
updateServerConfig

echo "Done :)"

