#!/bin/sh
# Debian xfree86-common 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 xfree86-common preinst
  for file in /usr/bin/X11 \
              /usr/include/X11 \
              /usr/lib/X11 \
              /usr/X11R6/lib/X11/doc; do
    if [ -e $file.xfree86-common-old ]; then
      rm $file
      mv $file.xfree86-common-old $file
    fi
  done;
}

# TODO comment out these two functions for stable release
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;
}

trap "echo ;\
      echo 'Received signal.  Aborting configuration of xfree86-common 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/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 "ERROR: $symlink symbolic link points to the wrong place."
      echo "Please report this message to <xfree86-common@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 <xfree86-common@packages.debian.org>."
    condecho=echo
  else
    $condecho
    echo "ERROR: $symlink symbolic link does not exist."
    echo "Please report this message to <xfree86-common@packages.debian.org>."
  fi
done

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

# ensure that /etc/X11/Xresources is now a directory (see preinst)
if [ ! -d /etc/X11/Xresources ]; then
  $condecho
  echo "ERROR: /etc/X11/Xresources is not a directory."
  echo "Please report this message to <xfree86-common@packages.debian.org>."
  condecho=echo
fi

if command -v install-docs > /dev/null 2>&1; then
  install-docs -i /usr/share/doc-base/xfree86-faq
fi

exit
