#!/bin/sh

# This script gets run by cdbs if you add a rule like this to your
# debian/rules file:
#
#makebuilddir::
#	SITENAME=seereason.com SERVERADMIN=ddssff@gmail.com PROXYPORT=9026 TESTINGPORT=8000 happstack-debianization-install
#
# If you run debian/rules makebuilddir by hand it will preprocess each
# of the files in the scripts subdirectory and write them out to
# debian/ with the added prefix "${APPNAME}-" as computed below.

# Get the application name from the debian binary package names.
APPNAME=`dh_listpackages | sed -n 's/-production$//p'`

case "${APPNAME}" in
    "")
      echo "happstack-debianization: need binary package with suffix -production"
      exit 1
      ;;
esac

d=/usr/share/happstack-debianization

# The essence of debianization is to put just the right files in just
# the right places with just the right names so that the Debian magic
# happens.  That is what the following loop is supposed to do.  For
# each of the files in scripts/, use sed to expand the macros and
# install the file into the debian subdirectory.

ls -1 $d | while read i; do

  # Create any required directories
  case $i in
      "production-apache") mkdir -p "debian/${APPNAME}-production/etc/apache2/sites-available/";;
      "logrotate") mkdir -p "debian/${APPNAME}-production/etc/logrotate.d/";;
  esac

  # Preprocess the script
  cat "$d/$i" |
  sed -e "s/#APPNAME#/${APPNAME}/g" \
      -e "s/#SITENAME#/${SITENAME}/g" \
      -e "s/#HOSTNAME#/${HOSTNAME}/g" \
      -e "s/#PROXYPORT#/${PROXYPORT}/g" \
      -e "s/#TESTINGPORT#/${TESTINGPORT}/g" \
      -e "s/#SERVERADMIN#/${SERVERADMIN}/g" \
      -e "s/#MESSAGE#/# This file was generated by the happstack-debiniazation package./g" |

  # Install the result
  case "$i" in
      "production-apache") cat > "debian/${APPNAME}-production/etc/apache2/sites-available/${SITENAME}";;
      "logrotate") cat > "debian/${APPNAME}-production/etc/logrotate.d/${SITENAME}";;
      *) cat > "debian/${APPNAME}-$i";;
  esac

done
