#!/bin/sh
MP="audio video pictures"
if [ "$1" = "-f" ]  || [ ! -s /etc/PARTNERS ] ; then
   rm -f /etc/PARTNERS >/dev/null 2>&1
   echo "" >> /etc/hosts
   BC=`ifconfig |grep "Bcast:" | cut -f 3 -d ":" | cut -f 1 -d " "`
   IADR=`ifconfig |grep "Bcast:" | cut -f 2 -d ":" | cut -f 1 -d " "`
   HNAME=`hostname`
   if [ "$BC" != "" ] ; then
      ALL=`ping -b -c2 $BC 2>/dev/null | grep DUP | cut -f 4 -d " " | cut -f 1 -d ":"`
      for i in $ALL ; do
	 PN=`grep "^${i}" /etc/PARTNERS 2>/dev/null`
	 if [ "$PN" = "" ] ; then
	    umount /mnt/samba > /dev/null 2>&1
	    mkdir /mnt/samba > /dev/null 2>&1
            mount -t smbfs -o username=root,password=gen2vdr //${i}/root /mnt/samba/ > /dev/null 2>&1
            PHN=`grep "^HOSTNAME=vdr" /mnt/samba/etc/conf.d/hostname 2>/dev/null`
	    if [ "$PHN" != "" ] ; then
	       TEST=`grep "^${i}" /etc/hosts 2>/dev/null`
	       if [ "$TEST" = "" ] ; then
	          echo "${i} $PHN" >> /etc/hosts
	       fi
	       echo "${i} $PHN" >> /etc/PARTNERS
	       TEST=`grep "^$IADR" /mnt/samba/etc/hosts 2>/dev/null`
	       if [ "$TEST" = "" ] ; then
                  echo "" >> /mnt/samba/etc/hosts
   		  echo "$IADR $HNAME" >> /mnt/samba/etc/hosts
	       fi
	       TEST=`grep "^$IADR" /mnt/samba/etc/PARTNERS 2>/dev/null`
	       if [ "$TEST" = "" ] ; then
   		  echo "$IADR $HNAME" >> /mnt/samba/etc/PARTNERS
	       fi
	    fi
            umount /mnt/samba/ > /dev/null 2>&1
	 fi
      done
   fi
fi
for i in `mount | grep "^vdr" | grep " nfs " | cut -f 3 -d " "` ; do
   umount -v ${i}
done
for i in `cat /etc/PARTNERS 2>/dev/null | cut -f 2 -d " "` ; do
   echo "<${i}>"
   for j in $MP ; do
      mkdir /${j}/${i} > /dev/null 2>&1
      mount -t nfs ${i}:/${j} /${j}/${i}
   done
done
