#!/bin/sh
set -e

# Author: Johnie Ingram <johnie@debian.org>
# Time-stamp: <97/01/02 05:26:42 root>

# Inspired by the Netscape post-install script written by
# Brian C. White <bcwhite@verisim.com>, and the Xfree86 server
# post-install script written by Stephen Early.


TARBALL=MetroX-3.1.2.tar
INNARDS=metroess.tar


if [ "$0" = "abort-upgrade" ]
then
    exit 0
fi

if [ "$TMPDIR" != "" ]
then
    TDIR=$TMPDIR
else
    TDIR=/var/tmp
fi

XTDIR=$TDIR/dpkg-matrox-inst
DELFILES="$XTDIR"
cd $TDIR

if [ -f ${TARBALL} -o -f ${TARBALL}.gz -o -f ${TARBALL}.Z ]
then
    if [ ! -f ${INNARDS}.gz -a ! -f ${INNARDS}.Z ]
    then
	[ -f ${TARBALL}.gz -o -f ${TARBALL}.Z ] && zopt="-z"
	echo -n "Untarring ${TARBALL} ... "
	if tar $zopt -xf ${TARBALL}*
	then
	    DELFILES="$DELFILES install.metroess metroess.tar.gz"
	    echo done.
	else
	    exit 1
	fi
    fi
fi

if [ -f ${INNARDS} -o -f ${INNARDS}.gz -o -f ${INNARDS}.Z ]
then
    if [ -f ${INNARDS}.gz -o -f ${INNARDS}.Z ]
    then
	zopt="-z"
    else
	zopt=""
    fi
    echo -n "Untarring ${INNARDS} ... "
    mkdir -p $XTDIR
    cd $XTDIR
    if tar $zopt -xf ../${INNARDS}* 2> /dev/null
    then
	echo done.
    else
	echo failed!
	echo -n "Cleaning up ... "
	cd $TDIR && rm -rf $DELFILES
	echo "done (sigh)."
	exit 1
    fi
else
    echo Error: ${INNARDS}.gz not found in $TARBALL or alone in $TDIR.
    exit 1
fi

echo -n "Installing MetroX: "
chown -R root.root $XTDIR
chmod -R u=rwX,go=rX $XTDIR

echo -n "server "
install -m 755 $XTDIR/usr/X11R6/bin/* /usr/X11R6/bin

echo -n "libraries "
rm -rf /usr/X11R6/lib/X11/Metro
echo -n "."
install -d -m 755 /usr/X11R6/lib/X11/Metro
cp -dpRf $XTDIR/usr/X11R6/lib/X11/Metro/* /usr/X11R6/lib/X11/Metro
echo -n "."

echo ". done."

echo -n Cleaning up...
cd $TDIR && rm -rf $DELFILES
echo "done."


### This part stolen from Stephen Early's xserver-svga 3.2-1.

if ! grep -q ^/usr/X11R6/bin/Xmetro /etc/X11/Xserver
then
    echo -n "Do you want to make the MetroX server the default? (y/n) [y] "
    read
  
    if [ "$REPLY" = "n" -o "$REPLY" = "N" ]
    then
	echo "The default X server will not be changed."
    else
	mv /etc/X11/Xserver /etc/X11/Xserver.old
	sed -e "1s/^.*$/\\/usr\\/X11R6\\/bin\\/Xmetro/" /etc/X11/Xserver.old \
	    > /etc/X11/Xserver

	if [ -L /usr/X11R6/bin/X -o ! -e /usr/X11R6/bin/X ]
	then
	    # We don't have an xbase package installed
	    echo -n "You don't have the X server wrapper program installed."
	    echo "/etc/X11/X"
	    echo "is being made into a symbolic link to your X server."
	    echo "/usr/X11R6/bin/X is a symbolic link to /etc/X11/X."
  
	    rm -f /usr/X11R6/bin/X
	    ln -s /etc/X11/X /usr/X11R6/bin/X
	    rm -f /etc/X11/X
	    ln -s /usr/X11R6/bin/Xmetro /etc/X11/X
	else
	    # We've got an xbase; should have the wrapper program installed.
	    rm -f /etc/X11/X
	    ln -s /usr/X11R6/bin/X /etc/X11/X
	fi
  	echo "The MetroX server is now the default."
    fi
fi

### End gratuitous theft.

echo
echo "Run MetroX configuration program now (this will terminate any"
echo -n "existing X sessions)?  (y/n) [n] "
read

if [ "$REPLY" = "Y" -o "$REPLY" = "y" ]
then
    set +e
    /etc/init.d/xdm stop > /dev/null 2>&1
    killall X > /dev/null 2>&1
    killall Xmetro > /dev/null 2>&1
    /usr/X11R6/bin/configX 2> /dev/null || echo "Config failed (sigh)."
else
    echo "You can configure MetroX by typing \"configX\"."
    echo
fi
