#!/bin/bash

if [ $(id -u) != 0 ]; then
    echo "Image installation requires root privileges!"
    exit 1
fi

# source in the functions
source /opt/vyatta/sbin/install-functions

# the INSTALL_LOG env var should be exported by the "caller".
# it will be used to log messages.

# the install partition e.g. sda1
ROOT_PARTITION=$1

becho "Mounting /dev/$ROOT_PARTITION..."

# mount the partition
mkdir -p $WRITE_ROOT
if ! try_mount "/dev/$ROOT_PARTITION $WRITE_ROOT"; then
  echo 'Exiting...'
  exit 1
fi

version=$(get_new_version)
image_name=$version
if [ -z "$image_name" ]; then
  echo 'Cannot find new version. Exiting...'
  exit 1
fi

echo -n "What would you like to name this image? [$image_name]: "
if [ "$VYATTA_PROCESS_CLIENT" == "gui2_rest" ]; then
    response=$NEWNAME
else
    read response
fi
if [ -n "$response" ]; then
    badchars=`echo $response | sed -e 's/[a-zA-Z0-9\.\_+-]//g'`
    if [ -n "$badchars" ]; then
        echo "Image name must be composed of a-z, A-Z, 0-9, or one of ._+-"
        exit 1
    fi
    image_name=$response
fi

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

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

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

# make the dir for the new version 
mkdir -p $WRITE_ROOT/boot/$image_name
# make dir for backing store
rw_dir=$WRITE_ROOT/boot/$image_name/rw
mkdir -p $rw_dir
work_dir=$WRITE_ROOT/boot/$image_name/work
mkdir -p $work_dir

echo Copying squashfs image...
# these are the defaults if installing from a specified ISO image file.
# in such cases, the ISO image has already been mounted by caller.
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
  # maybe installing from a live CD boot?
  squash_img=/lib/live/mount/medium/live/filesystem.squashfs
  boot_dir=/boot
  boot_files=$(find $boot_dir -maxdepth 1 -type f -o -type l 2>/dev/null)
  if [ ! -f "$squash_img" ] || [ -z "$boot_files" ]; then
    # not a live CD boot either. give up.
    becho 'Cannot find the squashfs image. Exiting...'
    exit 1
  fi
fi

target_squash=$WRITE_ROOT/boot/$image_name/$version.squashfs
cp -p $squash_img $target_squash
echo Copying kernel and initrd images...
cp -dp $boot_files $WRITE_ROOT/boot/$image_name/

# create persistence.conf file
echo "/ union" > $WRITE_ROOT/persistence.conf

# set up union root for postinst
mkdir -p $INST_ROOT $READ_ROOT
if ! try_mount "-o loop,ro -t squashfs $target_squash $READ_ROOT"; then
  echo 'Exiting...'
  exit 1
fi
margs=$(gen_mopts "overlay" $rw_dir $READ_ROOT $work_dir $INST_ROOT)
if ! try_mount "$margs"; then
  echo 'Exiting...'
  exit 1
fi

becho "Done!"

exit 0

