#!/bin/sh
# Debian xlib6g package post-installation script
# Copyright 1998, 1999 Branden Robinson.
# Licensed under the GNU General Public License, version 2.  See the file
# /usr/share/common-licenses/GPL or <http://www.gnu.org/copyleft/gpl.txt>.
# Acknowlegements to Stephen Early, Mark Eichin, and Manoj Srivastava.

set -e

cleanup () {
  # unroll changes in xlib6g preinst
  for file in /usr/X11R6/lib/X11/xkb/compiled; do
    if [ -e $file.xlib6g-old ]; then
      rm $file
      mv $file.xlib6g-old $file
    fi
  done;
}

# TODO comment out these two functions for stable release
maplink () {
  # returns what symlink should point to
  case "$1" in
    /usr/X11R6/lib/X11/xkb/compiled) echo /var/lib/xkb ;;
  esac;
}

readlink () {
  # perl kludge until readlink(1) from tetex-bin is moved into an essential package
  # returns what symlink actually points to
  perl -e '$l = shift; exit 1 unless -l $l; $r = readlink $l; exit 1 unless $r; print "$r\n"' $1;
}

trap "echo ;\
      echo 'Received signal.  Aborting configuration of xlib6g package.' ;\
      echo -n 'Cleaning up...' ;\
      cleanup ;\
      echo 'done.' ;\
      echo ;\
      exit 1" 1 2 3 15

case "$1" in
  configure) ;;
  abort-upgrade|abort-remove|abort-deconfigure)
    cleanup
    exit 0 ;;
esac

condecho=:

# TODO sanity checks -- comment out these for stable release
for symlink in /usr/X11R6/lib/X11/xkb/compiled; do
  if [ -L $symlink ]; then
    if [ $(maplink $symlink) != $(readlink $symlink) ]; then
      $condecho
      echo "ERROR: $symlink symbolic link points to the wrong"
      echo "place.  Please report this message to <xlib6g@packages.debian.org>."
      condecho=echo
    fi
  elif [ -e $symlink ]; then
    $condecho
    echo "ERROR: $symlink is not a symbolic link."
    echo "Please report this message to <xlib6g@packages.debian.org>."
    condecho=echo
  else
    $condecho
    echo "ERROR: symbolic link $symlink does not exist."
    echo "Please report this message to <xlib6g@packages.debian.org>."
  fi
done

if [ -d /usr/doc -a ! -e /usr/doc/xlib6g -a -d /usr/share/doc/xlib6g ]; then
  ln -sf ../share/doc/xlib6g /usr/doc/xlib6g
fi

if ! grep -qs ^/usr/X11R6/lib\$ /etc/ld.so.conf; then
  echo /usr/X11R6/lib >> /etc/ld.so.conf
fi

ldconfig

exit
