#!/bin/bash

Usage() { 
	echo ' '
	echo " Usage: sudo pcs-update-hprt <printer|labeler> <DFU file>" 
	echo ' '
}

account=`whoami`
if [ ${account} != "root" ]; then
    echo " Hi ${account}, you are NOT the supervisor."
    echo " The root permission is required to run this installer."
    echo " You must execute this script with sudo."
	Usage
    exit 1
fi

dpkg -s dfu-util >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "  dfu-util package not installed."
	echo "  Please, execute 'sudo apt-get install dfu-util'"
	exit 1
fi

if [ "$1" != "printer" -a "$1" != "labeler" ]; then
    echo " You must especify 'printer' or 'labeler' as first command."
	Usage
	exit 1
fi

if [ "$2" = "" ]; then
    echo " You must especify a DFU file to update firmware as second command."
	Usage
	exit 1
fi

if [ ! -f /usr/local/bin/$2 ]; then
    echo " File '$2' does not exist on /usr/local/bin folder."
	Usage
	exit 1
fi

device="5562"
if [ "$1" = "printer" ]; then
	device="5541"
fi
current=$(pwd)
cd /usr/local/bin
pcs-labeler-tools --vendor=2aaf --device=$device --write="1d 75 55 aa"
sleep 1
dfu-util -d 0483:df11 -a 0 -D $2
echo ' '
if [ $? -ne 0 ]; then
	echo " Firmware update failed!"
else
	echo " Firmware update OK!"
fi
cd $current
echo ' '
echo " You must shutdown and unplug power line during several seconds."  
echo ' '
