#!/bin/bash

tarFile=LINUX_UDP_BIN.tar
tarDirectory=./LINUX_UDP_BIN
currPath=`pwd`
exeName=UDPApp
scriptName=StartUDPServer
installerName=UDP_Install
delDir=LINUX_UDP_BIN

if [ -f ./$tarFile ]
then
	echo ""
else
	echo "$tarFile not present in $currPath."
	echo "Keep the $tarFile in same path as of $installerName & run again."
	echo "Press Enter key to exit..."
	read tempVar
	exit
fi

tar -xvf $tarFile &>/dev/null
if [ $? -ne 0 ]
then
	echo "Error when trying to unzip $tarFile"
	echo "Press Enter key to exit..."
	read tempVar
	exit
fi

while [ 1 ]
do
	clear
	echo "Please give the path for the UDP SERVER to be installed(/home/<user_name>/<directory_name>) :"
	read path
	
	if [ -z $path ]
	then
		echo "Invalid path provided !"
		echo "Press Enter key to continue..."
		read tempVar
	else
		break
	fi
done


if [ -d $path ]
then
	mv $tarDirectory/$exeName $tarDirectory/$scriptName $path
	if [ $? -ne 0 ]
	then
		echo "Error, Check sufficient permissions exists to given path : $path"
		exit 1
	fi
else
	mkdir -p $path
	if [ $? -ne 0 ]
        then
                echo "Error, Check sufficient permissions exists to given path : $path"
                exit 1
        fi
	mv $tarDirectory/$exeName $tarDirectory/$scriptName $path
        if [ $? -ne 0 ]
        then
                echo "Error, Check sufficient permissions exists to given path : $path"
                exit 1
        fi
fi

cd $path
chmod 755 $exeName
chmod 755 $scriptName
rm -rf $delDir
echo ""

cd $path
totalPath=`pwd`
echo "Successfuly Installed UDP Server at path : $totalPath"
echo "Please run UDP Server from above path only."
