 #!/bin/sh

echo ""
echo "---TerminalWorks TSPrint Client Installation for Linux---"
echo ""
echo ""
#echo "Usage: install|start|stop|unistall" >&2
OS=`uname -s`
REV=`uname -r`
MACH=`uname -m`
args=$1

if [ -z "$1" ]
  then
	echo "Usage: install|start|stop|uninstall" >&2
        read args
fi

		 
case "$args" in
  install*)
	echo "Installing TSPrintClient"
	echo "lvl1 Menu"
        
        #Check if TSPrintClient is allready running
         output=`ps aux|grep M\[o\]nitorClipboard`
	set -- $output
	pid=$2
        if [ -z $2 ]; then #if length is zero then app isn't active
 	echo " "
	else 
        echo " "
	sudo kill $pid
	sleep 2
	sudo kill -9 $pid >/dev/null 2>&1
	fi
 
	echo  "Check for OS Architecture"

	ARCH=$(file /bin/grep)	
	if [[ $ARCH == *ARM* ]]; then
		if [[ $ARCH ==  *32-bit* ]]; then
			echo "Architecture is ARM32, downloading java_for_ARM32" 
			wget http://www.terminalworks.com/downloads/tsprint/linux/java32ARM.tar.gz	
			wait
			tar -xvf java32ARM.tar.gz
			wait
		else
			echo "Architecture is ARM64, downloading java_for_ARM64" 
			wget http://www.terminalworks.com/downloads/tsprint/linux/javaARM.tar.gz	
			wait
			tar -xvf javaARM.tar.gz
			wait
		fi
		
	elif [[ $ARCH == *64-bit* ]]; then
		echo "Architecture is 64-Bit, downloading java_64bit"
		wget  http://www.terminalworks.com/downloads/tsprint/linux/java64t.tar.gz	
		wait
		tar -xvf java64t.tar.gz
		wait
	else 
		echo "Architecture is 32-Bit, downloading java_32bit"
		wget  http://www.terminalworks.com/downloads/tsprint/linux/java32t.tar.gz
		wait
		tar -xvf java32t.tar.gz
		wait
	fi
 	sleep 0.5   
 	wait

	echo "lvl2"
 	# Download the generic linux installation
 	wget  http://www.terminalworks.com/downloads/tsprint/linux/TSPrintClient.tar.gz &> /dev/null &
	echo -n "Downloading TSPrintClient for Linux"; sleep 0.8; echo -n "."; sleep 0.8; echo -n ".";sleep 0.8;    echo "."
 	wait

	echo "lvl3"
	# Extract the downloaded archive
        tar -xvf TSPrintClient.tar.gz 
 	echo -n "Extracting"; sleep 0.3; echo -n "."; sleep 0.3; echo -n ".";sleep 0.3; echo "."
 
	echo "lvl4"
 
	if [ -d "/opt/TSPrintClient" ]
	then
   		sudo rm -R /opt/TSPrintClient
	fi
	
	#Moving Files To OPT folder and autostart * etc>xdg *)

	sudo chmod +x ./TSPrintClient/TSPrintClient.desktop      
 	mv ./TSPrintClient/TSPrintClient.desktop /etc/xdg/autostart # auto start app
	sudo mv ./TSPrintClient /opt/
 	sudo mv ./java /opt/TSPrintClient/


        echo "lvl4.1"


	#Removing folder which are not in use anymore
         
	sudo rm -R ./TSPrintClient.tar.gz
	
	$dir="./java64t.tar.gz" > /dev/null 2>&1
	if [ -d $dir ] > /dev/null 2>&1; 
	then
 		 sudo rm -R ./java64t.tar.gz > /dev/null 2>&1 
	fi

	$dir="./java32t.tar.gz" > /dev/null 2>&1
	if [ -d $dir ] > /dev/null 2>&1; 
	then
		sudo rm -R ./java32.tar.gz > /dev/null 2>&1 
	fi

	$dir="./java32ARM.tar.gz" > /dev/null 2>&1
	if [ -d $dir ] > /dev/null 2>&1; 
	then
 	  	sudo rm -R ./java32ARM.tar.gz > /dev/null 2>&1  
	fi

	$dir="./javaARM.tar.gz" > /dev/null 2>&1
	if [ -d $dir ] > /dev/null 2>&1; 
	then
 	  	sudo rm -R ./javaARM.tar.gz > /dev/null 2>&1  
	fi

	$dir="./TSPrintClienLinux.jar" > /dev/null 2>&1
	if [ -d $dir ] > /dev/null 2>&1; 
	then
 	  sudo rm -R ./TSPrintClienLinux.jar > /dev/null 2>&1
	fi



        echo "lvl4.2"
 	echo ""
	echo "Checking installation files"
	echo ""
            
    
	ARG_OUT=$(cat /proc/version ARG1 2>&1)
	    if [[ $ARG_OUT == *"SUSE"* ]];then
    		echo $(tput setaf 1) "PLease Restart or Logout to Finish installation..." $(tput setab 7)$(tput sgr 0)
    	 else
			output=`ps aux|grep M\[o\]nitorClipboard`
		set -- $output
		pid=$2
		if [ $pid > /dev/null ]
		then		 
   			echo "Old TSPrintClient is active.Starting New TSPrintClient..."	 
			kill $(ps aux | grep '[M]onitorClipboard' | awk '{print $2}')
			wait
			sudo -u $SUDO_USER /opt/TSPrintClient/java/bin/java -classpath /opt/TSPrintClient/commons-codec-1.10.jar:/opt/TSPrintClient/commons-io-2.4.jar:/opt/TSPrintClient/sun.misc.BASE64Decoder.jar:/opt/TSPrintClient/TSPrintClient.jar MonitorClipboard &
 			
		else
			echo "$(tput setaf 1)Starting TSPrintClient... $(tput setab 7)$(tput sgr 0)"
			sudo -u $SUDO_USER /opt/TSPrintClient/java/bin/java -classpath /opt/TSPrintClient/commons-codec-1.10.jar:/opt/TSPrintClient/commons-io-2.4.jar:/opt/TSPrintClient/sun.misc.BASE64Decoder.jar:/opt/TSPrintClient/TSPrintClient.jar MonitorClipboard &
			fi 
		fi  

	

	echo ""
	echo ""
	echo "Installation Finished"
	echo ""
	;;
  
  stop)
	kill $(ps aux | grep '[M]onitorClipboard' | awk '{print $2}')
	;;
  uninstall)
	kill $(ps aux | grep '[M]onitorClipboard' | awk '{print $2}')	
	sudo rm /etc/xdg/autostart/TSPrintClient.desktop
	#sudo update-rc.d -f  TSPrintClientInstall.sh remove
	sudo rm -R /opt/TSPrintClient
	;;
  start)
	output=`ps aux|grep M\[o\]nitorClipboard`
	set -- $output
	pid=$2
	if [ $pid > /dev/null ]
	then
   		echo "TSPrintClient is already active"
	else
		sudo sh /opt/TSPrintClient/TSPrintClientStart.sh
	fi
        
	
	echo ""
	echo ""
	echo "TSPrintClient Started..."
	echo ""
        ;;
      
  *)
	
	exit 1
	;;
esac
exit 0
