#!/bin/sh
SDIR="/_config/backup"
MODCONF=""

set_autoload() {
   KV="$(uname -r)"
   KV_MAJOR=`uname -r | cut -f1 -d "."`
   KV_MINOR=`uname -r | cut -f2 -d "."`

   MODCONF="/etc/modules.autoload.d/kernel-${KV_MAJOR}.${KV_MINOR}"
   mv -f $MODCONF $MODCONF.org
   echo "natsemi" > $MODCONF
   echo "usb-storage" >> $MODCONF
}

echo ""
echo "mkcfg start"
cd /
chmod +x /_config/*/*.sh
chmod +x /_config/*/*/*.sh
chmod +x /etc/vdr/runvdr
chmod +x /_config/bin/*
chmod +x /etc/*/*.sh
chmod +x /etc/init.d/*
chmod +x /etc/*/*/*.sh
chmod +x /etc/*/*/*/*.sh
rm /etc/vdr/plugins/admin/~* > /dev/null 2>&1

EXCLUDES=""

if [ "$1" = "-i" ] ; then
   set_autoload
   echo "mkcfg -i"
   rm -f /_config/bin/insmod.sh > /dev/null 2>&1
   EXCLUDES="--exclude=./etc/resolv.conf* --exclude=./etc/ntp.conf* --exclude=./etc/yp.conf* \
            --exclude=./root/.bash_history --exclude=./root/.subversion --exclude=./root/.esniper \
            --exclude=./root/.cpan --exclude=./home/music/0*/* --exclude=./home/vdr/.freevo/vfs \
            --exclude=./home/vdr/.bash_history"

   sed -i $SDIR/autorun -e 's/export INST_OPT=.*$/export INST_OPT="INSTALL"/'
else
   sed -i $SDIR/autorun -e 's/export INST_OPT=.*$/export INST_OPT="RESTORE"/'
fi

rm -f /mnt/hda5/g2v_base_cfg.tar.bz2
tar -cvjf /mnt/hda5/g2v_base_cfg.tar.bz2 --exclude=*.org --exclude=timers.conf --exclude=./_config/status/*  \
    $EXCLUDES --exclude=./_config/doCheck --exclude=./_config/.* --exclude=./root/.freevo/vfs \
    ./_config ./etc ./root ./home

if [ "$1" = "-i" ] ; then
   if [ ! "$MODCONF" = "" ] && [ -f $MODCONF.org ] ; then
      mv -f $MODCONF.org $MODCONF
   fi
fi
	
echo "mkcfg end"
echo ""
