From 5ba546134dbdb6b764644756268f7c187851b7c7 Mon Sep 17 00:00:00 2001
From: William Harrington <kb0iic@berzerkula.org>
Date: Tue, 23 Mar 2021 13:13:02 -0500
Subject: Cleanup packageinstall.sh and get toplevel directory using tar and
 reduce the complexity of extracting then moving the source.

---
 packageinstall.sh | 38 +++++++++++++++++---------------------
 1 file changed, 17 insertions(+), 21 deletions(-)

diff --git a/packageinstall.sh b/packageinstall.sh
index 3e899a1..a890ded 100644
--- a/packageinstall.sh
+++ b/packageinstall.sh
@@ -1,3 +1,5 @@
+#!/bin/bash
+
 CHAPTER="$1"
 PACKAGE="$2"
 
@@ -5,19 +7,18 @@ if [ -f "${LFS}/sources/chapter${CHAPTER}/${PACKAGE}" ]; then
     echo -e "${GRN}Package ${YLW}${PACKAGE} ${GRN}already built and installed for ${YLW}Chapter ${CHAPTER}.${RST}"
 
 else
-    grep -i "^${PACKAGE}" packages.csv | grep -i -v "\.patch;" | while read line; do
-        VERSION="$(echo $line | cut -d\, -f2)"
-        URL="$(echo $line | cut -d\, -f3 | sed "s/@/${VERSION}/g")"
+    grep -i "^${PACKAGE}" packages.csv | grep -i -v "\.patch;" | while read -r line; do
+        VERSION="$(echo "$line" | cut -d\, -f2)"
+        URL="$(echo "$line" | cut -d\, -f3 | sed "s/@/${VERSION}/g")"
         CACHEFILE="$(basename "${URL}")"
-        DIRNAME="$(echo "${CACHEFILE}" | sed 's/\(.*\)\.tar\..*/\1/')"
+        #DIRNAME="$(echo "${CACHEFILE}" | sed 's/\(.*\)\.tar\..*/\1/')"
+		DIRNAME="$(tar -tf "${CACHEFILE}" | sed -e 's@/.*@@' | uniq)"
 
-        if [ -d "${DIRNAME}" ]; then
-            rm -rf ${DIRNAME}
-        fi
-        mkdir -p "${DIRNAME}"
+		# Remove existing if exists
+        rm -rf "${DIRNAME}"
 
         echo -ne "${IBLU}Extracting ${YLW}${CACHEFILE}... "
-        tar xf "${CACHEFILE}" -C "${DIRNAME}" >/dev/null 2>&1 & pid=$!
+		tar xf "${CACHEFILE}" > /dev/null 2>&1 & pid=$!
         spinner "$pid"
 		retval=$?
 		if [ "$retval" -ne 0 ]; then
@@ -25,31 +26,26 @@ else
 			return $retval
 		fi
 
-        #TOPLEVELDIR = "tar -tf ${CACHEFILE} | sed -e 's@/.*@@' | uniq"
-
-        pushd "${DIRNAME}" > /dev/null
-            if [ "$(ls -1A | wc -l)" == "1" ]; then
-				find */ -mindepth 1 -maxdepth 1 -exec mv -t ./ -- {} +
-            fi
+        pushd "${DIRNAME}" > /dev/null || return 1
 
             echo -ne "\n${CYN}Compiling ${YLW}${PACKAGE}${RST}... "
             sleep 5
 
-            mkdir -p "../log/chapter${CHAPTER}/"
-            source "../chapter${CHAPTER}/${PACKAGE}.sh" 2>&1 > "../log/chapter${CHAPTER}/${PACKAGE}.log" 2>&1 & pid=$!
+            mkdir -p "${LFS}/sources/log/chapter${CHAPTER}/"
+            source "${LFS}/sources/chapter${CHAPTER}/${PACKAGE}.sh" 2>&1 > "${LFS}/sources/log/chapter${CHAPTER}/${PACKAGE}.log" 2>&1 & pid=$!
             spinner "$pid"
             retval=$?
             if [ "$retval" -ne 0 ]; then
                 echo -e "\n${GRN}Compiling ${YLW}${PACKAGE} ${RED}FAILED!${RST}"
-                popd > /dev/null
+                popd > /dev/null || return 1
                 return $retval
             else
                 echo -e "\n${GRN}Done Compiling ${YLW}${PACKAGE}${RST}"
-                touch ${LFS}/sources/chapter${CHAPTER}/${PACKAGE}
-                rm -rf ${LFS}/sources/${DIRNAME}
+                touch "${LFS}"/sources/chapter"${CHAPTER}"/"${PACKAGE}"
+                rm -rf "${LFS}"/sources/"${DIRNAME}"
             fi
 
-        popd > /dev/null
+        popd > /dev/null || return 1
 
     done
 fi
-- 
cgit v1.2.3-54-g00ecf