#!/bin/bash
#
#  The post-install script needs to do two things:
#
#    1.  If this is a migration, then configure it.
#
#    2.  Otherwise force a rebuild
#
###

set -e

#
# Skip, if we are not in "configure" state
#
if [ "$1" != "configure" ]; then
        echo "I: Skipping configuration"
        exit 0
fi

#
#  The backup2l configuration file.
#
conf=/etc/backup2l.conf

#
#  Finally, regardless of whether we're upgrading or not, we
# trigger a rebuild
#
cd /etc/symbiosis/backup.d/ && make

######################################
#
# Divert config files.  This is cribbed from config-package-dev.
#
######################################

package="symbiosis-backup"
ourfile="/etc/symbiosis/backup.d/backup2l.conf" 
theirfile="$conf.dpkg-symbiosis-orig"

#
# Add the diversion, if it doesn't exist.
#
if ! dpkg-divert --list "$package" | \
  grep -xFq "diversion of $conf to $theirfile by $package"; then

  dpkg-divert --divert "$theirfile" --rename --package "$package" --add "$conf"

fi

#
# Now add the link in, unless it is already in place.
#
if [ ! -L "$conf" ] && [ ! -e "$conf" ]; then

  ln -s "$ourfile" "$conf"

elif [ ! -L "$conf" ] || \
     [ "$(readlink "$conf")" != "$ourfile" -a \
       "$(readlink "$conf")" != "$(basename "$theirfile")" ]; then

  echo "E: $conf is not linked to either "$ourfile" or "$theirfile"" >&2

fi

#DEBHELPER#

exit 0
