#!/bin/sh

EMAIL=your-email@somewhere

usage()
{
    cat <<EOF
Usage: $0 [-l <template>] <projectname>
       $0 -l

Options:
  -t <template>, --template=<template>   specify template
  -l, --list                             list templates
  -c <project>, --config=<project>       output sample tntnet.xml

EOF

    exit $1
}

tntnet_xml()
{
    cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!--
  This is the main configuration file for tntnet.

  You can find documentation about the parameters in the man page tntnet.xml(7)
  -->
<tntnet>
EOF
  if test -z "$1"
  then
    cat <<EOF
  <mappings>
    <!-- map /webapp/comp.* or /webapp/comp to comp@webapp -->
    <mapping>
      <target>\$2@\$1</target>
      <url>^/(.+)/([^.]+)(\..+)?</url>
    </mapping>
    <!-- map /comp to comp@comp -->
    <mapping>
      <target>\$1@\$1</target>
      <url>^/(.*)</url>
    </mapping>
  </mappings>
EOF
  else
    cat <<EOF
  <mappings>
    <!-- map / to $1@$1 -->
    <mapping>
      <target>$1@$1</target>
      <url>^/$</url>
    </mapping>
    <!-- map /comp to comp@$1 -->
    <mapping>
      <target>\$1@$1</target>
      <url>^/(.*)</url>
    </mapping>
  </mappings>
EOF
  fi

  cat <<EOF

  <!-- listen to a port -->
  <listeners>
    <listener>
      <port>8000</port>
    </listener>
  <!--
    <listener>
      <port>8443</port>
      <certificate>tntnet.pem</certificate>
    </listener>
    -->
  </listeners>

  <logging>
    <rootlogger>INFO</rootlogger>

    <loggers>
      <logger>
        <category>tntnet</category>
        <level>INFO</level>
      </logger>
      <logger>
        <category>component.$1</category>
        <level>INFO</level>
      </logger>
    </loggers>

    <!--
    <file>tntnet.log</file>
    <maxfilesize>10M</maxfilesize>
    <maxbackupindex>2</maxbackupindex>
    -->

  </logging>

  <server>$1</server>

</tntnet>
EOF
}

error()
{
    echo "error: $1"
    echo "use \`$0 --help\` for usage information"
    exit 1
}

list()
{
    ls $TEMPLATEDIR
    [ -d $USERTEMPLATEDIR ] && ls $USERTEMPLATEDIR
}

checkProc()
{
    echo checking for $2
    if ! which $1 >/dev/null 2>&1
    then
        echo ERROR: $2 not installed
        exit 1
    fi
}

checkSystem()
{
    if [ -e "$1" ]
    then
        echo ERROR: $1 does already exist
        exit 1
    fi
    checkProc autoreconf autoconf
    checkProc automake   automake
}

prefix=/usr
TEMPLATEDIR=${prefix}/share/tntnet/template
USERTEMPLATEDIR=~/.tntnet/template

########################################################################
# main
#
while [ $# -gt 0 ]
do
    arg=$1
    shift

    case "$arg" in
        --help|-h)
            usage
            exit 0
            ;;

        --list|-l)
            list
            exit 0
            ;;

        --*=*)
            optarg=`echo "$arg" | /usr/bin/sed 's/[-_a-zA-Z0-9]*=//'`
            ;;

        -*)
            optarg=$1
            shift
            ;;
    esac

    case "$arg" in
        --config=*|-c)
            CONFIG=1
            project=$optarg
            ;;

        --template=*|-t)
            TEMPLATE=$optarg
            ;;

        -*)
            error "unkown option \`$arg\`"
            ;;

        *)
            if [ -n "$project" ]
                then
                error "multiple project names specified"
            fi
            project=$arg
            ;;
    esac
done

if [ -z "$project" ]
then
    error "no project name specified"
fi

if [ "$CONFIG" ]
then
    tntnet_xml $project
    exit 0
fi

if [ -z "$TEMPLATE" ]
then
    TEMPLATE="default"
fi

if [ -e "$USERTEMPLATEDIR/$TEMPLATE" ]
then
    TDIR="$USERTEMPLATEDIR/$TEMPLATE"
else
    TDIR="$TEMPLATEDIR/$TEMPLATE"
fi

if [ ! -d "$TDIR" ]
then
    error "template $TEMPLATE not found"
fi

echo project=$project template=$TDIR

if [ -d "$project" ]
then
    error "directory $project already exists"
fi

checkSystem $project

mkdir "$project"
cd "$project"
cproject=$(echo $project|/usr/bin/sed 's/-/_/g')

# create directory structure
for D in $(cd "$TDIR"; find . -type d|grep -v '^\.$')
do
    T=$(echo $D|/usr/bin/sed "s/@PROJECT@/$project/g; s/@CPROJECT@/$cproject/g")
    echo "$T"
    mkdir "$T"
done

# copy files
for F in $(cd "$TDIR"; find . -type f|grep -v '^\.$')
do
    T=$(echo $F|/usr/bin/sed "s/@PROJECT@/$project/g; s/@CPROJECT@/$cproject/g")
    echo "$T"
    /usr/bin/sed "s/@PROJECT@/$project/g; s/@CPROJECT@/$cproject/g" < "$TDIR/$F" > "$T"
done

if [ -f configure.ac ]
then
    echo run autoreconf -i
    autoreconf -i

    echo "============================================================"
    echo "= project <$project> created"
    echo "= change to directory <$project> and run <./configure && make> to build the application"
    echo "= execute <./$project> there to run the application"
    echo "============================================================"
else
    echo "============================================================"
    echo "= project <$project> created"
    echo "============================================================"
fi
