
#!/bin/bash

# $Id$
#

# Script to update the default NST password on a ISO image. If you run
# this prior to burning the CD, you'll be able to have your own custom
# password.

usage() {
  cat <<UEOF
Usage:
  $(basename "$0") PASS ISO

Where:
  PASS - New password to put into ISO image
  ISO  - ISO image to update

This script is used to change the default NST password in a ISO image
file. It was originally created for the ISO image:

  nst-1.8.0.iso

It WILL NOT WORK if you try it on a different ISO image!
UEOF
  exit 1
}

#
# This offset is specific to version: 1.8.0
#
OFS="36438016"
PASS="${1}"
ISO="${2}"

PLEN="${#PASS}"

#
# See if password offset has been determined
#

if [ "$OFS" = "tbd" -o "$((OFS+0))" != "${OFS}" ]; then
  cat <<EOF
Sorry, but we are still working on the 1.8.0 release of the NST.

When we make a public release of the ISO image, this script will be
updated to included the necessary code to modify the password field.
EOF
  exit 1
fi

#
# See if we need to show usage
#

if [ "$#" != "2" ]; then
  usage
fi

if (( $PLEN < 6 )); then
  printf "\n***ERROR*** $PLEN is too short for password (at least 6 chars)\n"
  usage
fi

if (( $PLEN > 30 )); then
  printf "\n***ERROR*** $PLEN is too long for password (at most 30 chars)\n"
  usage
fi

if [ ! -f "${ISO}" ]; then
  printf "\n***ERROR*** could not find ISO image file: ${ISO}\n"
  usage
fi

PASS="$(echo "$PASS" | tr\
 'Tg8WfiYPNrvlkeKZFUnHRpLA1m4O7d92aBJxwQs5bocyGjuCDhtzq6MSV3I0XE'\
 'IkUYuMVLOhPWnSKEXv2s0Fr4atdD3lqTeJy7poNC1xZRBbjGz96mAQwgcf5iH8')"

CHECK="$(dd if="$ISO" skip=${OFS} bs=1 count=15 2>/dev/null)"

if [ "$CHECK" != "NST_BOOT_PASSWD" ]; then
  printf "\n***ERROR*** appears to be incorrect ISO image did not find\n"
  printf "NST_BOOT_PASSWD string at offset ${OFS}.\n"
  exit 1
fi

echo "Updating password line in ISO image at offset ${OFS}..."

printf "NST_BOOT_PASSWD=%-32s" '"'${PASS}'"' | \
  dd of="$ISO" seek=${OFS} bs=1 conv=notrunc 2>/dev/null

echo "Done. Good luck."
