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

NET_SCRIPTS="net.eth0 netmount samba sshd xinetd xxv vdradmind ntp-client nfs"
NET_CONFIG="/etc/conf.d/net"

if [ "$NET" = "0" ] ; then
   logger -s "ADMIN - Netzwerk wird deaktiviert"
   for i in $NET_SCRIPTS ; do
      rc-update del ${i} default >/dev/null 2>&1
   done
else
   logger -s "ADMIN - Netzwerk wird aktiviert"
   rc-update add net.eth0 default >/dev/null 2>&1
   rc-update add netmount default >/dev/null 2>&1

   echo "/etc/conf.d/hostname wird angepasst"
   echo "HOSTNAME=$HOSTNAME" > /etc/conf.d/hostname
   hostname $HOSTNAME
   sed -i /etc/samba/smb.conf -e "s/netbios name = .*$/netbios name = $HOSTNAME/"

   mv -f $NET_CONFIG $NET_CONFIG.old
   if [ "$DHCP" = "0" ] ; then
      BROADCAST=`echo $IPADR | cut -f 1 -d "."`
      IPRANGE=`echo $IPADR | cut -f 1 -d "."`
      IPRREST=""
      IPRLEN=32
      for i in `seq 2 4` ; do
         NM=`echo $SUBNET | cut -f ${i} -d "."`
         if [ "$NM" = "0" ] ; then
            NM=255
            IPRREST=$IPRREST.0
	           IPRLEN=$(($IPRLEN-8))
         else
      	     NM=`echo $IPADR | cut -f ${i} -d "."`
            IPRANGE=$IPRANGE.$NM
         fi
         BROADCAST=$BROADCAST.$NM
      done
      echo "iface_eth0=\"$IPADR broadcast $BROADCAST netmask $SUBNET\"" > $NET_CONFIG
      if [ "$GATEWAY" != "" ] ; then
         echo "gateway=\"eth0/$GATEWAY\"" >> $NET_CONFIG
      fi
      rm -f /etc/resolv.conf
      IPA=`ifconfig |grep "inet addr:"|cut -f2 -d":"|cut -f1 -d" "|sed '/127\./d'`
      if [ "$IPA" != "$IPADR" ] ; then
         /etc/init.d/net.eth0 stop
         ifconfig eth0 down
         /etc/init.d/net.eth0 start
      fi
   else
      echo "iface_eth0=\"dhcp\"" > $NET_CONFIG
      /etc/init.d/net.eth0 stop
      ifconfig eth0 down
      /etc/init.d/net.eth0 start
      IPADR=`ifconfig |grep "inet addr:"|cut -f2 -d":"|cut -f1 -d" "|sed '/127\./d'`
      if [ "$IPADR" = "" ] ; then
         IPADR=`ifconfig |grep "inet Adresse:"|cut -f2 -d":"|cut -f1 -d" "|sed '/127\./d'`
      fi
      if [ "$IPADR" = "" ] ; then
         logger -s "ADMIN - Keine IP-Adresse per DHCP gefunden"
         echo "iface_eth0=\"192.168.1.111 broadcast 192.168.1.255 netmask 255.255.0.0\"" > $NET_CONFIG
         echo "gateway=\"eth0/192.168.1.1\"" >> $NET_CONFIG
         rm -f /etc/resolv.conf
         /etc/init.d/net.eth0 stop
         ifconfig eth0 down
         /etc/init.d/net.eth0 start
         IPADR=`ifconfig |grep "inet addr:"|cut -f2 -d":"|cut -f1 -d" "|sed '/127\./d'`
         if [ "$IPADR" != "" ] ; then
            DHCP=0            
            $SETVAL DHCP 0
            $SETVAL IPADR "192.168.1.111"
            GATEWAY="192.168.1.1"
            $SETVAL GATEWAY "192.168.1.1"
            SUBNET="255.255.0.0"
            $SETVAL SUBNET "255.255.0.0"
         fi
      fi
      if [ "$IPADR" = "" ] ; then
         logger -s "ADMIN - Netzwerk wird deaktiviert"
         for i in $NET_SCRIPTS ; do
            rc-update del ${i} default >/dev/null 2>&1
         done
         $SETVAL NET 0
         exit 1
      fi

      IPRANGE=`echo $IPADR | cut -f 1 -d "."`
      IPRREST=""
      IPRLEN=32
      for i in `seq 2 4` ; do
         NM=`echo $SUBNET | cut -f ${i} -d "."`
         if [ "$NM" = "0" ] ; then
            NM=255
            IPRREST=$IPRREST.0
	           IPRLEN=$(($IPRLEN-8))
         else
      	     NM=`echo $IPADR | cut -f ${i} -d "."`
            IPRANGE=$IPRANGE.$NM
         fi
      done
   fi

   echo "/etc/vdr/svdrphosts.conf wird angepasst"
   sed -i /etc/vdr/svdrphosts.conf \
       -e "/192.168.0.*/d" \
       -e "/9.152.*/d" \
       -e "/# local subnet/d"

   echo "$IPRANGE$IPRREST/$IPRLEN   # local subnet" >> /etc/vdr/svdrphosts.conf

   if [ "$NAMESERVER" != "" ] ; then
      echo "nameserver $NAMESERVER" >> /etc/resolv.conf
   fi

   if [ "$SAMBA" = "0" ] ; then
      rc-update del samba default >/dev/null 2>&1
      /etc/init.d/samba stop
   else
      rc-update add samba default >/dev/null 2>&1
      echo "/etc/samba/smb.conf wird angepasst"
      sed -i /etc/samba/smb.conf -e "s/hosts allow = .*$/hosts allow = $IPRANGE./" -e "s/workgroup = .*/workgroup = $WORKGROUP/"
      /etc/init.d/samba restart
   fi
   logger -s "ADMIN - Samba: $SAMBA"

   if [ "$SSH" = "0" ] ; then
      rc-update del sshd default >/dev/null 2>&1
      /etc/init.d/sshd stop
   else
      rc-update add sshd default >/dev/null 2>&1
      /etc/init.d/sshd restart
   fi
   logger -s "ADMIN - SSH: $SSH"

   if [ "$NFS" = "0" ] ; then
      rc-update del nfs default >/dev/null 2>&1
      /etc/init.d/nfs stop
   else
      rc-update add nfs default >/dev/null 2>&1
      /etc/init.d/nfs restart
   fi
   logger -s "ADMIN - NFS: $NFS"

   sed -i /etc/xinetd.conf -e "s/only_from.*$/only_from = 127.0.0.0\/24 $IPRANGE$IPRREST\/$IPRLEN/"
   if [ "$FTP" = "0" ] ; then
      sed -i /etc/xinetd.d/ftp -e "s/disable.*$/disable = yes/"
      rc-update del xinetd default >/dev/null 2>&1
      /etc/init.d/xinetd stop
   else
      rc-update add xinetd default >/dev/null 2>&1
      sed -i /etc/xinetd.d/ftp -e "s/disable.*$/disable = no/"
      /etc/init.d/xinetd restart
   fi
   logger -s "ADMIN - FTP: $FTP"

   if [ "$NTP" = "0" ] ; then
      rc-update del ntp-client default >/dev/null 2>&1
   else
      rc-update add ntp-client default >/dev/null 2>&1
   fi
   logger -s "ADMIN - NTP(Uhrzeit per Internet): $NTP"

   if [ "$XXV" = "0" ] ; then
      rc-update del xxvd default >/dev/null 2>&1
   else
      rc-update add xxvd default >/dev/null 2>&1
   fi
   logger -s "ADMIN - XXV: $XXV"

   if [ "$VDRADMIN" = "0" ] ; then
      rc-update del vdradmind default >/dev/null 2>&1
   else
      rc-update add vdradmind default >/dev/null 2>&1
   fi
   logger -s "ADMIN - VDRADMIN: $VDRADMIN"

fi
exit 0
