#! /bin/sh

prefix=/usr
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

PATH=$PATH:$prefix/bin
export PATH

usage()
{
    cat 1>&2 <<EOF
Usage: $0 [OPTION]

Known values for OPTION are:

  --libs            print library linking information
  --cxxflags        print pre-processor and compiler flags
  --config[=app]    print default configuration-file
  --makefile[=app]  print simple makefile for a ecpp-project
  --properties=app  print a properties file
  --project=app     create a simple ecpp-project-directory
  --help            display this help and exit
  --version         output version information
EOF

    exit $1
}

template()
{
  if test -z "$1"
  then
    cat <<EOF
# map /webapp/comp.* or /webapp/comp to comp@webapp
MapUrl      ^/(.+)/([^.]+)(\..+)?  \$2@\$1
# map /comp.* or /comp to comp@comp
MapUrl      ^/([^.]+)(\..+)?       \$1@\$1
EOF
  else
    cat <<EOF
# map / to $1@$1
MapUrl      ^/$   $1@$1
# map /comp.* or /comp to comp@$1
MapUrl      ^/([^.]+)(\..+)?   \$1@$1
EOF
  fi

  cat <<EOF

# listen to a port
Listen              0.0.0.0 8000

# to enable ssl, we need a Certificate and another listen-command
#SslListen          0.0.0.0 8443    tntnet.pem

# this propertyfile defines, what and where to log
PropertyFile        tntnet.properties

# set limit to prevent DoS-attacks (default 0=no limit)
#MaxRequestSize     65536
#MaxRequestTime     600  # request timeout for the watchdog, when tntnet runs as a daemon
#User               tntnet
#Group              tntnet
#Dir                /
#Chroot             /var/safedir
#PidFile            /var/run/tntnet.pid
#Daemon             0
#MinThreads         5
#MaxThreads         100
#ThreadStartDelay   10  # in ms
#QueueSize          1000
#CompPath           path
#Load               webapp  # preload webapplication
#BufferSize         16384
#SocketReadTimeout  10
#SocketWriteTimeout 10000
#KeepAliveTimeout   15000
#KeepAliveMax       1000
#SessionTimeout     300
#ListenBacklog      16
#ListenRetry        5
#EnableCompression  no
#MimeDb             /etc/mime.types
#MinCompressSize    1024  # in bytes
#MaxUrlMapCache     8192
#DefaultContentType "text/html; charset=UTF-8"
#AccessLog          /var/log/tntnet/access.log
EOF
}

properties()
{
  cat <<EOF
# sample logging-properties for tntnet application $1
#
# print logging-messages with:
#   log_fatal("some fatal message");
#   log_error("some error message");
#   log_warn("some warn message");
#   log_info("some info message");
#   log_debug("some debug message");
#
rootLogger=INFO

# define logger-categories
logger.tntnet=INFO
logger.component.$1=INFO

# uncomment if you want to log to a file
#file=tntnet.log
#maxfilesize=1MB
#maxbackupindex=2
#host=localhost:1234  # send log-messages with udp
#disabled=1  # disable logging
#logprocess=1  # log through separate process
#logprocesuser=someuser  # change to user in log process
#logprocesgroup=somegroup  # change to group in log process
EOF
}

makefile()
{
  if test ! -z "$1"
  then
    cat <<EOF
all: $1.so

test: all
	\${TNTNET} tntnet.conf

$1.so: $1.o
	\${CXX} -o \$@ \${LDFLAGS} \$^

EOF
  fi

  cat <<EOF
.SUFFIXES: .ecpp .gif .jpg .css .js .cpp
ECPPC=$prefix/bin/ecppc
TNTNET=$prefix/bin/tntnet
CXXFLAGS+=-I$includedir -fPIC -O2
LDFLAGS+=-shared -L$libdir -ltntnet -lcxxtools

.ecpp.cpp:
	\${ECPPC} \${ECPPFLAGS} \${ECPPFLAGS_CPP} -o \$@ \$<
.gif.cpp:
	\${ECPPC} \${ECPPFLAGS} -m image/gif \${ECPPFLAGS_GIF} -b -o \$@ \$<
.jpg.cpp:
	\${ECPPC} \${ECPPFLAGS} -m image/jpg \${ECPPFLAGS_JPG} -b -o \$@ \$<
.png.cpp:
	\${ECPPC} \${ECPPFLAGS} -m image/png \${ECPPFLAGS_PNG} -b -o \$@ \$<
.css.cpp:
	\${ECPPC} \${ECPPFLAGS} -m text/css \${ECPPFLAGS_CSS} -b -o \$@ \$<
.js.cpp:
	\${ECPPC} \${ECPPFLAGS} -m application/javascript \${ECPPFLAGS_JS} -b -o \$@ \$<
.cpp.o:
	\${CXX} \${CPPFLAGS} \${CXXFLAGS} -c \$<
EOF
}

project()
{
        P=$1
        mkdir $P || exit 1
        makefile $P >$P/Makefile
        template $P >$P/tntnet.conf
        properties $P >$P/tntnet.properties
        cat >$P/$P.ecpp <<EOF
<%pre>
// put your includes here
// #include "foo.h"
</%pre>
<%args>
// define the query parameters
// bar;
</%args>
<%session>
// define your session scope variables here
// std::string mySessionState;
</%session>
<%cpp>
// put your C++ code here
</%cpp>
<html>
 <head>
  <title>ecpp-application $P</title>
 </head>
 <body>
  <h1>$P</h1>
  <p>Hello world!</p>
 </body>
</html>
EOF
        cat <<EOF

Sample ecpp-project "$P" created.
To build change to the directory "$P" and run make.
To run the application execute "tntnet" there.
To view the page navigate your browser to "http://localhost:8000/".

EOF
}

if test $# -eq 0; then
    usage
fi

while test $# -gt 0; do
    case "$1" in
    -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) optarg= ;;
    esac

    case "$1" in

    --version)
        echo 2.0
        exit 0
        ;;

    --help)
        usage
        ;;

    --cxxflags)
        echo -I$includedir
        ;;

    --libs)
        echo -L$libdir -ltntnet -lcxxtools
        ;;

    --makefile=*)
        makefile $optarg
        ;;

    --makefile)
        makefile
        ;;

    --config=*)
        template $optarg
        ;;

    --config)
        template
        ;;

    --properties=*)
        properties $optarg
        ;;

    --properties)
        properties $2
        shift
        ;;

    --project=*)
        project $optarg
        ;;

    --project)
        project $2
        shift
        ;;

    *)
        usage
        exit 1
        ;;
    esac
    shift
done

exit 0
