#!/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
}

add_inittab "resc::bootwait:/usr/libexec/apt-worker rescue"

# 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"

## Migrate some files to their new names.

migrate ()
{
    old="$1"
    new="$2"
    if [ -e "$old" -a ! -e "$new" ]; then
	echo "Migrating $old to $new"
	mv -f "$old" "$new"
    fi
}

migrate /home/user/.osso/appinstaller \
        /home/user/.osso/hildon-application-manager

migrate /etc/osso-application-installer \
        /etc/hildon-application-manager

migrate /var/lib/osso-application-installer \
        /var/lib/hildon-application-manager

if [ ! -d /var/lib/hildon-application-manager ]; then
  mkdir /var/lib/hildon-application-manager
fi
if [ ! -e /var/lib/osso-application-installer ]; then
  ln -s hildon-application-manager /var/lib/osso-application-installer
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

# Remove init links so that they get updated.  Yes, this violates
# policy.
#
update-rc.d -f hildon-application-manager remove >/dev/null

#DEBHELPER#

exit 0
