#! /bin/sh

set -e

trap "echo ; \
      echo Received signal... exiting. ; \
      echo ; \
      echo To reconfigure XFree86 at a later date, use dpkg --configure. ; \
      echo xbase-not-configured >>/etc/X11/config
      exit 1" 1 2 3 15

# Deal with a bug in previous versions.
if [ -L /etc/rc0.d/K1xdm ]
then
  mv /etc/rc0.d/K1xdm /etc/rc0.d/K01xdm
fi
if [ -L /etc/rc0.d/K1xfs ]
then
  mv /etc/rc0.d/K1xfs /etc/rc0.d/K01xfs
fi
if [ -L /etc/rc1.d/K1xdm ]
then
  mv /etc/rc1.d/K1xdm /etc/rc1.d/K01xdm
fi
if [ -L /etc/rc1.d/K1xfs ]
then
  mv /etc/rc1.d/K1xfs /etc/rc1.d/K01xfs
fi
if [ -L /etc/rc6.d/K1xdm ]
then
  mv /etc/rc6.d/K1xdm /etc/rc6.d/K01xdm
fi
if [ -L /etc/rc6.d/K1xfs ]
then
  mv /etc/rc6.d/K1xfs /etc/rc6.d/K01xfs
fi

update-rc.d xdm defaults 99 01 >/dev/null
update-rc.d xfs defaults >/dev/null

# Move old binaries to the right place
test -L /usr/bin/X11 && rm -f /usr/bin/X11
if [ -d /usr/bin/X11 ]
then
  cd /usr/bin/X11
  cp -a * /usr/X11R6/bin
  cd ..
  rm -rf X11
fi
( cd /usr/bin && ln -fs ../X11R6/bin X11 )

test -L /usr/include/X11 && rm -f /usr/include/X11
if [ -d /usr/include/X11 ]
then
  cd /usr/include/X11
  cp -a * /usr/X11R6/include/X11
  cd ..
  rm -rf X11
fi
( cd /usr/include && ln -fs ../X11R6/include/X11 X11 )

test -L /usr/lib/X11 && rm -f /usr/lib/X11
if [ -d /usr/lib/X11 ]
then
  cd /usr/lib/X11
  cp -a * /usr/X11R6/lib/X11
  cd ..
  rm -rf X11
fi
( cd /usr/lib && ln -fs ../X11R6/lib/X11 X11 )

# Make sure that Xsession is in the right place
if [ -f /etc/X11/xdm/Xsession ]
then
  echo "Note: Compare /etc/X11/Xsession and /etc/X11/xdm/Xsession.old"
  echo "once installation is complete."
  echo
  mv /etc/X11/xdm/Xsession /etc/X11/xdm/Xsession.old
  ln -s ../Xsession /etc/X11/xdm/Xsession
fi

# If we have no configuration file, create one
if [ ! -e /etc/X11/config ]
then
cat >/etc/X11/config <<EOF
# This file contains configuration flags for the X Window System.
# For a description of the meanings of the flags, see
# /usr/doc/X11/debian.README

run-xconsole
obey-nologin
allow-user-resources
allow-user-modmap
allow-user-xsession
allow-failsafe
EOF
fi

# If there's any saved configuration information from an upgrade, add it to
# the configuration file

if [ -f /etc/X11/config.instnotes ]
then
  cat /etc/X11/config /etc/X11/config.instnotes >/tmp/config.$$
  mv /tmp/config.$$ /etc/X11/config
  rm /etc/X11/config.instnotes
fi

# Remove the not-configured flag if it's there, otherwise restarts won't work
sed /^xbase-not-configured/d /etc/X11/config >/tmp/config.$$
mv /tmp/config.$$ /etc/X11/config

# Restart the font server
/etc/init.d/xfs start

cat <<EOF

There are three methods that can be used to start the X Window System:
\`startx', \`xdm', and \`xinit'.  We recommend the use of \`xdm', but you
may choose which you prefer.

EOF

# Maybe configure the X server
/usr/sbin/xbase-configure

# values for xdm: 3=don't change, 2=ask user
xdm=2
restart_xdm=0
if grep -q ^start-xdm /etc/X11/config || grep -q ^no-start-xdm /etc/X11/config
then
  xdm=3
  restart_xdm=1
fi

if [ $xdm = 2 ]
then

  while true
  do
    echo -n "Would you like xdm to be started at boot time? (y/n) [y]? "
    read input
    if [ $input = "y" ]
    then
      xdm=1
      break
    elif [ $input = "n" ]
    then
      xdm=0
      break
    elif [ ! $input ]
    then
      xdm=1
      break
    else
      echo "Please answer \`y' or \`n'."
    fi 2>/dev/null
  done
  
  sed '/^start-xdm/d; /^no-start-xdm/d' /etc/X11/config >/tmp/config.$$
  mv /tmp/config.$$ /etc/X11/config
  if [ $xdm = 1 ]
  then
    echo -n "Enabling \`xdm'."
    echo start-xdm >>/etc/X11/config
  
    echo
    echo -n "The next time you reboot the system, \`xdm' will automatically "
    echo "start."
  else
    echo "Disabling \`xdm'."
    echo no-start-xdm >>/etc/X11/config
  fi

  if [ $xdm = 1 ]
  then
  
    cat <<EOF

Starting xdm now will allow you to start using the X Window System
immediately. However, if your X server is not properly configured
starting xdm now could cause problems.

If you are installing an X server but have not yet configured it, you
should not start xdm now. If the X server is already configured then
it should be safe to start xdm.

EOF
  
    echo -n "Do you want to start xdm now? (y/n) [n] "
  
    read input
    if [ "$input" = "y" ] ; then
      /etc/init.d/xdm start
    else
      echo "You may start xdm later by typing '/etc/init.d/xdm start'."
    fi
  fi
fi

if [ $restart_xdm = 1 ]
then
  /etc/init.d/xdm start
fi

exit 0
