#!/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_sudoer ()
{
    line="$1"
    if ! grep -F -q "$line" /etc/sudoers; then
	echo "+$line"
	chmod u+w /etc/sudoers # *cough*
	echo "$line" >>/etc/sudoers
	chmod u-w /etc/sudoers
    fi
}

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

## Add lines to sudo so that "user" can run "apt-worker" as
## "root" and so that s/he can restore the catalogues.
##
## KEEP THIS IN SYNC WITH prerm

add_sudoer 'user ALL = NOPASSWD: /usr/libexec/apt-worker'
add_sudoer 'user ALL = NOPASSWD: /usr/bin/hildon-application-manager-util restore-catalogues'

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
