build-image: Make device skipping more generic

Currently, build-image skips device preparation stages and doesn't even
need a device connected, unlike other options. Make it more generic so
that in the future other options can also use such functionality. Also
results in a cleanup.

Signed-off-by: Joel Fernandes <joel@joelfernandes.org>
diff --git a/androdeb b/androdeb
index a489835..127c74a 100755
--- a/androdeb
+++ b/androdeb
@@ -13,6 +13,7 @@
 # Set default vars
 DISTRO=buster; ARCH=arm64
 ADB="adb"
+SKIP_DEVICE=0			# Skip device preparation
 
 # Default packages
 DEFAULT_PACKAGES+="bash
@@ -60,7 +61,7 @@
     --tempdir) TDIR="$2"; shift || true;     shift || true;     ;;
     --buildtar) TARDIR="$2"; shift || true;     shift || true;     ;;
     --device|-s) ADB="$ADB -s $2"; shift || true; shift || true; ;;
-    --build-image) BUILD_IMAGE=$2; shift || true; shift || true; ;;
+    --build-image) BI=1; BUILD_IMAGEF=$2; SKIP_DEVICE=1; shift || true; shift || true; ;;
     --debug) set -x; shift || true; ;;
     *) c_error "Unknown option ($1)"; usage; ;;
 esac
@@ -111,7 +112,7 @@
 
 if [[ ! -z ${TARDIR+x} ]] && [[ ! -d $TARDIR ]]; then die 7 "Tar dir specified doesn't exist"; fi
 
-if [ -z "$BUILD_IMAGE" ]; then
+if [ -z $BI ]; then
 	do_adb_root "$ADB"
 
 	if [ $? -ne 0 ]; then
@@ -123,6 +124,8 @@
 		c_error "packages on your distro, or by installing the Android SDK."
 		die 3 "Exiting."
 	fi
+else
+	[ ! -z $BUILD_IMAGEF ] || die 8 "--build-image passed but no image file provided"
 fi
 
 if [ ! -z "$REMOVE" ]; then
@@ -267,19 +270,17 @@
 
 if [[ $EUID -ne 0 ]]; then c_info "The next stage runs as sudo, please enter password if asked."; fi
 
-SKIP_COMPRESS=0; if [ ! -z "$BUILD_IMAGE" ]; then SKIP_COMPRESS=1; fi
-
 ex_files=$(mktemp); echo $EXTRA_FILES > $ex_files
 
 sudo $spath/buildstrap $ARCH $DISTRO $TDIR $OUT_TMP \
 		"$(make_csv "$PACKAGES")"\
-		$ex_files $INSTALL_BCC $SKIP_COMPRESS
+		$ex_files $INSTALL_BCC $SKIP_DEVICE
 rm $ex_files
 
 # If we only wanted to prepare a rootfs and don't have
 # a device connected, then just echo that and skip cleanup
-if [ ! -z "$BUILD_IMAGE" ]; then
-	c_info "For --build-image, only the image is built."
+if [ $SKIP_DEVICE -eq 1 ]; then
+	c_info "Device preparation is being skipped for the selected options"
 	c_info "any builds that need to happen on device may be cloned but not built."
 	sudo $spath/buildimage $OUT_TMP $(dirname $BUILD_IMAGE)/$(basename $BUILD_IMAGE)
 	sudo chmod a+rw $(dirname $BUILD_IMAGE)/$(basename $BUILD_IMAGE)
diff --git a/buildstrap b/buildstrap
index 50def86..5eca01f 100755
--- a/buildstrap
+++ b/buildstrap
@@ -11,7 +11,7 @@
 PACKAGES=$5
 EXTRA_FILES="$(cat $6)"
 INSTALL_BCC=$7
-SKIP_COMPRESS=$8
+SKIP_DEVICE=$8		# Skip any device-specific stages
 
 time qemu-debootstrap --arch $ARCH --include=$PACKAGES \
 	$DISTRO $OUT_TMP http://deb.debian.org/debian/
@@ -65,7 +65,7 @@
 # Should be really do this?
 chmod -R 0777 $TDIR/
 
-if [ $SKIP_COMPRESS -eq 1 ]; then exit 0; fi
+if [ $SKIP_DEVICE -eq 1 ]; then exit 0; fi
 
 c_info "Compressing new filesystem to prepare to push to Android /data/androdeb/"
 tar -zcf $TDIR/deb.tar.gz -C $TDIR debian