Added shellscript compactor to reduce size of self-extract script
authorDan
Tue, 23 Jun 2009 09:51:01 -0400
changeset 1 dbce0b16b0c1
parent 0 8e044e762a6c
child 2 7dddb5fb1595
Added shellscript compactor to reduce size of self-extract script
compact-shellscript.sed
generate.sh
self-extract-src.sh
self-extract.sh
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compact-shellscript.sed	Tue Jun 23 09:51:01 2009 -0400
@@ -0,0 +1,7 @@
+#!/bin/sed -f
+# Remove comments, except for the shebang
+s/^ *#[^!#].*$//
+# Remove spaces at the beginning of all lines
+s/^ *//
+# Remove empty lines
+/^$/d
--- a/generate.sh	Tue Jun 23 09:18:34 2009 -0400
+++ b/generate.sh	Tue Jun 23 09:51:01 2009 -0400
@@ -59,7 +59,10 @@
   exit 1
 fi
 
-printf "Pulling latest code..."
+printf "Compacting self-extraction script..."
+sed -f compact-shellscript.sed self-extract-src.sh > self-extract.sh || fail "Failed to generate compacted self-extract script"
+
+printf "\nPulling latest code..."
 hg -R $REPO archive -r $REVISION -t tgz enano-$VERSION.tar.gz || fail "Could not pull revision $REVISION from Mercurial repo"
 printf "\nExtracting..."
 tar xzCf `dirname $0`/stage enano-$VERSION.tar.gz || fail "Could not extract tarball"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/self-extract-src.sh	Tue Jun 23 09:51:01 2009 -0400
@@ -0,0 +1,160 @@
+#!/bin/bash
+
+if test -z "$BASH"; then
+  cmd="$0"
+  if test "`basename $0`" = "$0"; then
+    cmd="./$0"
+  fi
+  echo "This script is designed to be run using GNU bash."
+  echo "Try: chmod +x $0 && $cmd"
+  exit 1
+fi
+
+my_which_real()
+{
+  test -n "$1" || return 1
+  local i
+  local part
+  for (( i=1; ; i++ )); do
+    part=`echo "$PATH" | cut -d ':' -f $i`
+    test -n "$part" || return 1
+    if test -x "$part/$1"; then
+      echo "$part/$1"
+      return 0
+    fi
+  done
+}
+
+my_which()
+{
+  local out
+  local r
+  out=`my_which_real "$1"`
+  r=$?
+  if [ -z "$out" -o $r = 1 ]; then
+    echo "ERROR: $1 command not found on system" 1>&2
+    exit 1
+  fi
+  echo $out
+  return $r
+}
+
+# Show the extraction progress spinner.
+# Spins the propeller one time for each line on stdin
+progressbit()
+{
+  local i
+  local pbar
+  local line
+  i=0
+  pbar="-\\|/"
+  while read line; do
+    ((i++))
+    j=$(($i % ${#pbar}))
+    echo -ne "\r${pbar:$j:1} Extracting..."
+  done
+}
+
+# Create a temp directory. Very bad in terms of randomness source, but hey, I'm a security major. Can't help it.
+mktempdir()
+{
+  local tempdir
+  tempdir=/tmp/ext`$dd if=/dev/urandom bs=128 count=1 2>/dev/null | $md5sum - | cut -c 1-6`
+  echo $tempdir
+  return 0
+}
+
+# Outputs the raw compressed tarball data on stdout.
+getdata()
+{
+  offset=`$cat $0 | $grep -an '^##DATA' | cut -d ':' -f 1 || exit 1`
+  offset=$((offset + 1))
+  $cat $0 | $tail -n+$offset
+}
+
+# Go through each required command, and make sure it's on the system. If not, fail.
+for cmd in bzip2 tar grep tail cat wc dd md5sum; do
+  my_which $cmd>/dev/null || exit 1
+  eval $cmd=`my_which $cmd`
+  if test x$? = x1; then
+    exit 1
+  fi
+done
+
+tempdir=""
+extractonly=0
+while test -n "$1"; do
+  case "$1" in
+    -x)
+      extractonly=1
+      if test -n "$2"; then
+        tempdir=$2
+      else
+        tempdir=`echo $0 | sed -re 's/\.[a-z0-9_-]{1,5}$//'`
+      fi
+      ;;
+  esac
+  shift
+done
+
+# Make sure we have a working temp directory
+test -n "$tempdir" || tempdir=`mktempdir`
+
+if [ -d "$tempdir" ]; then
+  if [ $extractonly = 1 ]; then
+    i=0
+    basetemp="$tempdir"
+    while test -d "$tempdir"; do
+      tempdir="${basetemp}${i}"
+      ((i++))
+    done
+  else
+    while test -d "$tempdir"; do
+      tempdir=`mktempdir`
+    done
+  fi
+fi
+
+mkdir "$tempdir" || exit 1
+
+# Finally, extract the data.
+echo -ne "\e[?25l- Extracting..."
+getdata | $bzip2 -dc | $tar xvCf $tempdir - | progressbit \
+  || (
+      rm -rf $tempdir
+      echo -ne "\e[?25h"
+      exit 1
+     )
+
+# clear out the extraction progress
+echo -ne "\r\e[?25h"
+
+# trap exits so we can clean up if the script is interrupted
+handle_interrupt()
+{
+  rm -rf $tempdir
+  # added in rev. 2, interrupts should cause an error exit
+  exit 1
+}
+trap handle_interrupt 0
+
+if test -x $tempdir/autorun.sh && test $extractonly != 1; then
+  # Run the autorun script
+  $tempdir/autorun.sh
+  ret=$?
+  rm -rf $tempdir
+  exit $ret
+else
+  # Just let the user know where the files are and die.
+  echo "Contents extracted to $tempdir."
+fi
+
+# free our trap on signals
+trap "exit 0;" 0
+
+# done!
+exit 0
+
+# don't touch this marker! Everything after this is expected to be binary data.
+
+##DATA
--- a/self-extract.sh	Tue Jun 23 09:18:34 2009 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,137 +0,0 @@
-#!/bin/bash
-
-if test -z "$BASH"; then
-	cmd="$0"
-	if test "`basename $0`" = "$0"; then
-		cmd="./$0"
-	fi
-	echo "This script is designed to be run using GNU bash."
-	echo "Try: chmod +x $0 && $cmd"
-	exit 1
-fi
-
-my_which_real()
-{
-	test -n "$1" || return 1
-	local i
-	local part
-	for (( i=1; ; i++ )); do
-		part=`echo "$PATH" | cut -d ':' -f $i`
-		test -n "$part" || return 1
-		if test -x "$part/$1"; then echo "$part/$1"; return 0; fi
-	done
-}
-
-my_which()
-{
-	local out
-	local r
-	out=`my_which_real "$1"`
-	r=$?
-	if test -z "$out" -o $r = 1 ; then echo "ERROR: $1 command not found on system" 1>&2;exit 1;fi
-	echo $out
-	return $r
-}
-
-progressbit()
-{
-	local i
-	local pbar
-	local line
-	i=0
-  pbar="-\\|/"
-  while read line; do
-    ((i++))
-    j=$(($i % ${#pbar}))
-    echo -ne "\r${pbar:$j:1} Extracting..."
-  done
-}
-
-mktempdir()
-{
-	local tempdir
-	tempdir=/tmp/ext`$dd if=/dev/urandom bs=128 count=1 2>/dev/null | $md5sum - | cut -c 1-6`
-	echo $tempdir
-	return 0
-}
-
-getdata()
-{
-	offset=`$cat $0 | $grep -an '^##DATA' | cut -d ':' -f 1 || exit 1`
-	offset=$((offset + 1))
-	$cat $0 | $tail -n+$offset
-}
-
-for cmd in bzip2 tar grep tail cat wc dd md5sum; do
-	my_which $cmd>/dev/null || exit 1
-	eval $cmd=`my_which $cmd`
-	if test x$? = x1; then
-		exit 1
-	fi
-done
-
-tempdir=""
-extractonly=0
-while test -n "$1"; do
-	case "$1" in
-	-x)
-		extractonly=1
-		if test -n "$2"; then
-      tempdir=$2
-    else
-      tempdir=`echo $0 | sed -re 's/\.[a-z0-9_-]{1,5}$//'`
-    fi
-		;;
-	esac
-	shift
-done
-
-test -n "$tempdir" || tempdir=`mktempdir`
-
-if test -d "$tempdir"; then
-if test $extractonly = 1; then
-	i=0
-  basetemp="$tempdir"
-  while test -d "$tempdir"; do
-    tempdir="${basetemp}${i}"
-    ((i++))
-  done
-else
-	while test -d "$tempdir"; do
-    tempdir=`mktempdir`
-  done
-fi
-fi
-
-mkdir "$tempdir" || exit 1
-echo -ne "\e[?25l- Extracting..."
-getdata | $bzip2 -dc | $tar xvCf $tempdir - | progressbit \
-  || (
-      rm -rf $tempdir
-      echo -ne "\e[?25h"
-      exit 1
-     )
-
-echo -ne "\r\e[?25h"
-
-# trap exits so we can clean up if the script is interrupted
-handle_interrupt()
-{
-  rm -rf $tempdir
-}
-trap handle_interrupt 0
-
-if test -x $tempdir/autorun.sh && test $extractonly != 1; then
-	$tempdir/autorun.sh
-	ret=$?
-	rm -rf $tempdir
-	exit $ret
-else
-	echo "Contents extracted to $tempdir."
-fi
-
-trap "exit 0;" 0
-
-exit 0
-
-##DATA