#!/bin/bash
#
# This script runs the Bochs binary in /usr/lib/bochs/bochs and hacks
# it to skip the configuration menu.
#
# Based on the launcher script for Plex86
#
# 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 Bochs as root."
  exit 0
fi

if test ! -f $HOME/.bochsrc; then
  echo "\$HOME/.bochsrc does not exist! Read the instructions in /usr/share/doc/bochs/README.Debian before invoking Bochs"
  exit 1
fi

if test -z $1; then
  echo "Please specify which disk set to boot (without the suffix):"
  echo "Usage: bochs <disk>"
  echo "Where <disk> will replace the #disk# strings in ~/.bochsrc"
  find $HOME/.bochs/ -type f -maxdepth 1
  exit 0
fi

/usr/bin/X11/xset fp rehash

# we get a random location
bochsrc=/tmp/$RANDOM
until ! test -e $bochsrc ; do bochsrc=/tmp/$RANDOM ; done

# and filter bochsrc against it
cat $HOME/.bochsrc |\
sed s%\#home\#%$HOME%g |\
sed s%\#disk\#%$1%g > $bochsrc

# we get another random location
bochs_wrap=/tmp/$RANDOM
until ! test -e $bochs_wrap ; do bochs_wrap=/tmp/$RANDOM ; done

# and put our wrap codes there
cat <<EOF >$bochs_wrap
1
$bochsrc
4
EOF

# now we run bochs, while listening to standard input
cat $bochs_wrap /dev/stdin | /usr/lib/bochs/bochs || true

# and clean up
rm -f $bochsrc $bochs_wrap
