#!/bin/sh
# Debian xlib6g package pre-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

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 installation of xlib6g package.' ;\
      echo ;\
      exit 1" 1 2 3 15

case "$1" in
  install|upgrade) ;;
  abort-upgrade) exit 0 ;;
esac

condecho=:

# check symbolic link structure integrity
for symlink in /usr/X11R6/lib/X11/xkb/compiled; do
  if [ -L $symlink ]; then
   if [ $(maplink $symlink) != $(readlink $symlink) ]; then
      $condecho
      echo "Warning: $symlink symbolic link points to the wrong"
      echo "place.  Removing."
      condecho=echo
      rm $symlink
    fi
  elif [ -e $symlink ]; then
    $condecho
    echo "Warning: $symlink is not a symbolic link.  Moving to"
    echo "$symlink.xlib6g-old."
    condecho=echo
    if [ ! -e $symlink.xlib6g-old ]; then
      mv $symlink $symlink.xlib6g-old
    else
      $condecho
      echo "ERROR: $symlink.xlib6g-old already exists!  Aborting"
      echo "installation of xlib6g package.  Please clean up your crufty filesystem."
      condecho=echo
      exit 1
    fi
  fi
done

# clean up after 3.3.2.3a-2 to 3.3.2.3a-7
if [ -L /usr/X11R6/lib/X11/locale ]; then
  $condecho
  echo "Note: Removing /usr/X11R6/lib/X11/locale symbolic link."
  condecho=echo
  rm /usr/X11R6/lib/X11/locale
fi

if [ -d /var/state/xkb ]; then
  $condecho
  echo "Note: Removing obsolete directory /var/state/xkb."
  condecho=echo
  rm -r /var/state/xkb
fi

exit
