#!/bin/bash
source /etc/conf.d/vdr.admin.cfg

VDR_CONF="/etc/conf.d/vdr"

ALL_VARS=$(grep "^/etc" $ADMIN_CFG_FILE | cut -f 2 -d ":" | grep -v "^PLG_")

for i in $ALL_VARS ; do
   VAL=$($GETVAL ${i})
   VE=$(grep -m 1 "^$i=" $VDR_CONF | cut -f 2 -d '"')
#   logger -s "ADMIN - ${i}: ${VAL}-${VE} "
   if [ "$VE" != "$VAL" ] ; then
      logger -s "Changing ${i} (\"${VAL}\") to \"${VE}\" in $ADMIN_CFG_FILE"
      NLINE=$(grep -m 1 ":${i}:" $ADMIN_CFG_FILE)
      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 j in $ALL_VAL ; do
            if [ "${j}" = "$VE" ] ; then
             	 VE=$IDX
             	 break
            fi
            IDX=$(($IDX+1))
         done
      fi
      sed -i $ADMIN_CFG_FILE -e "s/\:${i}\:${OLDVAL}\:/\:${i}\:${VE}\:/"
      set +x
   fi
done

# And now the Plugins
#set -x
TMP_FILE="/tmp/~plgconf"

rm -f $TMP_FILE > /dev/null 2>&1
USE_PLG=$(grep "^PLUGINS=" $VDR_CONF |cut -f 2 -d '"')
NUM_PLG=$(echo "${USE_PLG}"|wc -w)
PLG_SCR="$ADMIN_SCRIPT_PATH/cfgplg.sh"

for i in $USE_PLG ; do
   if [ $NUM_PLG -ge 10 ] ; then
      ASC_NUM=$NUM_PLG
   else
      ASC_NUM="0${NUM_PLG}"
   fi   
   echo "${PLG_SCR}:PLG_${i}:${ASC_NUM}:I:2:0,99:Plugin ${i}:" >> $TMP_FILE
   NUM_PLG=$(($NUM_PLG - 1))
done

sh $PLG_SCR $TMP_FILE

echo 'Done!'
exit 0

