#!/bin/sh
# Patch a linux image which has support for Qt-5.3.0 or Qt-5.4.0 and should have Qt-5.6.3 instead.
# CrossControl AB 2018
# Daniel Nisses-Gagnér

VERSION=1.0.1
PROFILE_FILE=/etc/profile
SUCCESS=1

echo "Patching profile configured for Qt-5.4.0 to run Qt-5.6.3 runtime!"

#Unclock the filesystem
mount -o remount,rw /

# Patch /etc/profile. Insert new exports for tslib
if grep -q 'QT_QPA_PLATFORM' "${PROFILE_FILE}"; then
	
	# Append only if none of the new lines are already found in the file!
	if ! grep -q 'QT_QPA_EGLFS_DISABLE_INPUT' "${PROFILE_FILE}"; then
		sed -i '/export QT_QPA_PLATFORM/a \ \ \ \ export QT_QPA_EGLFS_DISABLE_INPUT=1' $PROFILE_FILE
		sed -i '/export QT_QPA_PLATFORM/a \ \ \ \ export TSLIB_TSDEVICE=/dev/touch0' $PROFILE_FILE
		sed -i '/export QT_QPA_PLATFORM/a \ \ \ \ export QT_QPA_EGLFS_HIDECURSOR=1' $PROFILE_FILE
	fi
else
	echo "Error: Expected information in /etc/profile not found. Is the OS version up-to-date?"
	SUCCESS=0
fi

# Add link from cc-aux2.so.2.9 to latest installed version
cd /usr/lib
CCAUXVERSION=$(readlink -f libcc-aux2.so)
ln -sf $CCAUXVERSION libcc-aux2.so.2.9

sync

#Lock the filesystem
mount -o remount,ro /

#Check for errors
if [ $SUCCESS == 1 ]; then
	# Create a stamp to indicate installed package
	if [ ! -d /opt/packages ]; then
		mkdir /opt/packages
	fi
	touch /opt/packages/Qt5.6.3_profile-patch_ARM_${VERSION}
else
	echo "Patch failed, error occured during patching!"
fi