#!/bin/bash

# global configuration file
# This file exists by default.
# Settings for the Kanotix-VDR-Scripts and for the Kanotix-VDR-Startmenu are stored in it.
# List of the variables that are available:
#     LanguageMode:       0 = Autoselect Language, 1 = Select Language manually (via VDR-OSD)
CONFIGFILE="/var/lib/vdr/kanotix-vdr/kanotix-vdr.conf"

# user configuration file
# This file doesn't exist by default, but it will be created by this script if it is needed.
# Settings for the Xine-Client or for the Kanotix-VDR-Startmenu are stored in it.
# List of the variables that are available:
#     XineFullscreen:       0 = Normal Mode, 1 = Fullscreen
USERCONFIG="$HOME/.kanotix-vdr.conf"

#########################################################################################
# Localisation
export TEXTDOMAINDIR="/usr/share/locale"
export TEXTDOMAIN=kanotix-vdr

# Functions
function dlg()
{
   DIALOG="Xdialog"
   # reset Variables
   ARG=; PARAMETERS=;
   if [ $# -eq 2 ]; then
      case "$1" in
      --msgbox|--yesno|--infobox|--inputbox|--textbox|--gauge)
         ARG="0 0"
         ;;
      esac
   fi
   # escape Parameters
   PARAMETERS="\"$1\""
   while shift; do
      (($#)) && PARAMETERS="$PARAMETERS \"$1\""
   done
   output="$(eval "$DIALOG --title 'Kanotix-VDR' $PARAMETERS $ARG 3>&1 1>&2 2>&3 ")"
   rc=$?
   echo "$output"
   return $rc
}

function run_as_root()
{
    # escape Parameters
    PARAMETERS="\"$1\""
    while shift; do
       (($#)) && PARAMETERS="$PARAMETERS \"$1\""
    done
    output="$(eval "kanotix-su \"$0\" $PARAMETERS")"
    rc=$?
    echo "$output"
    return $rc
}

function select_language()
{
    mode=$(grep ^LanguageMode $CONFIGFILE | cut -d= -f2)
    [ "$mode" ] && old=$(echo $mode)
    [ -z "$mode" ] && old=0
    new=$(dlg --default-item $old --menu $"Configure VDR-language:" 0 60 8 \
    0 $"Set VDR-language automatically" \
    1 $"Manually define VDR-language (see VDR-OSD-Menu)")
    if [ $? -eq 0 -a "$old" != "$new" ]; then
        if [ -z "$mode" ]; then
            echo "LanguageMode = $new" >> $CONFIGFILE
        else
            perl -pi -e "s/^LanguageMode =.*$/LanguageMode = $new/" $CONFIGFILE
        fi
    fi
}

function select_fullscreen_mode()
{
    mode=$(grep ^XineFullscreen $USERCONFIG | cut -d= -f2)
    [ "$mode" ] && old=$(echo $mode)
    [ -z "$mode" ] && old=0
    new=$(dlg --default-item $old --menu $"Configure VDR-Xine:" 0 60 8 \
    0 $"Start VDR-Xine in Normal mode" \
    1 $"Start VDR-Xine in Fullscreen mode")
    if [ $? -eq 0 -a "$old" != "$new" ]; then
        if [ -z "$mode" ]; then
            echo "XineFullscreen = $new" >> $USERCONFIG
        else
            perl -pi -e "s/^XineFullscreen =.*$/XineFullscreen = $new/" $USERCONFIG
        fi
    fi
}

# Parameters
if (($#)); then
    while (($#)); do
        case $1 in
        "select_language")
            select_language
            ;;
        "select_fullscreen_mode")
            select_fullscreen_mode
            ;;
        esac
        shift
    done
    exit
fi

# Main menu
while true
do
    if [ -e /var/lib/vdr/setup.conf -a -e /usr/bin/vdr ]; then
        # full Kanotix-VDR on this system
        selection=$(dlg --menu $"VDR-Configuration:" 0 60 8 \
            "" $"VDR-Xine configuration" \
            "1.1)" $"Fullscreen mode" \
            "" $"VDR-Server configuration" \
            "2.1)" $"VDR Language" \
            "" "" \
            "0)" $"Exit Configuration")
    else
        # only a Xine-Client on this system
        selection=$(dlg --menu $"VDR-Configuration:" 0 60 8 \
            "" $"VDR-Xine configuration" \
            "1.1)" $"Fullscreen mode" \
            "" "" \
            "0)" $"Exit Configuration")
    fi
    
    case $selection in
    "0)"|"")
        exit
        ;;
    "1.1)")
        select_fullscreen_mode
        ;;
    "2.1)")
        run_as_root select_language
        ;;
    esac
done

