#!/bin/bash

RESOURCE_DIR=@CMAKE_INSTALL_PREFIX@/@RESOURCE_DIR@
PGK_CFG_NAME=@PKG_CONFIG_FILE_NAME@

if [ $# -ne 1 ] ; then
  echo -e "usage:\n\ticl-create-project PROJECT_FOLDER_NAME"
  echo -e "\t(e.g.) ./icl-create-project MyICLProject"
  exit 1
fi

if [ -d $1 ] && [ "$1" != "." ] ; then
    echo "error: foldername $1 already exists"
    exit 1
fi

if [ -e $1 ]  && [ "$1" != "." ] ; then
    echo "error: file with name $1 already exists"
    exit 1
fi


if [ "$1" = "--update" ] ; then
    if [ ! -e Makefile ] ; then
        echo "error: unable to find Makefile"
        exit -1
    fi
    if [ ! -e Makefile.custom ] ; then
        echo "error: unable to find Makefile.custom"
        exit -1
    fi

    echo "# The following commands are suggested to"
    echo "# update your project build structure:"
    if [ -e "application" ] ; then
        echo svn mv application app
    fi
    
    if [ ! -e test ] ; then
        echo "mkdir test"
        echo "cp ${RESOURCE_DIR}/sample-test.cpp.template ./${PROJECT}/test/sample-test.cpp"
        echo "svn add test"
    fi

    if [ ! "$(echo Makefile | grep update-from-svn)" ] ; then
        echo "svn export https://opensource.cit-ec.de/svn/icl/trunk/scripts/icl-create-project-data/Makefile.template"
	echo "cp Makefile Makefile.old"
	echo "mv Makefile.template Makefile"
        echo "svn export https://opensource.cit-ec.de/svn/icl/trunk/scripts/icl-create-project-data/Makefile.custom.template"
	echo "cp Makefile.custom Makefile.custom.old"
	echo "mv Makefile.custom.template Makefile.custom"
        AT="@"
        echo "sed -i \"s|${AT}PKG_CONFIG_FILE_NAME${AT}|${PGK_CFG_NAME}|g\" Makefile.custom"
        echo '#please transfer your custom changes from Makefile.custom.old to the new Makefile.custom'
    else
        echo "make update-from-svn"
    fi
    exit 0;
fi

PROJECT=$1
if [ "$PROJECT" = "." ] ; then
    CREATE_PROJECT_INPLACE=TRUE ;
else
    CREATE_PROJECT_INPLACE=FALSE ;
fi

# Create folder root dir
echo -n "Creating folder ${PROJECT} ... "
mkdir -p ./${PROJECT}
echo "done"

# Create folder lib dir
echo -n "Creating sub folders ..."
for T in src app doc test ; do
    mkdir -p ./${PROJECT}/$T ;
    echo    "    $T"
done
echo    "                            done"

echo -n "Copying custom makefile ... "
cp ${RESOURCE_DIR}/Makefile.custom.template ./${PROJECT}/Makefile.custom
echo "done"

echo -n "Copying sample test ...     "
cp ${RESOURCE_DIR}/sample-test.cpp.template ./${PROJECT}/test/sample-test.cpp
echo "done"

echo -n "Copying makefile ...        "
cp  ${RESOURCE_DIR}/Makefile.template ./${PROJECT}/Makefile
echo "done"

echo -n "Copying doxyfile ...        "
cp ${RESOURCE_DIR}/doxyfile.template ./${PROJECT}/doc/doxyfile
echo "done"

if [ "$CREATE_PROJECT_INPLACE" = "TRUE" ] ; then
    USED_PROJECT_NAME=${PWD##*/}
else
    USED_PROJECT_NAME=$PROJECT
fi

echo "All source files placed in src/ are build into lib${USED_PROJECT_NAME}.so which"
echo "is automatically linked against the programs build from all source"
echo "files placed in app/ by calling"
echo " >> make"
echo " >> make install"
echo "Any source files in the test/ are build and run by calling"
echo " >> make test"
echo ""
echo "You can change the build configuration by editing Makefile.custom."
echo "You can adapt the name of your library and you can add"
echo "linker and compiler flags etc."
echo "You can change the build configuration by editing Makefile.custom."
echo ""
echo "You can always update the created build-system by calling"
echo " >> make update-from-svn"