# Handle the incoming configuration files:
config() {
  for infile in $1; do
    NEW="$infile"
    OLD="$(dirname $NEW)/$(basename $NEW .new)"
    # If there's no config file by that name, mv it over:
    if [ ! -r $OLD ]; then
      mv $NEW $OLD
    elif [ "$(cat $OLD | md5sum)" = "$(cat $NEW | md5sum)" ]; then
      # toss the redundant copy
      rm $NEW
    fi
    # Otherwise, we leave the .new copy for the admin to consider...
  done
}

preserve_perms() {
  NEW="$1"
  OLD="$(dirname $NEW)/$(basename $NEW .new)"
  if [ -e $OLD ]; then
    cp -a $OLD ${NEW}.incoming
    cat $NEW > ${NEW}.incoming
    mv ${NEW}.incoming $NEW
  fi
  config $NEW
}

# Examples of how to handle your '.new' config files:
#preserve_perms etc/rc.d/rc.xca.new
#config etc/xca.conf.new

# Update the desktop database:
if [ -x usr/bin/update-desktop-database ]; then
  chroot . /usr/bin/update-desktop-database usr/share/applications > /dev/null 2>&1
fi

# Update hicolor theme cache:
if [ -d usr/share/icons/hicolor ]; then
  if [ -x /usr/bin/gtk-update-icon-cache ]; then
    chroot . /usr/bin/gtk-update-icon-cache -f -t usr/share/icons/hicolor 1> /dev/null 2> /dev/null
  fi
fi

# Update the mime database:
if [ -x usr/bin/update-mime-database ]; then
  chroot . /usr/bin/update-mime-database usr/share/mime >/dev/null 2>&1
fi

( cd usr/share/icons/hicolor/48x48/mimetypes ; rm -rf x-xca-template.png )
( cd usr/share/icons/hicolor/48x48/mimetypes ; ln -sf ../apps/xca.png x-xca-template.png )
( cd usr/share/icons/hicolor/48x48/mimetypes ; rm -rf x-xca-database.png )
( cd usr/share/icons/hicolor/48x48/mimetypes ; ln -sf ../apps/xca.png x-xca-database.png )
( cd usr/share/icons/hicolor/16x16/mimetypes ; rm -rf x-xca-template.png )
( cd usr/share/icons/hicolor/16x16/mimetypes ; ln -sf ../apps/xca.png x-xca-template.png )
( cd usr/share/icons/hicolor/16x16/mimetypes ; rm -rf x-xca-database.png )
( cd usr/share/icons/hicolor/16x16/mimetypes ; ln -sf ../apps/xca.png x-xca-database.png )
( cd usr/share/icons/hicolor/32x32/mimetypes ; rm -rf x-xca-template.png )
( cd usr/share/icons/hicolor/32x32/mimetypes ; ln -sf ../apps/xca.png x-xca-template.png )
( cd usr/share/icons/hicolor/32x32/mimetypes ; rm -rf x-xca-database.png )
( cd usr/share/icons/hicolor/32x32/mimetypes ; ln -sf ../apps/xca.png x-xca-database.png )
( cd usr/share/icons/hicolor/64x64/mimetypes ; rm -rf x-xca-template.png )
( cd usr/share/icons/hicolor/64x64/mimetypes ; ln -sf ../apps/xca.png x-xca-template.png )
( cd usr/share/icons/hicolor/64x64/mimetypes ; rm -rf x-xca-database.png )
( cd usr/share/icons/hicolor/64x64/mimetypes ; ln -sf ../apps/xca.png x-xca-database.png )
