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

CFG_SAVE="$ADMIN_CFG_FILE.save"

rm -rf $ADMIN_EXEC_SCRIPT > /dev/null 2>&1
echo "#!/bin/sh" > $ADMIN_EXEC_SCRIPT

if [ -s $CFG_SAVE ] && [ "$1" != "-f" ] ; then
   ALL_SCRIPTS=`diff $ADMIN_CFG_FILE $CFG_SAVE | grep "^< /" | cut -b 3- | grep -v "/cfgvar.sh:" | grep -v "/cfgupd.sh:" | cut -d ":" -f 1 | sort`
else
   ALL_SCRIPTS=`cat $ADMIN_CFG_FILE | grep "^/" | grep -v "/cfgvar.sh:" | grep -v "/cfgupd.sh:" | cut -d ":" -f 1 | sort`
fi

LAST_SCRIPT=""
echo "sh ${ADMIN_SCRIPT_PATH}/cfgvar.sh 2>&1 >> ${ADMIN_LOG_FILE}" >> $ADMIN_EXEC_SCRIPT
echo "sh ${ADMIN_SCRIPT_PATH}/cfgupd.sh 2>&1 >> ${ADMIN_LOG_FILE}" >> $ADMIN_EXEC_SCRIPT

for i in $ALL_SCRIPTS ; do
   if [ "${i}" != "$LAST_SCRIPT" ] ; then
      if [ -f ${i} ] ; then
         echo "sh ${i} 2>&1 >> $ADMIN_LOG_FILE" >> $ADMIN_EXEC_SCRIPT
      else
         echo "<${i}> not found" >> $ADMIN_EXEC_SCRIPT
         echo "<${i}> not found"
      fi
   fi
   LAST_SCRIPT=${i}
done
echo "cp -f $ADMIN_CFG_FILE $CFG_SAVE" >> $ADMIN_EXEC_SCRIPT
echo "depscan.sh >/dev/null 2>&1" >> $ADMIN_EXEC_SCRIPT
echo "rm -f $ADMIN_EXEC_SCRIPT >/dev/null 2>&1" >> $ADMIN_EXEC_SCRIPT
chmod +x $ADMIN_EXEC_SCRIPT
if [ "$1" = "-f" ] ; then
   sh $ADMIN_EXEC_SCRIPT
fi
