#!/bin/bash

# this script installs a new release image into a running system.
# the new ISO image must be already mounted by caller.
# the script sets up a new union mount for the new release. a reboot
# is then required to boot into the newly installed release.

# the current install type: "union" or "old"
CUR_INSTALL=$1

source /opt/vyatta/sbin/install-functions

failure_exit () {
  echo "$*"
  exit 1
}

# Determine the default menuentry index for the grub.cfg file
get_grub_index () {
    cur_index=`grep "^set default=" $BOOT_DIR/grub/grub.cfg | \
	awk -F= '{print $2}'`

    (( cur_index++ ))

    cur_line=`grep "^menuentry" $BOOT_DIR/grub/grub.cfg | \
	tail -n +${cur_index} - | head -n 1 -`

    is_kvm=`echo $cur_line | grep KVM`
    is_serial=`echo $cur_line | grep Serial`
    
    # index 0 is KVM, 1 is Serial
    if [ -n "$is_serial" ]; then
	echo 1
    else
	echo 0
    fi
}

if [ `whoami` != 'root' ] ; then
  failure_exit 'This script must be run with root privileges.'
fi

# On image-installed systems, the image name can be found as the
# directory under "/boot" on the path to the running kernel on the
# boot line. On non-image-installed systems, this yelds the
# name of the kernel image file.
CURVER=`awk '{print $1}' /proc/cmdline`
CURVER=${CURVER#BOOT_IMAGE=/boot/}
CURVER=${CURVER%/vmlinuz*}

if [ -z "$CURVER" ]; then
  failure_exit 'Cannot find current version.'
fi

# get new version string. this is from the squashfs image.
NEWVER=`dpkg -l --root=${CD_SQUASH_ROOT} | grep "^..  vyatta-version " | awk '{print $3}'`
NEWNAME=$NEWVER

echo -n "What would you like to name this image? [$NEWNAME]: "
read response
if [ -n "$response" ]; then
    NEWNAME=$response
fi

# Validate image name
if [ "$NEWNAME" = "grub" -o "${NEWNAME:0:7}" = "vmlinuz" -o \
    "${NEWNAME:0:6}" = "initrd" -o "${NEWNAME:0:10}" = "System.map" -o \
    "$NEWNAME" = "Old-non-image-installation" ]; then
    echo "Can't use $NEWNAME.  It is a reserved image name."
    exit 1;
fi

if [ -z "$NEWNAME" ]; then
  failure_exit 'Invalid image name.'
fi

echo "OK.  This image will be named: $NEWNAME"

# this is the default if current install is union
BOOT_DIR=/live/image/boot
if [ "$CUR_INSTALL" == 'old' ]; then
  BOOT_DIR=/boot
elif [ "$CUR_INSTALL" != 'union' ]; then
  echo 'Invalid current install type. Exiting...'
  exit 1
fi

if [ -d $BOOT_DIR/$NEWNAME ]; then
    if [ "$CURVER" = "$NEWNAME" ]; then
           echo "$NEWNAME is the image you are currently running.  Can't"
	   echo "Re-install over the running image."
	   exit 1
    fi

    echo "An image named $NEWNAME is already installed on this system."
    echo "Proceeding with this installation will delete this copy of"
    echo "$NEWNAME and replace it with a new copy."
    echo -n "Do you want to replace it? (Yes/No) [No]: "
    resp=$(get_response "No" "Yes No Y N")
    if [ "$resp" != 'yes' ] && [ "$resp" != 'y' ]; then
	echo "OK.  Will not replace $NEWNAME"
	echo "Exiting..."
	exit 1
    fi
fi

# start the install
echo "Installing \"$NEWNAME\" image."

# create the new release directories
REL_ROOT=$BOOT_DIR/$NEWNAME
RW_DIR="$REL_ROOT/live-rw"
if ! mkdir -p "$RW_DIR"; then
  failure_exit 'Cannot create directory for new release.'
fi

# copy the squashfs image and boot files
echo "Copying new release files..."
squash_img=${CD_ROOT}/live/filesystem.squashfs
boot_dir=${CD_SQUASH_ROOT}/boot
boot_files=$(find $boot_dir -maxdepth 1 -type f -o -type l 2>/dev/null)
if [ ! -f "$squash_img" ] || [ -z "$boot_files" ]; then
  becho 'Cannot find the files. Exiting...'
  exit 1
fi
target_squash=$REL_ROOT/$NEWVER.squashfs 
cp -p $squash_img $target_squash >&/dev/null
cp --no-dereference --preserve=all $boot_files $REL_ROOT/ >&/dev/null

# mount copied squashfs
if ! try_mount "-o loop,ro $target_squash $READ_ROOT"; then
  failure_exit 'Failed to mount new squashfs image.'
fi

# set up root for postinst
margs="-t unionfs -o noatime,dirs=$RW_DIR=rw:$READ_ROOT=ro unionfs"
margs="$margs $INST_ROOT"
if ! try_mount "$margs"; then
  failure_exit 'Failed to set up root directory for postinst.'
fi

# set up /var/run fstab entry
PI_FSTAB=$INST_ROOT/etc/fstab
if ! grep -q 'tmpfs /var/run ' $PI_FSTAB >&/dev/null; then
  # replace the fstab. the default one has header that will cause
  # it to be wiped out on live boot.
  echo 'tmpfs /var/run tmpfs nosuid,nodev 0 0' >$PI_FSTAB
fi

# save current config dir if needed
if [ -f "$VYATTA_CFG_DIR/config.boot" ]; then
  resp=''
  while [ -z "$resp" ]; do
    echo 'Would you like to save the current configuration '
    echo 'directory and use the current start-up configuration '
    echo -n 'for the new version? (Yes/No) [Yes]: '
    resp=$(get_response "Yes" "Yes No Y N")
    if [ "$resp" == 'yes' ] || [ "$resp" == 'y' ]; then
      echo 'Copying current configuration...'
      ndir=${INST_ROOT}${VYATTA_CFG_DIR}
      mkdir -p $ndir
      find $VYATTA_CFG_DIR -maxdepth 1 -mindepth 1 \
        -exec cp '-a' '{}' "$ndir/" ';'
      chgrp -R vyattacfg $ndir
      chmod -R 775 $ndir
    fi
  done
fi

# postinst hook
PI_SCRIPT=${INST_ROOT}${vyatta_sysconfdir}/install-image/postinst
if [ -e "$PI_SCRIPT" ]; then
  echo "Running post-install script..."
  $PI_SCRIPT $INST_ROOT
fi

# set up grub entry (if provided)
DEF_GRUB=${INST_ROOT}${vyatta_sysconfdir}/grub/default-union-grub-entry
if [ -e "$DEF_GRUB" ]; then
  echo "Setting up grub configuration..."
  new_index=$(get_grub_index)

  def_grub_vers=/tmp/def_grub.$$
  cp $DEF_GRUB $def_grub_vers
  sed -i "s/menuentry \"Vyatta.*(/menuentry \"Vyatta image $NEWNAME (/" $def_grub_vers
  sed -i "s/menuentry \"Lost password change.*(/menuentry \"Lost password change $NEWNAME (/" $def_grub_vers
  sed -i "sX/boot/[A-Za-z0-9\.\-]*X/boot/${NEWNAME}Xg" $def_grub_vers

  old_grub_cfg=$BOOT_DIR/grub/grub.cfg
  new_grub_cfg=/tmp/grub.cfg.$$
  sed -n '/^menuentry/q;p' $old_grub_cfg >$new_grub_cfg
  cat $def_grub_vers >> $new_grub_cfg
  sed -n '/^menuentry/,${p}' $old_grub_cfg >>$new_grub_cfg
  sed -i "s/^set default=[0-9]\+$/set default=$new_index/" $new_grub_cfg
  mv $new_grub_cfg $old_grub_cfg
fi

echo 'Done.'

# done
exit 0