#!/bin/sh

VERSION="1.25"

NO_GRAPHICS=false
SLOW_CPU=false
BIOS_IMAGE="bios.bin"
FLOPPY_IMAGE="fd.img"
HARDDISK_IMAGE=

SHARE_DIR="/usr/share/8086tiny"
LIB_DIR="/usr/lib/8086tiny"
BIN=

check_write () {
	FILE="$1"
	if ! [ -f "$FILE" ]; then
		echo "$FILE not found" >&2
		echo "Bios and floppy images can be copied from $SHARE_DIR/" >&2
		exit 1
	fi
	if ! [ -w "$FILE" ]; then
		echo "$FILE is not writeable" >&2
		exit 1
	fi
}

print_version () {
    cat << EOF
8086tiny $VERSION
Copyright (c) 2013-2014 Adrian Cable - http://www.megalith.co.uk/8086tiny

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
EOF
}

print_help () {
    cat << EOF
Usage: $0 [OPTIONS] [[bios-image-file floppy-image-file [@][harddisk-image-file]]]
8086tiny is a tiny, free, open source, portable Intel PC emulator/VM, powerful
enough to run DOS, Windows 3.0, Excel, MS Flight Simulator, AutoCAD,
Lotus 1-2-3, and similar applications.

    -h, --help              display this help and exit
    -n, --no-graphics       disable graphics and audio support
    -s, --slow-cpu          increase the graphics emulation frame rate
    -v, --version           output version information and exit

    If bios-image-file and floppy-image-file are not specified, 'bios.bin' and
    'fd.img' are used as defaults. These can be copied from $SHARE_DIR/.

    If harddisk-image-file is prefixed with @ then 8086tiny will boot from the
    hard disk image. Otherwise, 8086tiny will boot from the floppy disk image.

    Full documentation at: </usr/share/doc/8086tiny/doc.html>
EOF
}

print_usage () {
    cat << EOF >> /dev/null
EOF
	exit 1
}

cleanup () {
	stty cooked echo
    echo
    exit
}

if ! TEMP="$(getopt -o hnsv --long help,no-graphics,slow-cpu,version -- "$@")" ; then
	print_usage
fi

eval set -- "$TEMP"

while true; do
	case "$1" in
		-n | --no-graphics )
			NO_GRAPHICS=true
			shift
		;;
		-s | --slow-cpu )
			SLOW_CPU=true
			shift
		;;
        -v | --version )
            print_version
            exit 0
        ;;
        -h | --help )
            print_help
            exit 0
        ;;
		* )
			break
		;;
	esac
done

shift

if [ "$#" -eq 2 ] || [ "$#" -eq 3 ]; then
	BIOS_IMAGE="$1"
	FLOPPY_IMAGE="$2"
elif [ "$#" -ne 0 ]; then
	echo "Invalid number of arguments" >&2
	print_usage
fi

check_write "$FLOPPY_IMAGE"
check_write "$BIOS_IMAGE"

if [ "$#" -eq 3 ]; then
	HARDDISK_IMAGE="$3"
	check_write "$( echo "$HARDDISK_IMAGE" | tr -d '@' )"
fi

if [ "$NO_GRAPHICS" = "false" ] && [ "$SLOW_CPU" = "false" ]; then
	BIN="$LIB_DIR/8086tiny"
elif [ "$NO_GRAPHICS" = "true" ] && [ "$SLOW_CPU" = "false" ]; then
	BIN="$LIB_DIR/8086tiny_ng"
elif [ "$SLOW_CPU" = "true" ] && [ "$NO_GRAPHICS" = "false" ]; then
	BIN="$LIB_DIR/8086tiny_slow"
else
	BIN="$LIB_DIR/8086tiny_slow_ng"
fi

clear

trap cleanup INT TERM
stty cbreak raw -echo min 0
if [ -z "$HARDDISK_IMAGE" ]; then
	"$BIN" "$BIOS_IMAGE" "$FLOPPY_IMAGE"
else
	"$BIN" "$BIOS_IMAGE" "$FLOPPY_IMAGE" "$HARDDISK_IMAGE"
fi

cleanup
