#!/bin/sh
#
# This script runs the Plex86 binary in /usr/lib/plex86/user/plex86 using
# the -f switch to specify a configuration file.
#
# Copyright (c) 2001, 2002 Robert Millan
#
# Protected by GNU GPL, See /usr/share/common-licenses/GPL for license terms

set -e

if [ `id -u` = "0" ]; then
  echo "It is not a good idea to run Plex86 as root."
  exit 0
fi

if ! grep -q plex86 /proc/devices; then
  echo "Plex86 module needs to be loaded for this to work,"
  echo "run \"/etc/init.d/plex86 start\" (as root) to load it."
  exit 1
fi

if test ! -d $HOME/.plex86; then
  echo "\$HOME/.plex86 does not exist! Read the instructions in /usr/share/doc/plex86/README.Debian.gz before invoking Plex86"
  exit 1
fi

if test -z $1; then
  echo "Please specify one of the following guest OS configurations:"
  echo "Usage: plex86 <guest_OS>"
  ls /etc/plex86/
  exit 0
fi

case "$1" in
    `ls $HOME/.plex86/conf/ 2>/dev/null | grep -x $1`)

        CONFIG="$HOME/.plex86/conf/$1"

    ;;

    `ls /etc/plex86/ | grep -x $1`)

        CONFIG="/etc/plex86/$1"

    ;;

    *)

        echo "$1 is not a valid configuration"
	exit 1

    ;;
esac

if test -f $HOME/.plex86/user.conf
then USER=`cat $HOME/.plex86/user.conf`
else USER=`cat /etc/plex86/user.conf`
fi

if echo $USER | grep -q user-x; then /usr/bin/X11/xset fp rehash; fi

cat $CONFIG |\
sed s%\#user\#%$USER%g |\
sed s%\#home\#%$HOME%g |\
/usr/lib/plex86/$USER/plex86 -f /dev/stdin
