#!/bin/bash
# Copyright (c) 1996-2003 SuSE Linux AG, Nuernberg, Germany.
#
#                             Toplevel Emu-Install
#
# Authors: Richard Jelinek <rj@suse.de> 1997
#          Michal Cihar <mcihar@suse.cz> 2002-2003
#

VERSION="0.8"

eval `locale|grep "LC_MESSAGES="`

if [ -z "$LC_MESSAGES" ] ; then
    LC_MESSAGES=english
fi

if ( echo "$LC_MESSAGES" | grep -q _ ) ; then
    LC_MESSAGES=`echo "$LC_MESSAGES"  | sed s@_.*@@`
fi

if [ "$LC_MESSAGES" = "czech" -o "$LC_MESSAGES" = "cs" ] ; then
    ERR="### CHYBA: "
    ERR_DSPT=$ERR"musi bt zadno -i nebo -p nebo -d."
    ERR_MTOL=$ERR"nejsou nainstalovny mtools."
    ERR_ROOT=$ERR"Pouze root me spoutt tento script."
    ERR_UNKN=$ERR"neznm argument "
    ERR_XWIN=$ERR"mus bt nainstalovny X Windows."
    ERR_DISK=$ERR"patn jmno disku "
    ERR_NIN="nen nainstalovn."
    MSG_DNE="Instalace hotova."
    MSG_NSYS="Pro tento emultor nen teba instalovat ROM-image."
    MSG_NYI="Stahovn z internetu pro tento emultor dosud neimplementovno."
    MSG_ROM="Instaluji ROM image pro emultor: "
    MSG_SFT="Instaluji software pro emultor: "
    MSG_NF=" nenalezen."
    MSG_DS1="Prosm, vlote disk s "
    MSG_DS2="a stisknte <Enter>."
    MSG_DS3="do"
    MSG_FET="Stahuji data z : "
elif [ "$LC_MESSAGES" = "german" -o "$LC_MESSAGES" = "de" ]; then
    ERR="### FEHLER: "
    ERR_DSPT=$ERR"kein DISK oder PATH angegeben."
    ERR_MTOL=$ERR"keine mtools gefunden. Werden fr diese Operation bentigt."
    ERR_ROOT=$ERR"Nur root darf dieses Skript benutzen."
    ERR_UNKN=$ERR"unbekanntes Argument "
    ERR_XWIN=$ERR"Das X Window System mu installiert sein."
    ERR_DISK=$ERR"falsche Diskettename "
    ERR_NIN="ist nicht installiert."
    MSG_DNE="Installation fertig."
    MSG_NSYS="Kein System fr diesen Emulator."
    MSG_NYI="Funktion ist fr diesen Emulator nicht implemetiert."
    MSG_ROM="Installiere ROM-Datei fr Emulator: "
    MSG_SFT="Installiere Software fr Emulator: "
    MSG_NF=" nicht gefunden."
    MSG_DS1="Bitte Diskette mit "
    MSG_DS2="einlegen und <Enter> drcken."
    MSG_DS3="in"
    MSG_FET="Holen Sie Data von: "
else
    ERR="### ERROR: "
    ERR_DSPT=$ERR"-i or -p or -d has to be given."
    ERR_MTOL=$ERR"no mtools found. Needed for this operation."
    ERR_ROOT=$ERR"Only root may use this script."
    ERR_UNKN=$ERR"unknown argument "
    ERR_XWIN=$ERR"The X Window System has to be installed."
    ERR_DISK=$ERR"wrong diskname "
    ERR_NIN="not installed."
    MSG_DNE="Installation done."
    MSG_NSYS="The emulator doesn't need to install ROM-images."
    MSG_NYI="Fetching from internet not yet implemented for this emulator."
    MSG_ROM="Installing ROM image for emulator: "
    MSG_SFT="Installing software for emulator: "
    MSG_NF=" not found."
    MSG_DS1="Please insert disk with "
    MSG_DS2="and press <Enter>."
    MSG_DS3="in"
    MSG_FET="Fetching data from: "
fi

#
# How to use this script. Called when false arguments are given
#
function usage() {
if [ "$LC_MESSAGES" = "czech" -o "$LC_MESSAGES" = "cs" ] ; then
 echo "Emuinstall - instalan program pro rzn emultory pod linuxem"
 echo "      Copyright (c) 1996-2000 SuSE GmbH, Fuerth Germany."
 echo "            Napsal Richard Jelinek (rj@suse.de)"
 echo "                      Version $VERSION"
 echo
 echo "Pouit:"
 echo -e "ei [-h] {-i | -p PATH | -d DISK} EMULATOR"
 echo -e "  -d,--disk \tpouij ROM-image z diskety."
 echo -e "            \tDISK je bu a: nebo b:"
 echo -e "  -h,--help \ttento help"
 echo -e "  EMULATOR  \tkter emultor instalovat. Mon hodnoty :"
 echo -e "            \tadamem, coleco  \tColeco Vision/ADAM emultor"
 echo -e "            \tamiga, uae      \tCommodore Amiga emultor"
 echo -e "            \tatarist, stonx  \tAtari ST Emultor"
 echo -e "            \tatari800        \tAtari 800 Emultor"
 echo -e "            \tcommodore, vice \tC64/C128/VIC20/PET emultor"
 echo -e "            \tvcs2600, stella \tAtari VCS2600 emultor"
 echo -e "            \txzx, spectrum   \tZX Spectrum emultor"
 echo -e "            \tz81, zx81       \tZX81 emultor"
 echo -e "            \t"
 echo -e "  -i,--int  \tSthnout ROM-image pro dan emultor z internetu."
 echo -e "  -p,--path \tPATH je adres kde se hledaj ROM-images"

elif [ "$LC_MESSAGES" = "german" -o "$LC_MESSAGES" = "de" ]; then

 echo "Emuinstall - ein Installationsprogramm fr Emulatoren unter Linux"
 echo "         Copyright (c) 1996-2002 SuSE GmbH, D-90762 Frth"
 echo "               Autor:  Richard Jelinek (rj@suse.de)"
 echo "                            Version $VERSION"
 echo
 echo "Argumente:"
 echo -e "ei [-h] {-i |-p PFAD | -d DISK} EMULATOR"
 echo -e "  -d,--disk \tDiskette benutzen statt Pfadangabe."
 echo -e "            \tDISK ist entweder a: oder b:"
 echo -e "  -h,--help \tDiese Hilfe."
 echo -e "  EMULATOR  \tWelcher Emulator installiert werden soll. Mgliche"
 echo -e "            \tWerte sind:"
 echo -e "            \tadamem, coleco  \tColeco Vision/ADAM Emulator"
 echo -e "            \tamiga, uae      \tCommodore Amiga Emulator"
 echo -e "            \tatarist, stonx  \tAtari ST Emulator"
 echo -e "            \tatari800        \tAtari 800 Emulator"
 echo -e "            \tcommodore, vice \tC64/C128/VIC20/PET Emulator"
 echo -e "            \tvcs2600, stella \tAtari VCS2600 Emulator"
 echo -e "            \txzx, spectrum   \tZX Spectrum Emulator"
 echo -e "            \tz81, zx81       \tZX81 Emulator"
 echo -e "            \t"
 echo -e "  -i,--int  \tSoftware fr den betreffenden Emulator aus dem"
 echo -e "            \tInternet holen."
 echo -e "  -p,--path \tPFAD ist das Verzeichnis, wo nach den ROM-Dateien"
 echo -e "            \tgesucht wird."

else
 echo "Emuinstall - an installation program for various emulators under linux"
 echo "         Copyright (c) 1996-2002 SuSE GmbH, Fuerth Germany."
 echo "              Written by Richard Jelinek (rj@suse.de)"
 echo "                       This is Version $VERSION"
 echo
 echo "Usage:"
 echo -e "ei [-h] {-i | -p PATH | -d DISK} EMULATOR"
 echo -e "  -d,--disk \tuse ROM-image from disk."
 echo -e "            \tDISK is either a: or b:"
 echo -e "  -h,--help \tthis help"
 echo -e "  EMULATOR  \twhich emulator to install. Possible values:"
 echo -e "            \tadamem, coleco  \tColeco Vision/ADAM emulator"
 echo -e "            \tamiga, uae      \tCommodore Amiga emulator"
 echo -e "            \tatarist, stonx  \tAtari ST Emulator"
 echo -e "            \tatari800        \tAtari 800 Emulator"
 echo -e "            \tcommodore, vice \tC64/C128/VIC20/PET emulator"
 echo -e "            \tvcs2600, stella \tAtari VCS2600 emulator"
 echo -e "            \txzx, spectrum   \tZX Spectrum emulator"
 echo -e "            \tz81, zx81       \tZX81 emulator"
 echo -e "            \t"
 echo -e "  -i,--int  \tFetch ROM-image for the given Emulator from"
 echo -e "            \tthe Internet."
 echo -e "  -p,--path \tPATH is directory where to search for ROM-images"

fi
}

#
# Copy a certain image from EI_PATH to certain destination
#
function img_copy() {

   if [ -e "$EI_PATH/$1" ]; then
      cp -i $EI_PATH/$1 $2
   else
      echo $ERR$EI_PATH/$1$MSG_NF 1>&2
      exit 1
   fi

}

#
# Fetch Data from given URL
#
function fetch_data() {

    EI_URLSRC=$1; shift
    EI_URLDST=$1; shift
    echo $MSG_FET $EI_URLSRC
    while [ ! -z "$1" ]; do
	  echo -e $1 "\c"
	  if !  wget --progress=dot --passive-ftp $EI_URLSRC$1 -O $EI_URLDST$1 2>&1 | grep -E '^([[:alpha:]]*:[[:digit:]]*|[[:space:]]*[[:digit:]]*K)' ; then
	     echo
	     echo $ERR$EI_URLSRC$1$MSG_NF
	     exit 1
	  fi
	  shift
    done
    echo
}

#
# User has given no Arguments
#
if [ $# -eq 0 ]; then
    usage
    exit 1
fi

#
# set some needed variables to default
#
EI_DISK=""
EI_PATH=""
EI_INET=0

EI_ADAM=0
EI_STELLA=0
EI_STONX=0
EI_UAE=0
EI_VICE=0
EI_XZX=0
EI_Z81=0
EI_A800=0

#
# check the command line parameters
#
while [ ! -z "$1" ]; do
    case "$1" in
	    -d|--disk) shift
		       case "$1" in
		       "a:"|"b:") EI_DISK=$1;;
		       *)         echo "$ERR_DISK $1." 1>&2
				  usage
				  exit 1;;
		       esac
		       shift;;

	    -h|--help) usage; exit 1;;

	    -i|--int)  EI_INET=1
		       shift;;

	    -p|--path) shift
		       EI_PATH=$1
		       shift;;

	    *)         
		       case "$1" in
			    adamem|coleco)   EI_ADAM=1;;
			    atari800)        EI_A800=1;;
			    vcs2600|stella)  EI_STELLA=1;;
			    atarist|stonx)   EI_STONX=1;;
			    amiga|uae)       EI_UAE=1;;
			    commodore|vice)  
					     EI_PARAM=$1
					     EI_VICE=1;;
			    xzx|spectrum)    EI_XZX=1;;
			    z81| zx81)       EI_Z81=1;;
			    *)		     echo $ERR_UNKN "'$1'" 1>&2
					     usage	     
					     exit 1;;
			esac
		        shift;;
    esac
done

#
# Check if root is running the script
#
if [ ! `whoami` = 'root' ]; then
    echo $ERR_ROOT 1>&2
    exit 1
fi

#
# If no software Installation choosen -> Check Path
#
if [ $EI_INET -eq 0 ] && [ -z $EI_PATH ] && [ -z $EI_DISK ]; then
    echo $ERR_DSPT
    exit 1
fi

if [ $EI_ADAM -eq 1 ]; then
    if [ -r /usr/lib/ei/adamem ]; then
        . /usr/lib/ei/adamem
    else
        echo $ERR adamem $ERR_NIN
    fi
fi

if [ $EI_STELLA -eq 1 ]; then
    if [ -r /usr/lib/ei/stella ]; then
        . /usr/lib/ei/stella
    else
        echo $ERR stella $ERR_NIN
    fi
fi

if [ $EI_STONX -eq 1 ]; then
    if [ -r /usr/lib/ei/stonx ]; then
        . /usr/lib/ei/stonx
    else
        echo $ERR stonx $ERR_NIN
    fi
fi

if [ $EI_UAE -eq 1 ]; then
    if [ -r /usr/lib/ei/uae ]; then
        . /usr/lib/ei/uae
    else
        echo $ERR uae $ERR_NIN
    fi
fi

if [ $EI_VICE -eq 1 ]; then
    if [ -r /usr/lib/ei/vice ]; then
        . /usr/lib/ei/vice
    else
        echo $ERR vice $ERR_NIN
    fi
fi

if [ $EI_XZX -eq 1 ]; then
    if [ -r /usr/lib/ei/xzx ]; then
        . /usr/lib/ei/xzx
    else
        echo $ERR xzx-pro $ERR_NIN
    fi
fi

if [ $EI_Z81 -eq 1 ]; then
    if [ -r /usr/lib/ei/z81 ]; then
        . /usr/lib/ei/z81
    else
        echo $ERR z81 $ERR_NIN
    fi
fi

if [ $EI_A800 -eq 1 ]; then
    if [ -r /usr/lib/ei/atari800 ]; then
        . /usr/lib/ei/atari800
    else
        echo $ERR atari800 $ERR_NIN
    fi
fi
