#!/bin/sh

cdd=`grep "^Package:" debian/control.stub | sed "s/Package: \+//"`
menudir=etc/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
}

# if there is no directory named menu ignore the menu stuff
if [ -d menu ] ; then
  # copy user menu file
  for pkg in `ls menu docs | grep -v "^[a-z]*:$*" | grep -v "^$" | sort | uniq` ; do
    if [ -d debian/"$cdd"-"$pkg" ] ; then
      mkdir -p debian/"$cdd"-"$pkg"/"$menudir"
    fi
    if [ -d menu/"$pkg" ] ; then
      cp -a menu/"$pkg" debian/"$cdd"-"$pkg"/"$menudir"/"$cdd"-"$pkg"
    fi
    # Check for documentation of packages which should be viewed in
    # case of missing GUI
    if [ -d docs -a -d docs/"$pkg" ] ; then
      for dep in `ls docs/"$pkg"/[a-z]*.txt` ; do
        # first check, whether this package is really listed in the
	# dependencies
	depmenu=`basename ${dep} .txt`
	if ! grep -A 3 "Package: $cdd-$pkg" debian/control  | grep "Depends:" | grep -q -w ${depmenu} ; then
	   echo "Package ${depmenu} seems not to be in dependencies"
	   continue
	fi
        menuhead=`head -n 1 $dep`
	title=`echo "$menuhead" | sed "s/[[:space:]]*:[[:space:]]\+.*//"`
	hint=`echo "$menuhead" | sed "s/.*:[[:space:]]\+//"`
	section=`echo "$cdd/$pkg" | perl -ne '@s = split ?/?; print "\u\L$s[0]/"."\u\L$s[1]";'`
	if [ _"$hint" != _"$title" ] ; then
	  cat >> debian/"$cdd"-"$pkg"/"$menudir"/"$cdd"-"$pkg" <<EOT
?package(${cdd}-${pkg}): needs="text" \\
    section="${section}" \\
    title="${title}" \\
    command="pager /usr/share/doc/${cdd}-${pkg}/${depmenu}.txt" \\
    hints="$hint"
EOT
        else
	  echo "Menu entry ${dep} for package ${cdd}-${pkg} has wrong format."
	  title=`echo ${depmenu} | perl -ne 'print "\u\L$_";'`
	  cat >> debian/"$cdd"-"$pkg"/"$menudir"/"$cdd"-"$pkg" <<EOT
?package(${cdd}-${pkg}): needs="text" \\
    section="${section}" \\
    title="${title}" \\
    command="view /usr/share/doc/${cdd}-${pkg}/${depmenu}"
EOT
	fi
      done
    fi
    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

  # check for packages without menu
  for pkg in `ls tasks` ; do
    if [ ! -d debian/"$cdd"-"$pkg"/"$menudir" -a ! -d docs/"$pkg" ] ; then
      echo "Warning: Please provide a menu for package ${cdd}-${pkg}."
    else
      # If there is a menu then we have to depend from ${cdd}-common
      mv debian/control debian/control.bak
      # If there is a file named VERSION we have to uses versioned dependency
      # if [ -s VERSION ] ; then version="(>= `head -n 1 VERSION`)" ; else version='' ; fi
      # I decided to use versioned Build-Depends in any case:
      version="(>= `dpkg-parsechangelog | grep "^Version:" | sed -e "s/^Version:[[:space:]]\+\([\.0-9]*\)[[:space:]]*/\1/"`)"
      sed -e "/^Package: ${cdd}-${pkg}/{;N;N;N;s/\(Package: ${cdd}-${pkg}\n.*Depends: .*\)\(\n\)/\1, $cdd-common $version\2/;}" \
           debian/control.bak >  debian/control
      rm -f debian/control.bak
    fi
  done
fi

# 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
  
  # Get name of Custom Debian Distribution
  [ -f common/conf ] && source 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
  mkdir -p debian/po
  for po in `ls /usr/share/cdd/templates/po/*` ; do
    poname=`basename $po`
    SubstCDDName po/${poname} po/${poname}
  done
  [ -d debian/po ] && debconf-updatepo

  # 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
  
  # Add common config file for ${cdd} if common/conf exists
  if [ -s common/conf ] ; then
    mkdir -p debian/"$cdd"-common/etc/cdd/"$cdd"
    cp -a common/conf debian/"$cdd"-common/etc/cdd/"$cdd"/"$cdd".conf
  fi
fi

# if docs exists copy relevant docs
if [ -d docs ] ; then
  for pkg in `ls docs` ; do
    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"
    cp -a docs/"$pkg"/[a-z]* debian/"$cdd"-"$pkg"/usr/share/doc/"$cdd"-"$pkg"
  done
fi
