#! /bin/sh

set -e

# Move the files of the complete installation into the appropriate 
# packages.

install_stuff() {
  from=debian/tmp
  to=debian/$target

  while [ "$#" -ge 1 ]; do
    dir="`dirname $1`"
    name="`basename $1`"

    mkdir -p $to/$dir
    mv $from/$dir/$name $to/$dir/
    shift
  done
}

check_completeness() {
  missing=`find debian/tmp -not -type d|wc -w`
  if [ "$missing" -gt 0 ]; then
    echo "There are $missing files missing from the resulting package set: "
    find debian/tmp -not -type d
    exit 1
  fi
}

echo -n "Removing default configuration files... "
find debian/tmp -name \*.default|xargs rm
echo done

echo -n "Removing broken .la files... "
find debian/tmp -name \*.la|xargs rm
echo done

echo -n "Removing static backend archives... "
rm debian/tmp/usr/lib/ldap/back_*.a
echo done

#echo -n "Installing: slapd"
#target=slapd
#install_stuff usr/sbin
#install_stuff usr/lib/ldap
#install_stuff usr/share/man/man5/slapd*
#install_stuff usr/share/man/man8
#install_stuff etc/ldap/schema
#install_stuff usr/share/ldap/ucdata
#rm -f debian/tmp/etc/ldap/slapd.conf
#mkdir -p debian/slapd/usr/share/slapd
#install -m644 debian/slapd.conf debian/slapd/usr/share/slapd/slapd.conf
#install -m755 debian/fix_ldif debian/slapd/usr/share/slapd/fix_ldif
#install -m755 debian/ldiftopasswd debian/slapd/usr/share/slapd/ldiftopasswd

#target=ldap-utils
#echo -n " $target"
## slappasswd is useful even without the server
#install -m755 -d debian/$target/usr/sbin
#mv debian/slapd/usr/sbin/slappasswd debian/$target/usr/sbin/
#install_stuff usr/bin
#install_stuff usr/share/man/man1
#install_stuff usr/share/man/man5/ldif*

target=libldap2
echo -n "Installing: $target"
install_stuff etc/ldap/ldap*.conf
install_stuff usr/share/man/man5/ldap.conf*
( cd debian/tmp/usr/lib && \
  	  ln -sf libldap_r.so.?.?.??? libldap.so.?.?.???
)
install_stuff usr/lib/*.so.*

target=libldap2-dev
echo -n " $target"
install_stuff usr/include
ln -sf libldap_r.a debian/tmp/usr/lib/libldap.a
install_stuff usr/lib/*.so usr/lib/*.a
install_stuff usr/share/man/man3
echo "."

#check_completeness
