#!/bin/sh

#
# This script is a mix from the mplayer TVout scripts and some
# changes by me to support 768 width
#
# Note: fbset and matroxset are copied from mplayer! They're 
# included here for ease of use, but check out the mplayer
# source for updates if you have problems. There are more
# init scripts there, setup utilities, etc.
#
# /Dischi


# possible locations for matroxset and fbset if they are not in the path
# and not in the runtime
export PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin

# find the freevo script
if [ -x "$FREEVO_SCRIPT" ]; then
    freevo=$FREEVO_SCRIPT
elif [ -e "`dirname $0`/../freevo" ]; then
    freevo=`dirname $0`/../freevo
elif which --skip-functions --skip-alias freevo >/dev/null 2>/dev/null; then
    freevo=`which --skip-functions --skip-alias freevo`
else
    echo freevo script not found
    exit 1
fi

if [ "$1" == "restore" ]; then
    if [ -c /dev/fb0 ]; then
	HEAD0=/dev/fb0
	HEAD1=/dev/fb1
    else
	HEAD0=/dev/fb/0
	HEAD1=/dev/fb/1
    fi

    #$freevo runapp matroxset -f ${HEAD1} -m 0
    #$freevo runapp matroxset -f ${HEAD0} -m 1
    #$freevo runapp matroxset -f ${HEAD1} -m 2

    $freevo runapp matroxset -f /dev/fb0 -m 1
    $freevo runapp matroxset -f /dev/fb1 -m 0

    if [ "$2" != "" ]; then
	shift
	$freevo runapp fbset -a -fb /dev/fb0 "$@"
    fi
    exit 0
fi

if ! [ "$1" == "pal" ] && ! [ "$1" == "PAL" ] && \
    ! [ "$1" == "ntsc" ] && ! [ "$1" == "NTSC" ]; then
    echo "usage: mgafb [ pal | ntsc ] resolution"
    echo "       mgafb restore"
    exit 1
fi


# I don't know what this is doing exacly, but it should enable the
# clone mode with IMHO looks better than the others

$freevo runapp matroxset -f /dev/fb1 -m 0
$freevo runapp matroxset -f /dev/fb0 -m 3

# set the correct fbset mode
if [ "$1" == "pal" ] || [ "$1" == "PAL" ]; then
    # switch to PAL
    $freevo runapp matroxset 1

    if [ "$2" == "768x576" ]; then
	$freevo runapp fbset -fb /dev/fb0 -depth 32 -left 20 -right 10 -hslen 34 \
	    -upper 30 -lower 10 -vslen 10 -xres 768 -yres 576 \
	    -vxres 768 -vyres 576 -depth 32 -laced false -bcast true -pixclock 38400 \
	    -hsync high -vsync high

# 	$freevo runapp fbset -fb /dev/fb0 -depth 32 -left 23 -right -5 \
# 	    -upper 39 -lower 10 -hslen 46 -vslen 4 -xres 768 -yres 576 \
# 	    -vxres 768 -vyres 576 -depth 32 -laced false -bcast true

    elif [ "$2" == "800x600" ]; then
	$freevo runapp fbset -fb /dev/fb0 -depth 32 -left 48 -right 24 \
	    -upper 70 -lower 32 -hslen 40 -vslen 2 -xres 800 -yres 600 \
	    -vxres 800 -vyres 600 -depth 32 -laced false -bcast true

    else
	echo unsupported mode $2 for pal
    fi


else
    $freevo runapp matroxset -f /dev/fb0 2 2
    db=`dirname $0`/fbset.db
    if [ "$2" == "640x480" ]; then
	$freevo runapp fbset -db $db -fb /dev/fb0 "ntsc-640x480_60"
    elif [ "$2" == "768x576" ]; then
	$freevo runapp fbset -db $db -fb /dev/fb0 "ntsc-768x576"
    elif [ "$2" == "800x600" ]; then
	$freevo runapp fbset -db $db -fb /dev/fb0 "ntsc-800x600"
    else
	echo unsupported mode $2 for ntsc
    fi
fi


setterm -blank 0
