#!/bin/sh
# Debian xfree86-common 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/bin/X11) echo ../X11R6/bin ;;
    /usr/include/X11) echo ../X11R6/include/X11 ;;
    /usr/lib/X11) echo ../X11R6/lib/X11 ;;
    /usr/X11R6/lib/X11/doc) echo ../../../share/doc/xfree86-common ;;
  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;
}

parseans () {
  if [ ! $1 ]; then
    echo $default
  else
    echo $1 | cut -c1 | tr '[A-Z]' '[a-z]';
  fi;
}

clobberdir () {
  if [ -d /usr/doc/xfree86-common ]; then
    rm -r /usr/doc/xfree86-common
  fi;
}

trap "echo ;\
      echo 'Received signal.  Aborting installation of xfree86-common package.' ;\
      echo ;\
      exit 1" 1 2 3 15

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

condecho=:

if [ "$2" ]; then
  if dpkg --compare-versions $2 gt 3.3.5-1; then
    if dpkg --compare-versions $2 le 3.3.6-4; then
      clobberdir
    fi
  fi
fi

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

if [ -L /usr/doc/X11 ]; then
  rm /usr/doc/X11
fi

# in xbase 3.3.2.3a-2, /etc/X11/Xresources became a directory
if [ -f /etc/X11/Xresources ]; then
  $condecho
  echo "Note: Moving /etc/X11/Xresources to /etc/X11/Xresources.xfree86-common-old."
  echo "Please see /usr/share/doc/xfree86-common/README.Debian for more information."
  condecho=echo
  if [ ! -e /etc/X11/Xresources.xfree86-common-old ]; then
    mv /etc/X11/Xresources /etc/X11/Xresources.xfree86-common-old
  else
    $condecho
    echo "ERROR: /etc/X11/Xresources.xfree86-common-old already exists!  Aborting"
    echo "installation of xfree86-common.  Please clean up your crufty filesystem."
    condecho=echo
    exit 1
  fi
fi

exit
