#!/bin/sh
echo This script will modify /etc/smb.conf and add the possibility to receive smb messages like from  net send
echo It will also install a script in /usr/local/bin/smbpopup
echo Also it will download and install and set the suid bit on a software called alert which goes to /usr/sbin
echo This script checks for some possible problems, but usage is at your own risk!
echo mail comments to osx AT biermann.org
echo "Ready to continue ? (Y)"
read ANSWER
if [ "$ANSWER" = "Y" ]; then
HIT=$(grep -ic "^\[global\].*" /etc/smb.conf )
MYTMPNAME=/tmp/abzud01.tmp.txt
MYTMPNAME1=/tmp/abzud00.tmp.txt
INSLINE="message command = /usr/local/bin/smbpopup %s %m ; rm %s"
NLINES=$( wc -l /etc/smb.conf | awk '{print $1}')

echo found section [global] at times: $HIT in /etc/smb.conf
if [ "$HIT" = "1" ]; then
   HIT1=$(grep -in "^\[global\].*" /etc/smb.conf | awk -F":" '{print $1}')
   echo found section [global] at line: $HIT1 in /etc/smb.conf
   echo 'removing current lines of "message command" directives, if any'
   grep -iv "message command" /etc/smb.conf >$MYTMPNAME1
   HIT1=$(grep -in "^\[global\].*" $MYTMPNAME1 | awk -F":" '{print $1}')
   NLINES=$( wc -l $MYTMPNAME1 | awk '{print $1}')
   head -n $HIT1 $MYTMPNAME1 >$MYTMPNAME
   echo $INSLINE >>$MYTMPNAME
   echo number of lines in original smb.conf: $NLINES
   DLINES=$(( $NLINES - $HIT1 ))
   echo number of lines from insertion point to  end: $DLINES
   tail -n $DLINES $MYTMPNAME1 >>$MYTMPNAME
   echo done modifications, do you whish to write the new /etc/smb.conf Y/N?
   read ANSWER
   if [ "$ANSWER" = "Y" ]; then
        echo please authenticate with your password!
        sudo mv -f /etc/smb.conf /etc/smb.conf.bak
	sudo mv -f $MYTMPNAME /etc/smb.conf  
	sudo chown root:wheel /etc/smb.conf
        echo orginal /etc/smb.conf saved as /etc/smb.conf.bak
        echo finished, please restart windows sharing
   else
	echo 'nothing done, you did not aswerer "Y" !'
   fi
   echo about to write /usr/local/bin/smbpopup ...
   sudo mkdir /usr/local
   sudo mkdir /usr/local/bin
   sudo mv /usr/local/bin/smbpopup /usr/local/bin/smbpopup.bak
   echo please ignore any error message above.
   sudo touch /usr/local/bin/smbpopup   
   sudo chown $USER /usr/local/bin/smbpopup
   cat >/usr/local/bin/smbpopup << "EOF"
#!/bin/sh
logger smbpopup: $1 $2
MSG=$( cat $1 )
/usr/sbin/alert "SMB_Message_From_$2:" "$MSG"
EOF
sudo chown root:wheel /usr/local/bin/smbpopup
sudo chmod a+x /usr/local/bin/smbpopup
echo "done !"
echo checking for /usr/sbin/alert ...
   if [ ! -f /usr/sbin/alert ]; then
     echo "alert was not found, please check http://www.biermann.org/philipp for the software!."
     echo "should I try to download and install the alert program ? (Y)"
     read ANSWER
     if [ "$ANSWER" = "Y" ]; then
        echo downloading from: http://www.biermann.org/philipp/OSX/alert.tar.gz
        curl http://www.biermann.org/philipp/OSX/alert.tar.gz | tar -zx
        sudo mv -f ./alert /usr/sbin/alert
     else
       echo "done nothing you did not type Y ... exit"
       exit
     fi
    fi
    echo checking again for /usr/sbin/alert ...
    if [ -f /usr/sbin/alert ]; then
      echo looks like the program is there
      echo setting correct permissions...type your password in case asked!
      sudo chmod a+x /usr/sbin/alert
      sudo chown root:wheel /usr/sbin/alert
      sudo chmod u+s /usr/sbin/alert
      echo all done!
    else
      echo "alert was not found something went wrong, please check http://www.biermann.org/philipp for the software!."   
     exit
    fi
else
 echo "error parsing /etc/smb.conf"
exit
fi
else
 echo "canceled!"
fi

