self-extract-src.sh
author Dan
Tue, 23 Jun 2009 09:55:13 -0400
changeset 2 7dddb5fb1595
parent 1 dbce0b16b0c1
child 4 c6c431cf0a89
permissions -rw-r--r--
Me and my obsession with getting files to exactly 2048 bytes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
     1
#!/bin/bash
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
     2
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
     3
if test -z "$BASH"; then
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
     4
  cmd="$0"
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
     5
  if test "`basename $0`" = "$0"; then
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
     6
    cmd="./$0"
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
     7
  fi
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
     8
  echo "This script is designed to be run using GNU bash."
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
     9
  echo "Try: chmod +x $0 && $cmd"
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    10
  exit 1
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    11
fi
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    12
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    13
my_which_real()
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    14
{
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    15
  test -n "$1" || return 1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    16
  local i
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    17
  local part
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    18
  for (( i=1; ; i++ )); do
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    19
    part=`echo "$PATH" | cut -d ':' -f $i`
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    20
    test -n "$part" || return 1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    21
    if test -x "$part/$1"; then
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    22
      echo "$part/$1"
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    23
      return 0
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    24
    fi
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    25
  done
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    26
}
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    27
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    28
my_which()
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    29
{
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    30
  local out
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    31
  local r
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    32
  out=`my_which_real "$1"`
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    33
  r=$?
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    34
  if [ -z "$out" -o $r = 1 ]; then
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    35
    echo "ERROR: $1 command not found on system" 1>&2
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    36
    exit 1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    37
  fi
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    38
  echo $out
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    39
  return $r
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    40
}
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    41
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    42
# Show the extraction progress spinner.
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    43
# Spins the propeller one time for each line on stdin
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    44
progressbit()
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    45
{
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    46
  local i
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    47
  local pbar
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    48
  local line
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    49
  i=0
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    50
  pbar="-\\|/"
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    51
  while read line; do
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    52
    ((i++))
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    53
    j=$(($i % ${#pbar}))
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    54
    echo -ne "\r${pbar:$j:1} Extracting..."
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    55
  done
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    56
}
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    57
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    58
# Create a temp directory. Very bad in terms of randomness source, but hey, I'm a security major. Can't help it.
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    59
mktempdir()
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    60
{
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    61
  local tempdir
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    62
  tempdir=/tmp/ext`$dd if=/dev/urandom bs=128 count=1 2>/dev/null | $md5sum - | cut -c 1-6`
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    63
  echo $tempdir
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    64
  return 0
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    65
}
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    66
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    67
# Outputs the raw compressed tarball data on stdout.
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    68
getdata()
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    69
{
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    70
  offset=`$cat $0 | $grep -an '^##DATA' | cut -d ':' -f 1 || exit 1`
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    71
  offset=$((offset + 1))
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    72
  $cat $0 | $tail -n+$offset
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    73
}
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    74
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    75
# Go through each required command, and make sure it's on the system. If not, fail.
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    76
for cmd in bzip2 tar grep tail cat wc dd md5sum; do
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    77
  my_which $cmd>/dev/null || exit 1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    78
  eval $cmd=`my_which $cmd`
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    79
  if test x$? = x1; then
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    80
    exit 1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    81
  fi
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    82
done
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    83
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    84
tempdir=""
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    85
extractonly=0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    86
while test -n "$1"; do
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    87
  case "$1" in
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    88
    -x)
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    89
      extractonly=1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    90
      if test -n "$2"; then
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    91
        tempdir=$2
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    92
      else
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    93
        tempdir=`echo $0 | sed -re 's/\.[a-z0-9_-]{1,5}$//'`
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    94
      fi
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    95
      ;;
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    96
  esac
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
    97
  shift
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    98
done
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
    99
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   100
# Make sure we have a working temp directory
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   101
test -n "$tempdir" || tempdir=`mktempdir`
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   102
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   103
if [ -d "$tempdir" ]; then
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   104
  if [ $extractonly = 1 ]; then
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   105
    i=0
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   106
    basetemp="$tempdir"
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   107
    while test -d "$tempdir"; do
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   108
      tempdir="${basetemp}${i}"
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   109
      ((i++))
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   110
    done
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   111
  else
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   112
    while test -d "$tempdir"; do
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   113
      tempdir=`mktempdir`
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   114
    done
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   115
  fi
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   116
fi
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   117
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   118
mkdir "$tempdir" || exit 1
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   119
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   120
# Finally, extract the data.
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   121
echo -ne "\e[?25l- Extracting..."
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   122
getdata | $bzip2 -dc | $tar xvCf $tempdir - | progressbit \
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   123
  || (
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   124
      rm -rf $tempdir
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   125
      echo -ne "\e[?25h"
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   126
      exit 1
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   127
     )
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   128
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   129
# clear out the extraction progress
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   130
echo -ne "\r\e[?25h"
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   131
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   132
# trap exits so we can clean up if the script is interrupted
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   133
handle_interrupt()
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   134
{
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   135
  rm -rf $tempdir
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   136
  # added in rev. 2, interrupts should cause an error exit
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   137
  exit 1
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   138
}
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   139
trap handle_interrupt 0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   140
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   141
if test -x $tempdir/autorun.sh && test $extractonly != 1; then
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   142
  # Run the autorun script
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   143
  $tempdir/autorun.sh
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   144
  ret=$?
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   145
  rm -rf $tempdir
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   146
  exit $ret
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   147
else
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   148
  # Just let the user know where the files are and die.
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   149
  echo "Contents extracted to $tempdir."
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   150
fi
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   151
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   152
# free our trap on signals
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   153
trap "exit 0;" 0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   154
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   155
# done!
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   156
exit 0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   157
1
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   158
# don't touch this marker! Everything after this is expected to be binary data.
dbce0b16b0c1 Added shellscript compactor to reduce size of self-extract script
Dan
parents: 0
diff changeset
   159
0
8e044e762a6c First revision, but working.
Dan
parents:
diff changeset
   160
##DATA