From 2105183cee6c9fc558f86a34d5dd291fae495c24 Mon Sep 17 00:00:00 2001
From: Brett Parker <arch@sommitrealweird.co.uk>
Date: Sun, 4 Sep 2005 18:52:55 +0000
Subject: [PATCH] Add command line option support

Added command line option support
git-archimport-id: arch@sommitrealweird.co.uk--2005-desktop/rpmbased-dist-chroot--mainline--0.1--patch-1
---
 README                  |  18 +++++-
 rpmbased-dist-chroot.sh | 129 +++++++++++++++++++++++++++++++---------
 2 files changed, 118 insertions(+), 29 deletions(-)

diff --git a/README b/README
index e96f8ea..90a9e82 100644
--- a/README
+++ b/README
@@ -20,5 +20,19 @@ Copy the file rpmbased-dist-chroot.sh to a directory in your PATH.
 
 Usage
 -----
-Edit the script and change the variables at the top to point to the right
-places, run, rejoice!
+
+rpmbased-dist-chroot.sh --install-root /some/place/to/put/the/chroot \
+   --rpm-archives /path/to/CD/RPMS/directory --install-packages list of \
+   packages seperated by spaces
+
+All options are optional, if they're not specified then it uses the following
+defaults:
+	install-root    : /mnt/temp/fedora
+	rpm-archives    : /media/cdrom/Fedora/RPMS
+	install-packages: bash coreutils yum
+
+Author
+------
+Brett Parker:
+	email: <iDunno@sommitrealweird.co.uk>
+	www:   http://www.sommitrealweird.co.uk/
diff --git a/rpmbased-dist-chroot.sh b/rpmbased-dist-chroot.sh
index 7b222c8..5aaf4ea 100755
--- a/rpmbased-dist-chroot.sh
+++ b/rpmbased-dist-chroot.sh
@@ -19,7 +19,8 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-set -f
+# Default to expecting a Fedora CD mounted at /media/cdrom
+# and installing bash, coreutils and yum to /mnt/temp/fedora
 
 RPMArchives=/media/cdrom/Fedora/RPMS
 InstallRoot=/mnt/temp/fedora
@@ -30,12 +31,76 @@ ourcachedirectory=$(mktemp -d)
 architecture=$(uname -m)
 export ARCH=$architecture
 
+function displayHelp() {
+cat <<END
+Usage: $0 [options]
+
+Options:
+	--install-root <directorytoinstallto>
+	--rpm-archives <pathtorpms>
+	--install-packages <listofpackagestoinstall>
+END
+	exit $1
+}
+
+argcommand=""
+for option in $@; do
+	if [[ ! $option =~ ^-- ]]; then
+		if [ "x$argcommand" == "xinstall-root" ]; then
+			InstallRoot=$option
+		elif [ "x$argcommand" == "xrpm-archives" ]; then
+			RPMArchives=$option
+		elif [ "x$argcommand" == "xinstall-packages" ]; then
+			if [ "x$installpackages" == "x" ]; then
+				installpackages=$option
+			else
+				installpackages="$installpackages $option"
+			fi
+		fi
+	else
+		if [[ $option =~ [=] ]]; then
+			argcommand=${option%%=*}
+			argcommand=${argcommand##--}
+			option=${option##*=}
+
+			if [ "x$argcommand" == "xinstall-root" ]; then
+				InstallRoot=$option
+			elif [ "x$argcommand" == "xrpm-archives" ]; then
+				RPMArchives=$option
+			elif [ "x$argcommand" == "xinstall-packages" ]; then
+				installpackages=$option
+			else
+				echo "Unknown option $option"
+				exit 1
+			fi
+		else
+			if [ "x$option" == "x--help" ]; then
+				displayHelp 0
+			elif [ "x$option" == "x--install-root" ]; then
+				argcommand=${option##--}
+			elif [ "x$option" == "x--rpm-archives" ]; then
+				argcommand=${option##--}
+			elif [ "x$option" == "x--install-packages" ]; then
+				argcommand=${option##--}
+				installpackages=""
+			else
+				echo "Unknown option $option"
+				exit 1
+			fi
+		fi
+	fi
+done
+
+echo "Install Packages: $installpackages"
+echo "RPMArchives     : $RPMArchives"
+echo "InstallRoot     : $InstallRoot"
+
 function buildcache() {
 	cd $RPMArchives
 
 	for file in *.{noarch,$architecture}.rpm; do
-		rpm -qpl $file | sed 's#^#'$file':#' >> $ourcachedirectory/fedora-files-cache
-		rpm -qp --provides $file | cut -d " " -f 1 | sed 's#^#'$file':#' >> $ourcachedirectory/fedora-package-cache
+		rpm -qpl $file | sed 's#^#'$file':#' >> $ourcachedirectory/files-cache
+		rpm -qp --provides $file | cut -d " " -f 1 | sed 's#^#'$file':#' >> $ourcachedirectory/package-cache
 	done
 }
 
@@ -49,9 +114,9 @@ function findrpm() {
 	escapename=$(echo $name | sed -e 's#\+#\\+#g; s#\.#\\.#g; s#(#\\(#g; s#)#\\)#g;')
 
 	if [ ${name:0:1} == "/" ]; then
-		grep -E "^.*:$escapename\$" $ourcachedirectory/fedora-files-cache | cut -d ":" -f 1 | head -n 1
+		grep -E "^.*:$escapename\$" $ourcachedirectory/files-cache | cut -d ":" -f 1 | head -n 1
 	else
-		grep -E "^.*:$escapename\$" $ourcachedirectory/fedora-package-cache | cut -d ":" -f 1 | head -n 1
+		grep -E "^.*:$escapename\$" $ourcachedirectory/package-cache | cut -d ":" -f 1 | head -n 1
 	fi
 }
 
@@ -64,9 +129,16 @@ function getdependencies() {
 			fname=$(findrpm $line)
 			packagename=$(getpackagename $fname)
 			if [ ! -z $name ] && [ ! -z $packagename ]; then
-				if ( ! grep '^'$fname' ' $ourcachedirectory/buildfedora-packagelist  > /dev/null ) && ( ! rpm -r ${InstallRoot} -qv $packagename > /dev/null ); then
-					echo $fname $packagename >> $ourcachedirectory/buildfedora-packagelist
-					getdependencies $fname $packagename
+				if [[ $firstpackage == 0 ]]; then
+					if ( ! grep '^'$fname' ' $ourcachedirectory/build-packagelist > /dev/null ) && ( ! rpm -r ${InstallRoot} -qv $packagename > /dev/null ); then
+						echo $fname $packagename >> $ourcachedirectory/build-packagelist
+						getdependencies $fname $packagename
+					fi
+				else
+					if ( ! grep '^'$fname' ' $ourcachedirectory/build-packagelist > /dev/null ); then
+						echo $fname $packagename >> $ourcachedirectory/build-packagelist
+						getdependencies $fname $packagename
+					fi
 				fi
 			fi
 		fi
@@ -94,30 +166,33 @@ buildcache
 
 rpm -r ${InstallRoot} --initdb
 
-# install setup and filesystem
-
-echo $(findrpm setup)
-echo $(findrpm filesystem)
-
-rpm -r ${InstallRoot} --ignorearch --install ${RPMArchives}/$(findrpm setup)
-echo rpm -r ${InstallRoot} --ignorearch --install ${RPMArchives}/$(findrpm setup) >> $ourcachedirectory/install-log
-rpm -r ${InstallRoot} --ignorearch --install ${RPMArchives}/$(findrpm filesystem)
-echo rpm -r ${InstallRoot} --ignorearch --install ${RPMArchives}/$(findrpm filesystem) >> $ourcachedirectory/install-log
+firstpackage=1
 
 for package in $installpackages; do
-	rm -f ~/.buildfedora-packagelist
-	touch ~/.buildfedora-packagelist
+	
+	rm -f $ourcachedirectory/build-packagelist
+	touch $ourcachedirectory/build-packagelist
 
 	echo "Installing packages required for $package"
-
-	getdependencies $(findrpm $package) $package
-	
-	echo rpm -r ${InstallRoot} --ignorearch --install $(cat ~/.buildfedora-packagelist | cut -d ' ' -f 1) >> ~/.fedora-install-log
 	
-	cd ${RPMArchives} && rpm -r ${InstallRoot} --ignorearch --install $(cat ~/.buildfedora-packagelist | cut -d ' ' -f 1)
-	if ( ! grep " $package\$" ~/.buildfedora-packagelist > /dev/null 2>/dev/null ); then
-		echo rpm -r ${InstallRoot} --ignorearch --install $(findrpm $package) >> $ourcachedirectory/install-log
-		cd ${RPMArchives} && rpm -r ${InstallRoot} --ignorearch --install $(findrpm $package)
+	if [[ "x$(findrpm $package)" != "x" ]]; then
+		
+		getdependencies $(findrpm $package) $package
+		echo " RPM File    :" $(findrpm $package)
+		echo " Dependencies:"
+		cat $ourcachedirectory/build-packagelist | cut -d ' ' -f 2 | sed -e 's#^#  #;'
+		
+		echo rpm -r ${InstallRoot} --ignorearch --install $(cat $ourcachedirectory/build-packagelist | cut -d ' ' -f 1) >> $ourcachedirectory/install-log
+
+		firstpackage=0
+		
+		cd ${RPMArchives} && rpm -r ${InstallRoot} --ignorearch --install $(cat $ourcachedirectory/build-packagelist | cut -d ' ' -f 1)
+		if ( ! grep " $package\$" $ourcachedirectory/build-packagelist > /dev/null 2>/dev/null ); then
+			echo rpm -r ${InstallRoot} --ignorearch --install $(findrpm $package) >> $ourcachedirectory/install-log
+			cd ${RPMArchives} && rpm -r ${InstallRoot} --ignorearch --install $(findrpm $package)
+		fi
+	else
+		echo " Failed to find the RPM providing $package - skipping"
 	fi
 done
 
-- 
2.39.5