#!/bin/sh
# commission.sh <target serial number>

if [ "$#" -ne 1 ]; then
    echo ""
    echo "Illegal number of parameters."
    echo "Usage: commission.sh < serial number >"
    echo ""
    exit 0
fi

#read -p "Enter or scan device's serial number: " serialNumber
serialNumber=$1

echo "---> Kill Insight Apps "
pkill -9 Insight

echo "---> Extracting sbin.tar "
cd /Insight
tar -x -f sbin.tar 
chmod +rwx /Insight/sbin/*

# create test_identity file and copy to /Insight/sbin/identity_tools/
id="/Insight/sbin/identity_tools"
echo "---> Creating /Insight/sbin/identity_tools folder "
mkdir ${id}
echo "---> Creating identity test_identity file to copy to target"
echo "#!/bin/sh" > ${id}/test_identity
echo "echo $serialNumber" >> ${id}/test_identity
echo "" >> ${id}/test_identity
chmod +rwx ${id}/test_identity

# create identity.config and copy to Insight
echo "---> Creating identity.config file"
echo "{\"serialNumber\":\"$serialNumber\", \"revisionNumber\":2," > /Insight/identity.config
echo "\"machineType\":\"AACore\", \"firmwareImageType\":3," >> /Insight/identity.config
echo "\"hardwareRevisionNumber\":4}" >> /Insight/identity.config

# creates symbolic links on AACore target
ln -s /Insight/sbin/identity_tools/test_identity /Insight/identity
# Create Insight bootup auto start link 
ln -s /Insight/Scripts/insightStartup.sh /etc/rc5.d/S20insight

# run script to start Insight applications
echo "---> Start up Insight apps for commissioning"
/Insight/Scripts/insightStartup.sh

#run script on remote host to generate keys and link up SBC and server
echo "---> Running generate_keys.sh script on host"
cd /Insight/sbin
#./eol.sh -- 10.54.101.8:8080 ../keys/id.key
./eol.sh -- 169.254.0.1:8080 ../keys/id.key
sleep 3
echo ""
echo ""
echo "---> Commissioning finished <---"
echo ""



