#!/bin/sh -e

set -e

## Make sure that sources.list exists.

## If it doesn't exist, we try to rescue the backup made by our
## preinst.  This allows us to survive a update from a broken package
## database that doesn't contain conffile information, like the one in
## IT2006.

if [ ! -f /etc/apt/sources.list ]; then
  if [ -f /tmp/sources.list-preinst-backup ]; then
    mv -f /tmp/sources.list-preinst-backup /etc/apt/sources.list
  else
    touch /etc/apt/sources.list
  fi
else
  rm -f /tmp/sources.list-preinst-backup || true
fi

add_inittab ()
{
    line="$1"
    if ! grep -F -q "$line" /etc/inittab; then
	echo "+$line"
	echo "$line" >>/etc/inittab
    fi
}

rem_inittab ()
{
    line="$1"
    grep -v -F "$line" /etc/inittab >/etc/inittab.tmp || true
    mv /etc/inittab.tmp /etc/inittab
}

# At one point, apt-worker was called apt-worker.bin.  Make sure we
# get rid of it.
rem_inittab "resc::bootwait:/usr/libexec/apt-worker.bin rescue"

# Then the rescue operation in apt-worker was no enough. Now ham-rescue.sh
# is used.
rem_inittab "resc::bootwait:/usr/libexec/apt-worker rescue"

if [ ! -d /var/lib/hildon-application-manager ]; then
  mkdir /var/lib/hildon-application-manager
fi

## Clean away old configuration files

rm -f /etc/osso-backup/applications/osso-application-manager.conf

update-sudoers || true

if [ -x /usr/bin/update-mime-database ]; then
 update-mime-database /usr/share/mime
fi

if [ -x /usr/bin/osso-update-category-database ]; then
 osso-update-category-database /usr/share/mime
fi

if [ -x /usr/bin/update-desktop-database ]; then
 update-desktop-database
fi

hildon-application-manager-config update || true

#DEBHELPER#

exit 0
