#!/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
}

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

CURVER=$(sed -n 's/^Version \+: \+\([^ ]\+\)$/\1/p' \
           ${vyatta_sysconfdir}/version 2>/dev/null)
if [ -z "$CURVER" ]; then
  failure_exit 'Cannot find current version.'
fi

# get new version string. this is from the squashfs image.
NEWVER=$(grep '^Version ' ${CD_SQUASH_ROOT}${vyatta_sysconfdir}/version \
          | tr -s ' ' | cut -d ' ' -f 3)
if [ -z "$NEWVER" ]; then
  failure_exit 'Cannot find new release version.'
fi
if [ "$CURVER" == "$NEWVER" ]; then
  failure_exit "Cannot install the same release version \"$NEWVER\"."
fi

# start the install
echo "Installing \"$NEWVER\" release."

# 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

# create the new release directories
REL_ROOT=$BOOT_DIR/$NEWVER
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 -dp $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 if needed
def_cfg="$VYATTA_CFG_DIR/config.boot"
if [ -f "$def_cfg" ]; then
  resp=''
  while [ -z "$resp" ]; do
    echo 'Would you like to use the current 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
      cp -p $def_cfg $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..."
  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 >>$new_grub_cfg
  sed -n '/^menuentry/,${p}' $old_grub_cfg >>$new_grub_cfg
  sed -i 's/^set default=[0-9]\+$/set default=0/' $new_grub_cfg
  mv $new_grub_cfg $old_grub_cfg
fi

echo 'Done.'

# done
exit 0