#!/bin/ash
#
#
#
 case "$1" in
 'add')
 echo "69 dgram udp nowait root busybox tftpd -rl /srv/tftp" > /etc/inetd/tftp.conf
 ;;
 'start')
 tftpd add
 inetd
 ;;
 'remove')
 rm /etc/inetd/tftp.conf 2>/dev/null
 ;;
 'stop')
 tftpd remove
 inetd
 ;;
 'restart')
 tftpd start
 ;;
 'test')
 if [ ! -e /etc/inetd/tftp.conf ]; then
   echo "Your TFTP Server seems to be down."
   read -p "Do you wish to start it now (y/n)?: " CHOICE
   CHOICE=y$CHOICE
   if [ $CHOICE = yy ]; then
     tftpd start
   fi
   echo ""
 fi
 echo "Testing port 69 at localhost:"
 nc -uvz localhost 69 >/tmp/test.file 2>&1
 if [ -s /tmp/test.file ]; then
   echo "localhost (127.0.0.1:69) open"
   else
   echo "localhost (127.0.0.1:69) closed"
 fi
 echo ""
 echo "Trying to get a test file from localhost to the /tmp directory:"
 cd /tmp
 rm -f test.file
 tftp -g -r test.file localhost
 echo ""
 if [ -s /tmp/test.file ]; then
   echo "It works :)"
   else
   echo "It doesn't work :("
 fi
 ;;
 'help')
 echo "tftpd start --> start TFTP server"
 echo "tftpd stop --> stop TFTP server"
 echo "tftpd restart --> restart TFTP server"
 echo "tftpd test --> test TFTP server"
 ;;
 *)
 echo "Usage:"
 tftpd help
 ;;
 esac