#!/bin/sh
source /etc/conf.d/vdr.admin.cfg
VDR_CONF="/etc/conf.d/vdr"

VAL=$2
NAME=$1

NLINE=$(grep -m 1 ":$NAME:" $ADMIN_CFG_FILE)

if [ "$NLINE" = "" ] ; then
   echo "AdmVar: <$NAME> not found"
   exit 1
fi

VE=$(grep "^${NAME}=" $VDR_CONF)
if [ "$VE" = "" ] ; then
   logger -s "Adding ${NAME}=\"${VAL}\" to $VDR_CONF"
   echo "${NAME}=\"${VAL}\"" >> $VDR_CONF
else
   logger -s "Changing ${NAME}=\"${VAL}\" in $VDR_CONF"
   sed -i $VDR_CONF -e "s/^${NAME}=.*/${NAME}=\"${VAL}\"/"
fi

TYPE=$(echo $NLINE | cut -f 4 -d ":")
OLDVAL=$(echo $NLINE | cut -f 3 -d ":")
if [ "$TYPE" = "L" ] ; then
   ALL_VAL=$(echo $NLINE | cut -f 6 -d ":" | sed 's/,/ /g')
   IDX=0
   for i in $ALL_VAL ; do
      if [ "${i}" = "$VAL" ] ; then
       	 VAL=$IDX
      fi
      IDX=$(($IDX+1))
   done
fi

sed -i $ADMIN_CFG_FILE -e "s/\:$NAME\:$OLDVAL\:/\:$NAME\:$VAL\:/"
