#!/bin/bash

E1=E
E2=("PLAY QUAKE")

function play_quake {

    X11_GEOM=`echo "$X11_GEOM" | sed -e "s|x| |"`
    [ "$TMP_SOUND" = "off" ] && SOUND="-nosound" || SOUND=

    cd $QUAKEHOMEDIR

    if [ -z "$DISPLAY" ]; then

	if [ "$Q1_RMODE" = "gl" ] ; then

	    case "$Q1GAME" in
		qw) LD_PRELOAD=$Q1_DRV \
		    $QUAKEBINDIR/glqwcl -height ${CONHEIGHT:=320} \
					-width ${CONWIDTH:=320} \
					-nocdaudio \
					-sndspeed 44100 \
					$SOUND
		    ;;
		qf) msgbox "Sorry, no GL-Quakeforge for console."
		    ;;
		*)  $QUAKEBINDIR/glquake $Q1GAME \
					 $SOUND
		    ;;
	    esac

	elif [ "$Q1_RMODE" = "soft" ] ; then

	    case "$Q1GAME" in
		qw) $QUAKEBINDIR/qwcl -sndspeed 44100 \
				      -nocdaudio \
				      -mode $MODE \
				      $SOUND
		;;
		qf) backup_conf --force /etc/quakeforge.conf

		    sed -e "s|.*setrom fs_userpath .*|setrom fs_userpath \"$QUAKEHOMEDIR\"|g" < /etc/quakeforge.conf > /etc/quakeforge.conf.tmp

		    mv /etc/quakeforge.conf.tmp /etc/quakeforge.conf

		    $QUAKEBINDIR/qf-client-svga -sndspeed 44100 \
						-mode $MODE \
						$SOUND

		    restore_conf /etc/quakeforge.conf
		    ;;
		*)  if [ -f $QUAKEBINDIR/squake ]; then

			$QUAKEBINDIR/squake -nostdout \
					    -sndspeed 44100 \
					    -nocdaudio \
					    -mode $MODE \
					    $Q1GAME \
					    $SOUND
		    fi
		    ;;
	    esac

	fi

	hit_enter

    else

	if [ "$Q1_RMODE" = "gl" ] ; then

	    case "$Q1GAME" in
		qw) $QUAKEBINDIR/glqwcl.glx -conwidth ${CONWIDTH:=320} \
					    -sndspeed 44100 \
					    -nocdaudio \
					    $SOUND
		    ;;
		qf) backup_conf --force /etc/quakeforge.conf

		    sed -e "s|.*setrom fs_userpath .*|setrom fs_userpath \"$QUAKEHOMEDIR\"|g" < /etc/quakeforge.conf > /etc/quakeforge.conf.tmp

		    mv /etc/quakeforge.conf.tmp /etc/quakeforge.conf

		    $QUAKEBINDIR/qf-client-glx  -conwidth ${CONWIDTH:=320} \
						-sndspeed 44100 \
						-nocdaudio \
						$SOUND

		    restore_conf /etc/quakeforge.conf
		    ;;
		*)  $QUAKEBINDIR/glquake.glx -conwidth ${CONWIDTH:=320} \
					    -sndspeed 44100 \
					    -nocdaudio \
					    $Q1GAME \
					    $SOUND
		    ;;
	    esac

	elif [ "$Q1_RMODE" = "soft" ] ; then

	    case "$Q1GAME" in

		qw) $QUAKEBINDIR/qwcl.x11 -sndspeed 44100 \
					  -nocdaudio \
					  -winsize ${X11_GEOM:="320 200"} \
					  $SOUND
		    ;;
		qf) backup_conf --force /etc/quakeforge.conf

		    sed -e "s|.*setrom fs_userpath .*|setrom fs_userpath \"$QUAKEHOMEDIR\"|g" < /etc/quakeforge.conf > /etc/quakeforge.conf.tmp

		    mv /etc/quakeforge.conf.tmp /etc/quakeforge.conf

		    $QUAKEBINDIR/qf-client-x11 -sndspeed 44100 \
					       -nocdaudio \
					       $SOUND

		    restore_conf /etc/quakeforge.conf
		    ;;
		*)  if [ -f $QUAKEBINDIR/quake.x11 ]; then

			$QUAKEBINDIR/quake.x11 -nostdout \
					       -sndspeed 44100 \
					       -nocdaudio \
					       -winsize ${X11_GEOM:="320 200"} \
					       $Q1GAME \
					       $SOUND

		    elif [ -f $QUAKEBINDIR/xf86quake ]; then

			$QUAKEBINDIR/xf86quake -nostdout \
					       -sndspeed 44100 \
					       -nocdaudio \
					       $Q1GAME \
					       $SOUND

		    elif [ -f $QUAKEBINDIR/xquake ]; then

		    	$QUAKEBINDIR/xquake -nostdout \
					    -sndspeed 44100 \
					    -nocdaudio \
					    $Q1GAME \
					    $SOUND

		    fi
		    ;;

	    esac

	    xset r on

	fi

    fi
}
