#!/bin/bash

# Default username
USERNAME="linvdr"

# Default password
PASSWORD="linvdr"

# global configuration file of the vdradmin-daemon
# This file doesn't exist by default.
# The username and password are stored in it and will be read by the script if it exists.
# If it is not readable the userconfig (~/vdradmin.conf) will be used instead or the user will be asked for username/password.
# If it doesn't exist the userconfig will be used or (if this also doesn't exist) the default-values will be used.
CONFIGFILE="/var/lib/vdradmin/vdradmind.conf"

# user configuration file with username/password to access vdradmin
# This file doesn't exist by default.
# The username and password are stored in it and will be read by the script if it exists.
# It will only be used if the global configuration doesn't exist or is not readable.
# To create a user configuration file run this script with the parameter --create-userconfig
USERCONFIG="$HOME/vdradmin.conf"

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

# Functions
function start_vdradmind_if_needed()
{
    # start vdradmind if it is:
    #  - not already running
    #  - installed (/etc/init.d/vdradmin-am exists)
    #  - $IP is "localhost" or "127.0.0.1"
    
    ps --pid $(</var/run/vdradmin.pid) &>/dev/null && return    # already runnnig
    [ ! -x "/etc/init.d/vdradmin-am" ] && return                # not installed
    [ "$IP" != "localhost" -a "$IP" != "127.0.0.1" ] && return  # not local
    
    Xdialog --yesno $"The VDRAdmin-Daemon is not running on your computer...\nDo you want to start it now?" 0 0 || return    # user doesn't want to start vdradmind
    
    kanotix-su /etc/init.d/vdradmin-am start
}

DIALOG=Xdialog

# get IP
IP=$(head -1 $HOME/vdrip.conf)
[ -z "$IP" ] && IP="localhost"

# parse parameters
case $1 in
--create-userconfig|--config)
    echo $"Warning: Password will be echoed and stored unencrypted in $USERCONFIG!"
    echo -n $"VDRAdmin Username: "
    read USERNAME
    echo -n $"VDRAdmin Password: "
    read PASSWORD
    [ -z "$USERNAME" -o -z "$PASSWORD" ] && exit
    echo "USERNAME = $USERNAME" > $USERCONFIG
    echo "PASSWORD = $PASSWORD" >> $USERCONFIG
    echo $"Your username/password has been written to $USERCONFIG"
    chmod 600 $USERCONFIG
    exit
    ;;
esac

[ ! -r "$CONFIGFILE" -a -r "$USERCONFIG" ] && CONFIGFILE="$USERCONFIG"

if [ -r "$CONFIGFILE" ]; then
    # configfile is readable, Username and Password are extracted from it.
    user=$(grep ^USERNAME "$CONFIGFILE" | cut -d= -f2)
    pass=$(grep ^PASSWORD "$CONFIGFILE" | cut -d= -f2)
    if [ "$user" -a "$pass" ]; then
        USERNAME=$(echo $user)
        PASSWORD=$(echo $pass)
    fi
elif [ -f "$CONFIGFILE" ]; then
   # configfile is not readable, but exists, so the user has to give Username and Password.
   AUTH="$(Xdialog --separator ":" --password --2inputsbox $"VDRAdmin" 0 0 $"VDRAdmin Username" "$USERNAME" $"VDRAdmin Password" "$PASSWORD" 2>&1)"
   [ -z "$AUTH" ] && exit
   start_vdradmind_if_needed
   x-www-browser "http://${AUTH}@$IP:8001/" &
   exit
fi

start_vdradmind_if_needed
x-www-browser "http://${USERNAME}:${PASSWORD}@$IP:8001/" &
