#!/bin/bash

#used for cli command "file tftp"
#input:
#        $1: "put" or "get"
#        $2:  the remote file with path
#        $3: the local file name,when $1 is "get",this parm is withoutput path,otherwise with full path
#        $4: the remote ip address.
#no output var


. /etc/functions
#crontab -r

TFTP_STATUS_FILE=/tmp/run/tftp_status

is_v4_ip ()
{
    dot=`echo $1 | awk -F '.' '{print NF-1}'`
    if [ $dot -ne 3 ]; then
        return 1
    fi

    count=0
    for var in `echo $1 | awk -F. '{print $1, $2, $3, $4}'`
    do
        echo $var | grep "^[0-9]*$" &>/dev/null
        if [ $? -ne 0 ]; then
            return 1
        fi

        if [ $var -ge 0 -a $var -le 255 ] ; then
            count=`expr $count + 1`
            continue
        else
            return 1
        fi
    done

    if [ $count -eq 4 ]; then
        return 0
    else
        return 1
    fi
}

#xorp_group=`id | awk -F '=' '{print $NF}' |grep "root" | wc -l`
root_group=`id | awk '{print $NF}' |grep "root" | wc -l`
xorp_group=`id | awk '{print $NF}' |grep "xorp" | wc -l`

if [[ $xorp_group -eq 0 && $root_group -eq 0 ]]; then
    exit 0
fi

is_v4_ip $4
	
case $? in
	0)
		#break
		;;
	1)
		echo "Your input IP is invalid."
		exit 0
		;;
esac
	

if [ $1 = "get" ]; then
    localPath="/cftmp/"
    fullPath="$localPath""$3"
    if [ $(expr "$3" : ".*rootfs.tar.gz*") -ne 0 ]; then
        echo "Start to get the '$2' to '$fullPath'."
        echo "Waiting......"
        #sudo atftp -g -r $2 -l $fullPath $4
        sudo tftp $4 >$TFTP_STATUS_FILE 2>&1 <<EOF
             verbose
             get $2 $fullPath
             quit
EOF
        RES=`grep "Received" $TFTP_STATUS_FILE | wc -l`
        rm -fr $TFTP_STATUS_FILE
        case $RES in
        #case $? in
            1)
                #break
            ;;
            0)
                echo "Failed to get file from remote!"
                sudo rm -fr $fullPath
                exit 0
            ;;
            esac
        echo "Done!"
        sync
#       echo "Remove the TP files......"
#       sudo rm -rf /pica/etc/templates/
#       sync
#       echo "Update the image......"
#       sudo tar -zxvpf $fullPath -C /
#       sync
#       echo ""
#       echo "Update image Done! Please reboot the switch now!"
    elif [ $(expr "$3" : ".*pica.tar.gz*") -ne 0 ]; then
        echo "Start to get the '$2' to '$fullPath'."
        echo "Waiting......"
        #sudo atftp -g -r $2 -l $fullPath $4
        sudo tftp $4 >$TFTP_STATUS_FILE 2>&1 <<EOF
             verbose
             get $2 $fullPath
             quit
EOF
        RES=`grep "Received" $TFTP_STATUS_FILE | wc -l`
        rm -fr $TFTP_STATUS_FILE
        case $RES in
		#case $? in
            1)
               #break
            ;;
            0)
                echo "Failed to get file from remote!"
                sudo rm -fr $fullPath
                exit 0
            ;;
            esac

        echo "Done!"
        sync
#       echo "Remove the TP files......"
#       sudo rm -rf /pica/etc/templates/
#       sync
#       echo "Update the image......"
#       sudo tar -zxvpf $fullPath -C /
#        sync
#        echo ""
#        echo "Update image Done! Please reboot the switch now!"
    elif [ $(expr "$3" : ".*pica_startup.boot") -ne 0 ]; then
        echo "Start to get the '$2' to /pica/config/."
        echo "Waiting......"
        #sudo atftp -g -r $2 -l /pica/config/$3 $4
        sudo tftp $4 >$TFTP_STATUS_FILE 2>&1 <<EOF
             verbose
             get $2 /pica/config/$3
             quit
EOF
        RES=`grep "Received" $TFTP_STATUS_FILE | wc -l`
        rm -fr $TFTP_STATUS_FILE
        case $RES in
        #case $? in
            1)
                #break
            ;;
            0)
                echo "Failed to get file from remote!"
                sudo rm -fr $fullPath
                exit 0
            ;;
            esac

        echo "Done!"
        sync
        echo "Update the configuration file......"
        cp /pica/config/pica_startup.boot /pica/config/pica.conf
        sync
        echo ""
        echo "Update configuration file Done!"
    else
        #sudo atftp -g -r $2 -l $fullPath $4
        sudo tftp $4 >$TFTP_STATUS_FILE 2>&1 <<EOF
             verbose
             get $2 $fullPath
             quit
EOF
        RES=`grep "Error" $TFTP_STATUS_FILE | wc -l`
        #rm -fr $TFTP_STATUS_FILE
        case $RES in
            0)
                #break
            ;;
            1)
                echo "Failed to get file from remote!"
                sudo rm -fr $fullPath
                rm -fr $TFTP_STATUS_FILE
                exit 0
            ;;
        esac
        RES=`grep "unreachable" $TFTP_STATUS_FILE | wc -l`
        case $RES in
            0)
                #break
            ;;
            1)
                echo "Failed to get file from remote!"
                sudo rm -fr $fullPath
                rm -fr $TFTP_STATUS_FILE
                exit 0
            ;;
        esac
        RES=`grep "timed out" $TFTP_STATUS_FILE | wc -l`
	case $RES in
            0)
            ;;
            1)
                echo "Failed to get file from remote!"
                sudo rm -fr $fullPath
                rm -fr $TFTP_STATUS_FILE
                exit 0
            ;;
        esac
        RES=`grep "No such file or directory" $TFTP_STATUS_FILE | wc -l`
   	case $RES in
            0)
            ;;
            1)
                echo "The directory ${fullPath%/*} doesn't exist!"
                #sudo rm -fr $fullPath
                rm -fr $TFTP_STATUS_FILE
                exit 0
            ;;
        esac
         rm -fr $TFTP_STATUS_FILE
	fi
elif [ $1 = "put" ]; then
    if [ -f $3 ]; then
        echo "Start to copy."
        #sudo atftp -p -r $2 -l $3 $4
        sudo tftp $4 >$TFTP_STATUS_FILE <<EOF
             verbose
             put $3 $2
             quit
EOF
        RES=`grep "Sent" $TFTP_STATUS_FILE | wc -l`
        rm -fr $TFTP_STATUS_FILE
        case $RES in
        #case $? in
            1)
                #break
            ;;
            0)
                echo "Failed to put the file to remote!"
                exit 0
            ;;
        esac

        echo "Done!"
    else
        echo "Error, $3 doesn't exist!"
    fi
fi
#crontab  /etc/crontab
exit 0
