#! /bin/sh

prefix=/usr/local
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
  --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
#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
#DefaultContentType "text/html; charset=iso-8859-1"
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
EXTRA_ENV+="PATH=\$\$PATH:$prefix/bin"
ECPPC=env \${EXTRA_ENV} ecppc
TNTNET=env \${EXTRA_ENV} tntnet
CXXFLAGS+=-I$includedir -fPIC -O2
LDFLAGS+=-shared -L$libdir -ltntnet -lcxxtools

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

project()
{
        P=$1
        mkdir $P || exit 1
        makefile $P >$P/Makefile
        template $P >$P/tntnet.conf
        cxxtools-config --properties tntnet >$P/tntnet.properties
        cat >$P/$P.ecpp <<EOF
<html>
 <head>
  <title>ecpp-application $P</title>
 </head>
 <body>
  <h1>$P</h1>
 </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 tntnet.conf" 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 1.6.2
        exit 0
        ;;

    --help)
        usage
        ;;

    --cxxflags)
        echo -I$includedir
        ;;

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

    --makefile=*)
        makefile $optarg
        ;;

    --makefile)
        makefile
        ;;

    --config=*)
        template $optarg
        ;;

    --config)
        template
        ;;

    --project=*)
        project $optarg
        ;;

    --project)
        project $2
        shift
        ;;

    *)
        usage
        exit 1
        ;;
    esac
    shift
done

exit 0
