#!/bin/sh
# /usr/local/sbin/dphys-config-altern-xsession - set /etc/alternatives/*
# author Neil Franklin, last modification 2006.10.12
# copyright ETH Zuerich Physics Departement,
#   use under either modified/non-advertising BSD or GPL license

# this script is called by dphys-config, once on each install/update
#   no parameters or env vars for this script
# it requires in /etc/dphys-config.list 1 line:
#   local/sbin/dphys-config-altern-xsession:/usr/:chmod 755 {}; {}

# this will only work on Debian, as /etc/alternatives/* is Debian specific

set -e

# get ready to work
# /usr/local added for FreeBSD, because their ports end up in there
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
export PATH


# --- main stuff

if [ -x /usr/bin/fvwm2 ] ; then
  # this block taken from old /usr/sbin/dphys-local-x package
  #   possibly /usr/bin/fvwm forgets (or once forgot?) to install itsself here
  if ! update-alternatives --display x-session-manager | \
      grep -q /usr/bin//fvwm > /dev/null ; then
    update-alternatives --install /usr/bin/x-session-manager \
        x-session-manager /usr/bin/fvwm2 99 --slave \
        /usr/share/man/man1/x-session-manager.1.gz \
        x-session-manager.1.gz /usr/share/man/man1/fvwm2.1.gz
  fi

  update-alternatives --set x-session-manager /usr/bin/fvwm2
else
  # we are doing new installation, dphys-config run before dphys-admin
  #   config files + scripts are being installed, but packaged software missing
  MISSING_PACKAGE=yes
fi


# --- avoid problem with not running on install, because packages not yet here

if [ "${MISSING_PACKAGE}" = yes ] ; then
  # remove this script to force rerun and reinstall on next dphys-config run
  #   should happen after the reboot directly after installing all packages
  rm $0
  # this will actually produce warning from dphys-config, but  exit 0  is wrong
  exit 1
fi


# --- finish off

exit 0
