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

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

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

   HOSTNAME=`$GETVAL HOSTNAME`
   echo "/etc/hostname wird angepasst"
   mv -f /etc/hostname /etc/hostname.org
   echo "$HOSTNAME" > /etc/hostname
   hostname $HOSTNAME


   mv -f $NET_CONFIG $NET_CONFIG.old
   DHCP=`$GETVAL DHCP`
   if [ "$DHCP" = "0" ] ; then
      IPADR=`$GETVAL IPADR -B`
      GATEWAY=`$GETVAL GATEWAY -B`
      SUBNET=`$GETVAL SUBNET -B`
      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 "Keine IP-Adresse per DHCP gefunden"
         if [ "$1" = "INIT" ] ; then
            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
               $SETVAL DHCP 0
               $SETVAL IPADR "192.168.1.111"
               $SETVAL GATEWAY "192.168.1.1"
               $SETVAL SUBNET "255255.0.0"
	    fi
	 fi
      fi
      if [ "$IPADR" = "" ] ; then
         logger "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

      SUBNET=`$GETVAL SUBNET -B`
      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

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

   SAMBA=`$GETVAL SAMBA`
   if [ "$SAMBA" = "0" ] ; then
      rc-update del samba default >/dev/null 2>&1
      /etc/init.d/samba stop
   else
      WORKGROUP=`$GETVAL WORKGROUP`
      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

   SSH=`$GETVAL SSH`
   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

   NFS=`$GETVAL NFS`
   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

   FTP=`$GETVAL FTP`
   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.conf -e "s/only_from.*$/only_from = 127.0.0.0\/24 $IPRANGE$IPRREST\/$IPRLEN/"
      sed -i /etc/xinetd.d/ftp -e "s/disable.*$/disable = no/"
      /etc/init.d/xinetd restart
   fi

   NTP=`$GETVAL NTP`
   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

fi
exit 0
