From 07b236c0eff977516c979559a9c710c84842096a Mon Sep 17 00:00:00 2001
From: kwindrem <58538395+kwindrem@users.noreply.github.com>
Date: Thu, 7 Mar 2024 08:52:01 -0800
Subject: [PATCH] removed spi1-1cs.dtbo - unused

---
 FileSets/VersionIndependent/spi1-1cs.dtbo | Bin 1523 -> 0 bytes
 FileSets/fileListVersionIndependent       |   1 -
 HelperResources/CommonResources           | 190 ++++++++++++++++------
 HelperResources/EssentialResources        |   3 +
 HelperResources/IncludeHelpers            |  16 +-
 HelperResources/version                   |   2 +-
 changes                                   |   3 +
 packageDependencies                       |   1 +
 version                                   |   2 +-
 9 files changed, 157 insertions(+), 61 deletions(-)
 delete mode 100644 FileSets/VersionIndependent/spi1-1cs.dtbo
 create mode 100644 packageDependencies

diff --git a/FileSets/VersionIndependent/spi1-1cs.dtbo b/FileSets/VersionIndependent/spi1-1cs.dtbo
deleted file mode 100644
index 1ee5981dd8bbe5980ee9095581f82caf81653e1e..0000000000000000000000000000000000000000
GIT binary patch
literal 0
HcmV?d00001

literal 1523
zcmaJ>&2AGh5O&&B1(ixbLE?bKPlbdKQL-sWRZisrcmTv<<!qd=V*gn0Rtc}bv3JhA
z04L5|xPZh<aN)!q_&%>4Z_+lFW@pBp`8=Ne>>vAoe+#kmM+k8YeINQU<ObwAgyW|W
z-CvEl^6gk}8+xDixQx=L<0u^-A3e8gooOj&smiL8ftg`!7qa&c1G*E2`Kc-sxd=lB
zbaW91VG(DVo)C<`2Du42fZT%IhB%zN)^`3A_^fM-+F(Vx1-Hy29>wJmhk4E?4l&x|
z&k|t8$6j_8hc!fO8~cUHZt}0demPN7Stk{znZvo)5O)sFu?NcbjCDJ_9oSj^RxY49
z+@7qTHhVBpr?&ge+%5x~d0NYy+IZgIR5$<OVpqR9vH6?oVC=<x<PLtnO?_O)cX*aR
zB@U?@1~qv1fcpWmWjIMbjSK;|Bi)-7ajr+txV1U^9`qTct1DU64e^K%Y`sXwd18^-
z&xeqYeNXCsi@*H~xcdn4pEkyy=jR>deD*m(dk-v`njb)axg>^lwEww1%QT+Xg=-Se
zg8MGMdW^xSlI2WQXq2B^*?hEYUeinD^DcgCg?kJvU~nJCeD^WK^%z@Ul1DNLSE|H6
zShl*kbL;N~@!fraY2Fs%Sxmky=eWUma)X0!95KjIa)86-Q(ukpw2)OiPLycc)4X!#
zg)-cl)>%}=c_xZCGMgamP);VL(z+L^B+=qf$M2Q3krh>C(#vG3v~b=)xV{R@z*5b4
tL3^p3_ok(KSF0>qAZoy;%xHUkJ>!h!xWuLf_KFALK#=it10n0V_z%ZoAesOG

diff --git a/FileSets/fileListVersionIndependent b/FileSets/fileListVersionIndependent
index 93b0286..221ba27 100644
--- a/FileSets/fileListVersionIndependent
+++ b/FileSets/fileListVersionIndependent
@@ -1,3 +1,2 @@
-/u-boot/overlays/spi1-1cs.dtbo
 /u-boot/overlays/VenusGpioOverlay.dtbo
 /u-boot/overlays/VenusGpioOverlayForCanHats.dtbo
\ No newline at end of file
diff --git a/HelperResources/CommonResources b/HelperResources/CommonResources
index fc853c2..f448b30 100755
--- a/HelperResources/CommonResources
+++ b/HelperResources/CommonResources
@@ -46,6 +46,47 @@ fileListAll=()
 ######## skip to bottom of file for remainder of code executed when script is sourced
 
 
+# checkPackageDependencies checks the packageDependencies file in the package directory
+#
+# all unmet dependencies are reported to the command line/log
+#	and to stderr if not running from the command line
+# then the script exists
+
+function checkPackageDependencies ()
+{
+	dependencyFile="$scriptDir/packageDependencies"
+
+	#no dependencies specified for this package
+	if ! [ -f "$dependencyFile" ]; then return; fi
+
+	errors=false
+
+	while IFS= read -r line; do
+		read package requirement <<< "$line"
+		if [ -f "$installedVersionPrefix"$package ]; then
+			packageInstalled=true
+		else
+			packageInstalled=false
+		fi
+		case $requirement in
+			installed)
+				if ! $packageInstalled ; then
+					setInstallFailed $EXIT_PACKAGE_CONFLICT "$package must be installed first"
+					error=true
+				fi
+				;;
+			uninstalled)
+				if $packageInstalled ; then
+					setInstallFailed $EXIT_PACKAGE_CONFLICT "$package must be uninstalled first"
+					error=true
+				fi
+				;;
+		esac
+
+		if $errors; then endScript; fi
+	done < "$dependencyFile"
+}
+
 # getFileLists reads the file list from files in the FileSets directory
 #
 #	'fileList' file must only list version-dependent files
@@ -253,7 +294,39 @@ setInstallFailed ()
 	fi
 	message="${@:2}"
 	if [ ! -z "$message" ]; then
-		logMessage "$message"
+		logMessage "ERROR: $message"
+		# if not running from console, output error to stderr
+		if ! $logToConsole ; then
+			echo "$message" >&2
+		fi
+	fi
+}
+
+# forcePackageUninstall insures a conflicting package is uninstalled before
+#	this package is installed
+# the setup script must call this script BEFORE it begins installing anything
+# $1 is the package name
+# $2 contains an optional message
+
+forcePackageUninstall ()
+{
+	if (( $# < 1 )); then
+		return
+	fi
+	if [ -f "$installedVersionPrefix""$1" ]; then
+		if (( $# >= 2 )); then
+			logMessage "${@:2}"
+		else
+			logMessage "uninstalling $1 - it conflicts with $packageName"
+		fi
+		if [ -e "/data/$1/setup" ]; then
+			"/data/$1/setup" "uninstall" "auto" "deferReboot" "deferGuiRestart"
+		else
+			logMessage "WARNING can't uninstall $1 - no package directory or no setup script"
+		fi
+		if [ -e "/data/settupOptions/$1" ]; then
+			touch "/data/settupOptions/$1/DO_NOT_AUTO_INSTALL"
+		fi
 	fi
 }
 
@@ -374,11 +447,12 @@ updateActiveFile ()
     # separate source and replacement files specified
     local sourceFile=""
     local activeFile=""
+    local prevousPackage=""
     if [ $# == 2 ]; then
         if [ -f "$1" ]; then
 			sourceFile="$1"
         else
-            setInstallFailed $EXIT_FILE_SET_ERROR "ERROR: specified soure file "$1" does not exist - can't continue with install"
+            setInstallFailed $EXIT_FILE_SET_ERROR "specified soure file "$1" does not exist"
 			return 1
         fi
         activeFile="$2"
@@ -409,7 +483,7 @@ updateActiveFile ()
             sourceFile="$pkgFileSets/$baseName"
 		# nothing found - can't continue
 		else
-			setInstallFailed $EXIT_FILE_SET_ERROR "ERROR: no soure file for $baseName - can't continue with install"
+			setInstallFailed $EXIT_FILE_SET_ERROR "no soure file for $baseName"
 			return 1
         fi
     fi
@@ -419,48 +493,51 @@ updateActiveFile ()
 		# fatal if GUI v1 not present and needed for install
 		if [[ "$pathToFile" == "/opt/victronenergy/gui/"* ]]; then
 			if $guiV1required ; then
-				setInstallFailed $EXIT_NO_GUI_V1 "ERROR $packageName requires GUI v1 which is not on the system - can't continue"
+				setInstallFailed $EXIT_NO_GUI_V1 "$packageName requires GUI v1"
 			# GUI v1 not needed - proceed without updating active file
 			fi
 		# active file is not in GUI v1 part of file system - so this is fatal
 		 else
-			setInstallFailed $EXIT_FILE_SET_ERROR "ERROR: path to $activeFile does not exist - can't continue with install"
+			setInstallFailed $EXIT_FILE_SET_ERROR "path to $activeFile does not exist"
 		fi
 		return 1
 	fi
 
-	# attempt backup --if not made
-	#	indicates installing over a previous install - so check if activeFile would be changed
-	#		this would occur if installing over the top of an already installed package
-	#		which SHOULD update the active file
-	#	or a conflict with another package - OTHER PACKAGE'S MOD (active file) IS OVERWRITTEN SILENTLY !!!!!!
-	#	or a previous install step failed (not relavant here -- already checked above)
-    if ! backupActiveFile "$activeFile" ; then
-		# check to see if active file needs to be updated
-		if [ -f "$activeFile" ]; then
-			# already updated - no change to active file
-			if cmp -s "$sourceFile" "$activeFile" > /dev/null ; then
-				return 1
-			fi
+	# check for package conflicts
+	if [ -f "$activeFile.package" ]; then
+		prevousPackage=$( cat "$activeFile.package" )
+		if [ $packageName != $prevousPackage ]; then
+			setInstallFailed $EXIT_PACKAGE_CONFLICT "$(basename $activeFile) was already modfied by $prevousPackage"
+			return 1
+		fi
+	fi
+	# already updated - nothing to do
+	if [ -f "$activeFile" ]; then
+		if cmp -s "$sourceFile" "$activeFile" > /dev/null ; then
+			return 1
 		fi
 	fi
 
-	# all checks passed - update active file
-	if [ -e "$activeFile.orig" ] || [ -e "$activeFile.NO_ORIG" ]; then
-		# add file to installed files list (used for uninstallAll)
-		# do this before actually modifying things just in case there's an error
-		#	that way the uninstall is assured
-		echo "$activeFile" >> "$installedFilesList"
-		cp "$sourceFile" "$activeFile"
-
-		updateRestartFlags "$activeFile"
-		thisFileUpdated=true
-		return 0
 	# backup missing (this should not ever happen)
-	else
-		setInstallFailed $EXIT_FILE_SET_ERROR "ERROR: no backup for $baseName - can't continue with install"
+    if ! backupActiveFile "$activeFile" ; then
+		setInstallFailed $EXIT_FILE_SET_ERROR "no backup for $baseName"
 		return 1
 	fi
+
+	# add file to installed files list (used for uninstallAll)
+	# do this before actually modifying things just in case there's an error
+	#	that way the uninstall is assured
+	echo "$activeFile" >> "$installedFilesList"
+
+	# update package flag to prevent overwrites by other packages
+	echo $packageName > "$activeFile.package"
+
+	# update the active file
+	cp "$sourceFile" "$activeFile"
+
+	updateRestartFlags "$activeFile"
+	thisFileUpdated=true
+	return 0
 }
 
 
@@ -471,23 +548,31 @@ updateActiveFile ()
 #
 # returns 0 if active file was restored, 1 if not
 # also sets thisFileUpdated for backwards compatibility with existing setup scripts
+#
+# restore only if the package that updated this file is the current package
+#	failure is indicated in the return code but does not trigger as faliure
 
 restoreActiveFile ()
 {
     thisFileUpdated=false
 
-    local baseName="$(basename $1)"
+	if [ -f "$1.package" ]; then
+		prevousPackage=$( cat "$1.package" )
+		if [ $packageName != $prevousPackage ]; then
+			return 1
+		fi
+    fi
     if [ -e "$1.orig" ]; then
         mv -f "$1.orig" "$1"
-        rm -f "$1.NO_ORIG"
         thisFileUpdated=true
     elif [ -f "$1.NO_ORIG" ]; then
         rm -f "$1"
-        rm -f "$1.NO_ORIG"
         thisFileUpdated=true
     fi
 
 	if $thisFileUpdated ; then
+        rm -f "$1.NO_ORIG"
+        rm -f "$1.package"
 		# remove file from installed file list
 		if [ -f "$installedFilesList" ]; then
 			grep -v "$1" "$installedFilesList" | tee "$installedFilesList" > /dev/null
@@ -550,7 +635,7 @@ _checkFileSets ()
 	
 	# versioned file sets exist but empty file list
 	if [ ! -z $versionList ] && [ -z $fileList ]; then
-        setInstallFailed $EXIT_FILE_SET_ERROR "ERROR: versions exist, but empty file list - can't continue"
+        setInstallFailed $EXIT_FILE_SET_ERROR "versions exist, but empty file list"
         return
 	# no versioned file sets - nothing to validate - allow install
 	elif [ -z $versionList ]; then return; fi
@@ -659,7 +744,7 @@ _checkFileSets ()
     done
 
     if [ -f "$fileSet/INCOMPLETE" ]; then
-        setInstallFailed $EXIT_FILE_SET_ERROR "ERROR: incomplete file set for $venusVersion - can't continue"
+        setInstallFailed $EXIT_FILE_SET_ERROR "incomplete file set for $venusVersion"
 	# if we get this far and fs is not marked INCOMPLETE, then the file set does not need to be checked again next pass
 	else
 		touch "$fileSet/COMPLETE"
@@ -697,6 +782,8 @@ buildUninstallListsfromSetupScript ()
 					param=$( echo ${params[i+1]} | sed s?'$qmlDir'?$qmlDir? )
 				fi
 				if ! [ -z "$param" ] && ! [[ $param == \#* ]]; then
+					# remove any  quotes around parameters
+					param=$( echo $param | sed -e 's?"?? g' -e "s?'?? g" )
 					scriptUninstallFilesList+=("$param")
 				fi
 				;;
@@ -707,6 +794,8 @@ buildUninstallListsfromSetupScript ()
 				if ! [ -z "$param" ] && ! [[ $param == \#* ]]; then
 					scriptUninstallServicesList+=("$param")
 				else
+					# remove any quotes around parameters
+					param=$( echo $param | sed -e 's?"?? g' -e "s?'?? g" )
 					scriptUninstallServicesList+=($packageName)
 				fi
 				;;
@@ -1086,7 +1175,7 @@ if [ -f /etc/venus/machine ]; then
 	machine=$(cat /etc/venus/machine)
 else
 	machine=""
-	setInstallFailed $EXIT_INCOMPATIBLE_VERSION "can't determine Venus device type"
+	setInstallFailed $EXIT_INCOMPATIBLE_PLATFORM "can't determine Venus device type"
 fi
 
 # initialize version strings and numbers for future checks
@@ -1110,8 +1199,6 @@ else
 	packageVersionNumber=0
 fi
 
-logMessage "--- starting setup script $packageVersion"
-
 # collect command line options
 reinstall=false
 force=false
@@ -1147,6 +1234,9 @@ while [ $# -gt 0 ]; do
     shift
 done
 
+# do after logToConsole is enabled/disabled abvove
+logMessage "--- starting setup script $packageVersion"
+
 # make sure rootfs is mounted R/W & and resized to allow space for replacement files
 #	arbitrary minimum size of 3 MB
 rootMinimumSize=3
@@ -1271,13 +1361,13 @@ fi
 versionStringToNumber $firstCompatibleVersion
 firstCompatibleVersionNumber=$versionNumber
 if (( $venusVersionNumber < $firstCompatibleVersionNumber )); then
-	setInstallFailed $EXIT_INCOMPATIBLE_VERSION "ERROR $venusVersion before first compatible $firstCompatibleVersion"
+	setInstallFailed $EXIT_INCOMPATIBLE_VERSION "$venusVersion before first compatible $firstCompatibleVersion"
 elif [ -f "$scriptDir/obsoleteVersion" ]; then
 	obsoleteVersion=$(cat "$scriptDir/obsoleteVersion")
 	versionStringToNumber $obsoleteVersion
 	obsoleteVersionNumber=$versionNumber
 	if (( $venusVersionNumber >= $obsoleteVersionNumber )); then
-		setInstallFailed $EXIT_INCOMPATIBLE_VERSION "ERROR $venusVersion after last compatible $obsoleteVersion"
+		setInstallFailed $EXIT_INCOMPATIBLE_VERSION "$venusVersion after last compatible $obsoleteVersion"
 	fi
 fi
 if ! $installFailed ; then
@@ -1291,9 +1381,6 @@ fi
 #	do final checks before permitting install
 if [ $scriptAction != 'UNINSTALL' ]; then
 
-	# check for package conflicts and prevent installs if there are any
-	###################################
-
 	# determine if GUI v1 is installed
 	#	Note, it may NOT be running or selected to run!!!!
 	if [ ! -d "/opt/victronenergy/gui" ]; then
@@ -1317,17 +1404,20 @@ if [ $scriptAction != 'UNINSTALL' ]; then
 	fi
 
 	if ! $guiV1present && $guiV1required ; then
-		setInstallFailed $EXIT_NO_GUI_V1 "ERROR $packageName requires GUI v1 which is not on the system - can't install"
+		setInstallFailed $EXIT_NO_GUI_V1 "$packageName requires GUI v1"
 	fi
 
 	# attempting an install without the comand line prompting
 	#	and needed options have not been set yet - can't continue
 	if [ $scriptAction == 'INSTALL' ]; then
 		if ! $optionsSet ; then
-			setInstallFailed $EXIT_OPTIONS_NOT_SET "ERROR required options have not been set - can't install"
+			setInstallFailed $EXIT_OPTIONS_NOT_SET "required options have not been set"
 			endScript
 		fi
 	fi
+
+	checkPackageDependencies
+
 	getFileLists "$pkgFileSets"
 
     _checkFileSets
@@ -1360,11 +1450,11 @@ if [ $scriptAction != 'UNINSTALL' ]; then
 		baseName=$( basename $file )
 		patchFile="$patchSourceDir/$baseName.patch"
 		if ! [ -f "$file" ]; then
-			setInstallFailed $EXIT_FILE_SET_ERROR "ERROR: original file missing for $baseName - can't continue with install"
+			setInstallFailed $EXIT_FILE_SET_ERROR "original file missing for $baseName"
 			endScript
 		fi
 		if ! [ -f "$patchFile" ]; then
-			setInstallFailed $EXIT_FILE_SET_ERROR "ERROR: patch file missing for $baseName - can't continue with install"
+			setInstallFailed $EXIT_FILE_SET_ERROR "patch file missing for $baseName"
 			endScript
 		fi
 		# BusyBox patch doesn't support the -o option so copy source to output first
@@ -1376,11 +1466,11 @@ if [ $scriptAction != 'UNINSTALL' ]; then
 		elif [ -e "$file" ] ; then
 			cp "$file" "$patchedFiles/$baseName"
 		else
-			setInstallFailed $EXIT_FILE_SET_ERROR "ERROR: no source for $baseName patch - can't continue with install"
+			setInstallFailed $EXIT_FILE_SET_ERROR "no source for $baseName patch"
 			endScript
 		fi
 		if ! patch "$patchedFiles/$baseName" "$patchFile" &> /dev/null ; then
-			setInstallFailed $EXIT_FILE_SET_ERROR "ERROR: could not patch $baseName - can't continue with install"
+			setInstallFailed $EXIT_FILE_SET_ERROR "could not patch $baseName"
 			endScript
 		fi
 	done
diff --git a/HelperResources/EssentialResources b/HelperResources/EssentialResources
index 24dd56b..aadb94c 100755
--- a/HelperResources/EssentialResources
+++ b/HelperResources/EssentialResources
@@ -62,6 +62,9 @@ EXIT_RUN_AGAIN=250
 EXIT_ROOT_FULL=249
 EXIT_DATA_FULL=248
 EXIT_NO_GUI_V1=247
+EXIT_PACKAGE_CONFLICT=246
+
+
 # old variables - keep for compatibility
 exitReboot=$EXIT_REBOOT
 exitSuccess=$EXIT_SUCCESS
diff --git a/HelperResources/IncludeHelpers b/HelperResources/IncludeHelpers
index a9ca229..9d1c44a 100755
--- a/HelperResources/IncludeHelpers
+++ b/HelperResources/IncludeHelpers
@@ -17,26 +17,26 @@
 #	NOTE: this script uses VersionResources ONLY
 #	it does source any other helper files since final location has not yet been chosen
 
-pkgDir=$( dirname $0 )
+pkgDir="$( cd "$(dirname $0)" >/dev/null 2>&1 ; /bin/pwd -P )"
+pkgRoot="$( dirname "$pkgDir")"
 pkgName=$( basename $pkgDir )
-shDir=$( dirname "$pkgDir" )/SetupHelper
-
+shHelperDir="$pkgRoot/SetupHelper/HelperResources"
 # assume helper files are in package directory - change below if not
 helperResourcesDir="$pkgDir/HelperResources"
 
 if [ -e "$helperResourcesDir/version" ]; then
 	# both helper file sets are present - compare them and choose the newest
-	if [ -e "$shDir/HelperResources" ]; then
+	if [ -e "$shHelperDir" ]; then
 		# temporarily source the local VersionResources
 		source "$pkgDir/HelperResources/VersionResources"
 		# setup helper files are newer - switch to those helper files
-		compareVersions $( cat "$shDir/version" ) $( cat "$pkgDir/HelperResources/version" )
+		compareVersions $( cat "$shHelperDir/version" ) $( cat "$pkgDir/HelperResources/version" )
 		if (( $? == 1 )); then
-			helperResourcesDir="$shDir/HelperResources"
+			helperResourcesDir="$shHelperDir"
 		fi
 	fi
-elif [ -e "$shDir/HelperResources" ]; then
-	helperResourcesDir="$shDir/HelperResources"
+elif [ -e "$shHelperDir" ]; then
+	helperResourcesDir="$shHelperDir"
 else
 	echo "$pkgName: helper files not found - can't continue" | tee -a "/data/log/SetupHelper"
 	exit 1
diff --git a/HelperResources/version b/HelperResources/version
index f467a21..7408476 100644
--- a/HelperResources/version
+++ b/HelperResources/version
@@ -1 +1 @@
-v6.7
+v6.12
diff --git a/changes b/changes
index 5411550..a174982 100644
--- a/changes
+++ b/changes
@@ -1,3 +1,6 @@
+v4.7:
+	removed spi1-1cs.dtbo - unused, and confilcts with VeCanSetup
+
 v4.6:
 	fixed: v4.5 had corrupted file sets (v2.73 and maybe others)
 	cleanup overlay install
diff --git a/packageDependencies b/packageDependencies
new file mode 100644
index 0000000..c768938
--- /dev/null
+++ b/packageDependencies
@@ -0,0 +1 @@
+RemoteGPIO  uninstalled
diff --git a/version b/version
index 70c703f..d0c01d6 100644
--- a/version
+++ b/version
@@ -1 +1 @@
-v4.6
+v4.7