#!/bin/sh

cdd=`/usr/share/cdd-dev/cdd-get-names metapackageprefix`
menudir=usr/share/cdd/"$cdd"/menu

SubstCDDName () {
    sed -e "s/#CDD#/${cdd}/g" \
        -e "s?\([/ ]\)\(config.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#-config 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."
	# I'm sure there are more clever ways to make the first letter upper case ...
	# task=`echo $pkg | sed 's/^\(.\).*/\1/' | tr "^[a-z]" "^[A-Z]"``echo $pkg | sed 's/^.//'`
	# For the moment disable tasksel if there is no "Task:" tag given
    else
	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"
    fi

    # 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 config/config exists use this as general helper script
if [ -s config/config ] ; then
  mkdir -p debian/"$cdd"-config/usr/bin
  mkdir -p debian/"$cdd"-config/usr/share/man/man1
  cp -a config/config   debian/"$cdd"-config/usr/bin/"$cdd"-config
  cp -a config/config.1 debian/"$cdd"-config/usr/share/man/man1/"$cdd"-config.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"-config debian/"$cdd"-"$pkg"/usr/bin/"$cdd"-"$pkg"
    ln -s "$cdd"-config.1.gz debian/"$cdd"-"$pkg"/usr/share/man/man1/"$cdd"-"$pkg".1.gz
  done
fi

# config/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 config/conf ] ; then
  # Get name of Custom Debian Distribution
  . config/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/{config.*,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}-config
  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"-config/etc/cdd/"$cdd"
  cp -a config/conf debian/"$cdd"-config/etc/cdd/"$cdd"/"$cdd".conf
fi
