#!/bin/sh

cdd=`grep "^Package:" debian/control.stub | sed "s/Package: \+//"`
menudir=usr/share/cdd/"$cdd"/menu

SubstCDDName () {
    sed -e "s/#CDD#/${cdd}/g" \
        -e "s?\([/ ]\)\(common.templates:*\)?\1${cdd}-\2?" \
        -e "s/#CDDNAME#/${CDDNAME}/g" /usr/share/cdd/templates/$1 >> \
	debian/$2
}

# Make dependency from menu containing packages to the right #CDD#-common version
version="(>= `dpkg-parsechangelog | grep "^Version:" | sed -e "s/^Version:[[:space:]]\+\([\.0-9]*\)[[:space:]]*/\1/"`)"

# General Task registry, menu registration and post{inst,rm} preparation
for pkg in `ls tasks | grep -v "^[a-z]*:$*" | grep -v "^$" | sort | uniq` ; do
    # registration
    if ! grep -q -w "^Task:" tasks/$pkg ; then
	echo "Control file template tasks/$pkg does not contain 'Task:' field."
	exit -1
    fi
    task=`grep -w "^Task:" tasks/"$pkg" | sed "s/^Task:[[:space:]]*\([^[:space:]]*\)/\1/"`
    mkdir -p debian/"$cdd"-"$pkg"/usr/share/cdd/tasks/"$cdd"/
    echo "$task" > debian/"$cdd"-"$pkg"/usr/share/cdd/tasks/"$cdd"/"$pkg"

    # if we provide an extra menu which overrides some menus provided by
    # the maintainers of the dependand packages move them now to the right
    # directory
    [ -d menu ] && [ -d menu/"$pkg" ] && \
	    if [ `find menu/"$pkg" -maxdepth 1 -name \*.menu | wc -l` -gt 0 ] ; then
	mkdir -p debian/"$cdd"-"$pkg"/"$menudir"
	for dep in `find menu/"$pkg" -maxdepth 1 -name \*.menu` ; do
		cp -a "$dep" debian/"$cdd"-"$pkg"/"$menudir"/`basename "$dep" .menu`
        done
    fi

    # Provide a README.Debian in any case
    mkdir -p debian/"$cdd"-"$pkg"/usr/share/doc/"$cdd"-"$pkg"
    [ ! -s docs/"$pkg"/README.Debian ] && cp -a /usr/share/cdd/templates/README.Debian debian/"$cdd"-"$pkg"/usr/share/doc/"$cdd"-"$pkg"
    # Check for documentation of packages (*.txt or *.html) which should be viewed in
    # case of missing GUI
    [ -d menu ] && [ -d menu/"$pkg" ] && \
	    if [ `find menu/"$pkg" -maxdepth 1 -name \*.txt -o -name \*.html | wc -l` -gt 0 ] ; then
	for dep in `find menu/"$pkg" -maxdepth 1 -name \*.txt -o -name \*.html` ; do
            # Formerly here was checked, whether this package is really listed in the
	    # dependencies, with more clever menu handling it is enough to verify
	    # whether it is mentioned at all in the package relations and the menu
	    # system cares about whether a sugested package is installed or not
	    depmenu=`basename ${dep} .txt`
	    if [ "$depmenu" = `basename ${dep}` ] ; then depmenu=`basename ${dep} .html` ; fi
	    if ! grep -A 5 "Package: $cdd-$pkg" debian/control | grep -q -w "$depmenu" ; then
		## echo "Package ${depmenu} seems not to be in dependencies"
		continue
	    fi
	    cp -a "$dep" debian/"$cdd"-"$pkg"/usr/share/doc/"$cdd"-"$pkg"
	done
    fi

    # post{inst/rm} template are appended if some extra scripts are provided or just created
    # an extra postinst has to be saved (*.stub) and restored by the clean target in
    # debian/rules
    [ -s debian/"$cdd"-"$pkg".postinst.stub ] && cp debian/"$cdd"-"$pkg".postinst.stub debian/"$cdd"-"$pkg".postinst
    sed -e "s/#CDD#/${cdd}/g" \
        -e "s/#PKG#/${cdd}-${pkg}/g" \
       /usr/share/cdd/templates/postinst >> debian/"$cdd"-"$pkg".postinst
    sed -e "s/#CDD#/${cdd}/g" \
        -e "s/#PKG#/${cdd}-${pkg}/g" \
       /usr/share/cdd/templates/postrm >> debian/"$cdd"-"$pkg".postrm
done

# if common/common exists use this as general helper script
if [ -s common/common ] ; then
  mkdir -p debian/"$cdd"-common/usr/bin
  mkdir -p debian/"$cdd"-common/usr/share/man/man1
  cp -a common/common   debian/"$cdd"-common/usr/bin/"$cdd"-common
  cp -a common/common.1 debian/"$cdd"-common/usr/share/man/man1/"$cdd"-common.1

  # install link to package helper script
  for pkg in `ls tasks` ; do
    mkdir -p debian/"$cdd"-"$pkg"/usr/bin
    mkdir -p debian/"$cdd"-"$pkg"/usr/share/man/man1
    ln -s "$cdd"-common debian/"$cdd"-"$pkg"/usr/bin/"$cdd"-"$pkg"
    ln -s "$cdd"-common.1.gz debian/"$cdd"-"$pkg"/usr/share/man/man1/"$cdd"-"$pkg".1.gz
  done
fi

# common/conf should really exist for the CDD registry in /etc/cdd
# currently there is no error message issued if it is missing but
# this might be reasonable
if [ -s common/conf ] ; then
  # Get name of Custom Debian Distribution
  . common/conf
  # Config file should set CDDNAME, but if not try to build a useful one
  if [ _"$CDDNAME" = _"" ] ; then
    CDDNAME=Debian-`echo ${cdd} | perl -ne 'print "\u\L$_";'`
  fi
  # Move templates for user configuration script
  for comm in `ls /usr/share/cdd/templates/{common.*,apt.conf}` ; do
    commname=`basename $comm`
    SubstCDDName ${commname} ${cdd}-${commname}
  done
  # rename apt.conf.d file 
  [ -s debian/${cdd}-apt.conf ] && mv debian/${cdd}-apt.conf debian/90${cdd}-common
  if [ -d debian/po.stub ] ; then
      cp -a debian/po.stub debian/po
  else
      mkdir -p debian/po
  fi
  for po in `ls /usr/share/cdd/templates/po/*` ; do
    poname=`basename $po`
    SubstCDDName po/${poname} po/${poname}
  done
  [ -d debian/po ] && debconf-updatepo

  # Add common config file for ${cdd}
  mkdir -p debian/"$cdd"-common/etc/cdd/"$cdd"
  cp -a common/conf debian/"$cdd"-common/etc/cdd/"$cdd"/"$cdd".conf
fi

pkgsrc=debian-${cdd}
# Fix *.dsc file to mention all binary targets
TASKS="" ; \
if [ -d common ] ; then TASKS="${cdd}-common" ; fi ; \
for task in `ls -1 tasks | sort` ; do \
    if [ "${TASKS}" != "" ] ; then TASKS="${TASKS}, " ; fi ; \
    TASKS="${TASKS}${cdd}-${task}" ; \
done ; \
sed "s/^\(Binary: \)${cdd}/\1${TASKS}/" ../${pkgsrc}_*.dsc > tmp.dsc ; \
mv tmp.dsc ../${pkgsrc}_*.dsc
