#!/bin/sh
. /etc/profile
. /etc/systemconfig

if [ -e /etc/X11/WINDOWMANAGER ]
then
    . /etc/X11/WINDOWMANAGER
fi

xhost +local:
xhost +127.0.0.1
xset -dpms s off

export MALLOC_CHECK_=0



# # Run any scripting that applies to special hardware.  These brand-specific commands that run
# # after Tweaker will, of course, override generic or hardware-specific settings.
# SpecialHardwareCommands() {
#     special_hardware_file=/myth/.special_hardware_type
#     special_hardware=`cat $special_hardware_file`
#
#     case $special_hardware in
#         dragon*)
#             sudo -E twk_audio.pl --implement digital
#             sudo -E twk_dragon.pl --implement all
#             ;;
#         ausdragon*)
#             ;;
#         *)
#             # there are no brand-specific commands for configuring whatever
#             # special hardware the user chose
#             ;;
#     esac
# }



. /etc/osd_cat.cfg || {
        color=yellow
        outline=2
        outlinecolour=black
        shadow=0
        shadowcolour=black
        font="-adobe-helvetica-bold-*-*-*-34-*-*-*-*-*-*-*"
}

function msg(){
    echo -e "$1"  | osd_cat --pos=middle --align=center --offset=200 --delay=5 --color=$color --outline=$outline --outlinecolour=$outlinecolour --shadow=$shadow --shadowcolour=$shadowcolour --font=$font &
}


function unclutter(){
    if [ -e /etc/unclutter ]
    then
        . /etc/unclutter.cfg
    else
        idle=1
        jitter=200
    fi

    /usr/X11R6/bin/unclutter -root -idle $idle -jitter $jitter -noevents  &
}

function keylaunch(){
    if [ -f /usr/bin/keylaunch ]
    then
#       MHOME=`cat /etc/passwd | grep "mythtv" | cut -d":" -f6`
       if [ ! -e $HOME/.keylaunchrc ]
       then
          echo "setting symlink for keylaunch"
          ln -s /etc/keylaunchrc $HOME/.keylaunchrc
       fi
       /usr/bin/keylaunch &
    fi

}


function load_nvidia_settings(){
    lsmod |cut -f1 -d" " |grep -q nvidia
    if [ $? = 0 ]
    then
        /usr/bin/nvidia-settings --load-config-only
    fi
}

function run_tilda(){


    if [ -f /usr/bin/tilda ]
    then
        if [ ! -e  ~/.tilda/config_0 ]
        then
            mkdir -p ~/.tilda/
        fi

        width=`/usr/bin/xwininfo -root|grep Width | awk '{ print $2}'`
        height=`/usr/bin/xwininfo -root|grep Height | awk '{ print $2}'`
	center=$[width/2]
        sixtypercent=`echo "scale=0; ( $width*.6) / 1" |bc`
        quarter_height=`echo "scale=0; ($height*.35) /1" |bc`
	

	if [ $sixtypercent -lt 700 ] 
	then
             sixtypercent=700
        fi

	if [ $quarter_height -lt 350 ] 
	then
             quarter_height=350
        fi
        startloc=`echo "scale=0 ; (${center}-(${sixtypercent})/2) / 1" |bc`


        sed -e "s/^max_width.*$/max_width = $sixtypercent/" \
            -e "s/^max_height.*$/max_height = $quarter_height/" \
            -e "s/^x_pos.*$/x_pos = $startloc/g" /etc/tilda_config > ~/.tilda/config_0

        /usr/bin/tilda -h &

    fi
}


function mouse_move(){
    #move mouse to corner
    xdotool mousemove 0 0
}


function start_screensaver(){

    if [ x"$Screensavertype" = "xgscreensaver" ]
    then
        gnome-screensaver &
    elif  [ x"$Screensavertype" = "xxscreensaver" ]
    then
        xscreensaver  -no-splash &
    fi
}

function show_help_tip(){

    if [ x$ShowTips = "x1" ]
    then
        msg "Alt+h for help" &
    fi
}

function update_apple_trailers(){
    #update apple trailers
    if [[ x$RunFrontend = x1 && -f ~/.configure ]]
    then
        /usr/bin/php -q /usr/bin/myth_trailers_grabber > /home/mythtv/appletrailer.xml && /usr/bin/fix_aple_url.sh 2>/dev/null &
    fi
}


function first_configure(){
    if [ x$STARTUP_STYLE = xmythvantage ]
    then
        first_configure_mv
    else
        first_configure_default
    fi
}


function first_configure_default(){
    if [ !  $SystemType = "Frontend_only" ]
    then
        if [  -f ~/.configure ]
        then
            # Run Tweaker, which applies generic and hardware-specific settings
            if [ ! -f ~/.no_meth ]
            then
                #remove tweaker cause it's broken and duplicates entries
                #sudo -E tweaker.pl
                #/bin/rm /tmp/fps # ??? clean up a temp file that will eventually be obsolete anyway
                # Run any brand-specific commands, if necessary.
                SpecialHardwareCommands
            else
                rm -f ~/.no_meth
            fi

        sudo sv stop mythbackend
        /usr/LH/bin/dtheme.sh
        xterm -fn *18* -e sudo taskset -c 0 /usr/bin/mythtv-setup
        sudo sv start mythbackend
        sudo chown -R mythtv.mythtv /home/mythtv
        nice -n 19 mythfilldatabase --quiet &
        msg "Guide data is being loaded. \n Until this completes\n some shows will appear as unknown \n in the program guide."
        rm ~/.configure
        fi
    fi
}


function first_configure_mv(){
    if [ !  $SystemType = "Frontend_only" ]
    then
    if [ ! -f ~/.configured ]
            then
            autocard.py -g &
            MythVantage -b
            #/root/autocard.py  -g &
            #/usr/local/bin/mythinstall -b

    #0 yes
    #1 yes/autodetech
    #2  no
            STATUS=$?
            if [ ! $STATUS = 2 ]
            then
                    /usr/bin/backend_control.sh stop BE 127.0.0.1
                    /usr/local/bin/mythtv-setup
                    /usr/bin/backend_control.sh start BE 127.0.0.1
            fi
            touch  ~/.configured
            fi
    fi
}

function start_myth() {
    if [ x"$UseMythWelcome" = "x1" ]
    then
        STARTCMD="/usr/bin/mythwelcome"
    else
        STARTCMD="/usr/bin/mythfrontend -d -l /var/log/mythtv/mythfrontend.log"
    fi

    echo "====================================================================="
    echo "Starting $STARTCMD"
    firstboot.sh &

    #continue on with starting mythtv

    if [ x$STARTUP_STYLE = xmythvantage ]
    then
        start_myth_mv
    else
        start_myth_default
    fi

    }


function start_myth_default(){

    if [ -f /tmp/nomfe ]
    then
        exit 0
    else
        if [ -f /tmp/debug ]
        then
            echo "using gbd/debug"
            gdb /usr/bin/mythfrontend -x $MV_ROOT/mfegdbcommands.txt
            mv gdb.txt gdb.txt.mythfrontend
            sleep 10
        else
            killall -9 irexec
            /usr/bin/irexec -d
            $STARTCMD 2>&1 &
        fi
    fi




}


function start_myth_mv(){

    while [ x = x ]
    do
            if [ -f /tmp/nomfe ]
            then
                    sleep 120
            else
                    if [ -f /tmp/debug ]
                    then

                            echo "using gbd/debug"
                            gdb /usr/local/bin/mythfrontend -x $MV_ROOT/mfegdbcommands.txt
                            bash -c "osdClient.pl localhost 5000 'Restart: mdb.txt.mythfrontend:::SYS' "&
                            mv gdb.txt gdb.txt.mythfrontend
                            sleep 10

                    else
                            killall -9 irexec
                            /usr/bin/irexec &
                            $STARTCMD 2>&1
                    fi
            fi
    echo "_______________________________________________________"
    done


    }


function set_background()
{
    echo "Setting the background"

    }


if [ x$WM = "x" ]
then
	fluxbox > /var/log/fluxbox.log 2>&1

else
    #if using mythvantage style, start the WM right away
    # this is done so that starting mythtv can loop.

    if [ x$STARTUP_STYLE = xmythvantage ]
    then
        $WM &
    fi
    mouse_move
    keylaunch
    set_background
    unclutter
    load_nvidia_settings
    run_tilda
    start_screensaver
    show_help_tip
    #update_apple_trailers
    first_configure
    start_myth

    # start windowmanger
    if [ x$STARTUP_STYLE != xmythvantage ]
    then
        $WM
    fi

fi