#!/bin/sh

# vim: noexpandtab ts=8 sw=4 softtabstop=4

# Setup a semi-sane environment
PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH
HOME=/root
export HOME
TERM=${TERM:-xterm}
export TERM

AVATAR_PROJECT="TrueNAS"


# Constants for base 10 and base 2 units
: ${kB:=$((1000))}      ${kiB:=$((1024))};       readonly kB kiB
: ${MB:=$((1000 * kB))} ${MiB:=$((1024 * kiB))}; readonly MB MiB
: ${GB:=$((1000 * MB))} ${GiB:=$((1024 * MiB))}; readonly GB GiB
: ${TB:=$((1000 * GB))} ${TiB:=$((1024 * GiB))}; readonly TB TiB

is_truenas()
{

    test "$AVATAR_PROJECT" = "TrueNAS"
    return $?
}

# The old pre-install checks did several things
# 1:  Don't allow going from FreeNAS to TrueNAS or vice versa
# 2:  Don't allow downgrading.  (Not sure we can do that now.)
# 3:  Check memory size and cpu speed.
# This does memory size only for now.
pre_install_check()
{
    # We need at least 8 GB of RAM
    # minus 1 GB to allow for reserved memory
    local minmem=$((7 * GiB))
    local memsize=$(sysctl -n hw.physmem)

    if [ ${memsize} -lt ${minmem} ]; then
	dialog --clear --title "${AVATAR_PROJECT}" --defaultno \
	--yesno "This computer has less than the recommended 8 GB of RAM.\n\nOperation without enough RAM is not recommended.  Continue anyway?" 7 74 || return 1
    fi
    return 0
}

wait_keypress()
{
    local _tmp
    read -p "Press ENTER to continue." _tmp
}

# Constant media size threshold for allowing swap partitions.
: ${MIN_SWAPSAFE_MEDIASIZE:=$((60 * GB))}; readonly MIN_SWAPSAFE_MEDIASIZE

# Check if it is safe to create swap partitions on the given disks.
#
# The result can be forced by setting SWAP_IS_SAFE in the environment to either
# "YES" or "NO".
#
# Sets SWAP_IS_SAFE to "YES" if
#   we are on TrueNAS
#   *or*
#   every disk in $@ is >= ${MIN_SWAPSAFE_MEDIASIZE} and none is USB and user says ok
# Otherwise sets SWAP_IS_SAFE to "NO".
#
# Use `is_swap_safe` to check the value of ${SWAP_IS_SAFE}.
check_is_swap_safe()
{
    # We assume swap is safe on TrueNAS,
    # and we try to use the existing value for ${SWAP_IS_SAFE} if already set.
    if [ -z "${SWAP_IS_SAFE}" ] ; then
	local _disk
	local sector_size
	local sectors
	# Check every disk in $@, aborting if an unsafe disk is found.
	for _disk ; do
	  sector_size=$(blkid -i /dev/${_disk} | sed "s/.*LOGICAL_SECTOR_SIZE=\"\([0-9]\+\)\"/\1/")
	  sectors=$(cat /sys/block/${_disk}/size)
	    if [ $((sector_size * sectors)) -lt ${MIN_SWAPSAFE_MEDIASIZE} ] &&
		grep -qF "0" /sys/block/${_disk}/removable ; then
		SWAP_IS_SAFE="NO"
		break
	    fi
	done
    fi
    # Make sure we have a valid value for ${SWAP_IS_SAFE}.
    # If unset, we are either on TrueNAS or didn't find an unsafe disk.
    case "${SWAP_IS_SAFE:="YES"}" in
	# Accept YES or NO (case-insensitive).
	[Yy][Ee][Ss])
	    # Confirm swap setup
	    if ! dialog --clear --title "${AVATAR_PROJECT}" \
		    --yes-label "Create swap" --no-label "No swap" --yesno  \
		    "Create 16GB swap partition on boot devices?" \
		    7 74 ; then
		SWAP_IS_SAFE="NO"
	    fi
	    ;;
	[Nn][Oo]) ;;
	# Reject other values.
	*)  echo "Ignoring invalid value for SWAP_IS_SAFE: ${SWAP_IS_SAFE}"
	    unset SWAP_IS_SAFE
	    check_is_swap_safe "$@"
	    ;;
    esac
    export SWAP_IS_SAFE
}

# A specialized checkyesno for SWAP_IS_SAFE.
# Returns 0 if it is ok to set up swap on the chosen disks, otherwise 1.
# `check_is_swap_safe` must be called once before calling `is_swap_safe`.
is_swap_safe()
{
    case "${SWAP_IS_SAFE:?}" in
	[Yy][Ee][Ss]) true;;
	*) false;;
    esac
}

# return 0 if no raid devices, or !0 if there are some.
get_raid_present()
{
	local _cnt
	local _dummy

	if [ ! -d "/dev/raid" ] ; then
		return 0;
	fi

	_cnt=0
	ls /dev/raid/ > /tmp/raidfiles
	while read _dummy ; do _cnt=$(($_cnt + 1));done < /tmp/raidfiles
	return $_cnt
}

get_physical_disks_list()
{
    local _disk

    for _disk in $(ls /dev/sd* /dev/vd* /dev/hd* /dev/xvd* | grep -v '[0-9]' | sed 's|/dev/||g')
    do
	VAL="${VAL} ${_disk}"
    done

    VAL=`echo $VAL | tr ' ' '\n'| grep -v '^cd'`
    export VAL
}

get_media_description()
{
    local _media
    local _description
    local _cap

    _media=$1
    if [ -n "${_media}" ]; then
	_description=`sgdisk -p /dev/${_media} | grep "Model:" \
	    | cut -d ' ' -f 2-`
	if [ -z "${_description}" ]; then
	    _description="Unknown Device"
	fi
	_cap=`sgdisk -p /dev/${_media} | grep "Disk /dev/${_media}" | cut -d ' ' -f 5-6`
	echo "${_description} -- ${_cap}"
    fi
}

disk_is_mounted()
{
    local _dev

    for _dev
    do
	if mount -v | grep -qE "^/dev/${_dev}[sp][0-9]+"
	then
	    return 0
	fi
    done
    return 1
}


new_install_verify()
{
    local _type="$1"
    shift
    local _upgradetype="$1"
    shift

    local _disks="$*"
    local _tmpfile="/tmp/msg"
    cat << EOD > "${_tmpfile}"
WARNING:
EOD

    if [ "$_type" = "upgrade" -a "$_upgradetype" = "inplace" ] ; then
      echo "- This will install into existing zpool on ${_disks}." >> ${_tmpfile}
    else
      echo "- This will erase ALL partitions and data on ${_disks}." >> ${_tmpfile}
    fi

    cat << EOD >> "${_tmpfile}"
- You can't use ${_disks} for sharing data.

NOTE:
- Installing on flash media is preferred to installing on a
  hard drive.

Proceed with the ${_type}?
EOD
    _msg=`cat "${_tmpfile}"`
    rm -f "${_tmpfile}"
    dialog --clear --title "$AVATAR_PROJECT ${_type}" --yesno "${_msg}" 13 74
    [ $? -eq 0 ] || exit 1
}

ask_upgrade()
{
    local _disk="$1"
    local _tmpfile="/tmp/msg"
    cat << EOD > "${_tmpfile}"
Upgrading the installation will preserve your existing configuration.

Do you wish to perform an upgrade or a fresh installation on ${_disk}?
EOD
    _msg=`cat "${_tmpfile}"`
    rm -f "${_tmpfile}"
    dialog --title "Upgrade this $AVATAR_PROJECT installation" --no-label "Fresh Install" --yes-label "Upgrade Install" --yesno "${_msg}" 8 74
    return $?
}

ask_upgrade_inplace()
{
    local _tmpfile="/tmp/msg"
    cat << EOD > "${_tmpfile}"
User configuration settings and storage volumes are preserved and not affected by this step.\n\n
The boot device can be formatted to remove old versions, or the upgrade can be installed in a new boot environment without affecting any existing versions.
EOD
    _msg=`cat "${_tmpfile}"`
    rm -f "${_tmpfile}"
    dialog --trim --title "Update Method Selection" --yes-label "Install in new boot environment" --no-label "Format the boot device" --yesno "${_msg}" 0 0
    return $?
}

ask_boot_method()
{
    # If we are not on efi, set BIOS as the default selected option
    dlgflags=""
    if [ "$BOOTMODE" != "UEFI" ] ; then
      dlgflags="--defaultno"
    fi

    local _tmpfile="/tmp/msg"
    cat << EOD > "${_tmpfile}"
$AVATAR_PROJECT can be booted in either BIOS or UEFI mode.

BIOS mode is recommended for legacy and enterprise hardware,
whereas UEFI may be required for newer consumer motherboards.
EOD
    _msg=`cat "${_tmpfile}"`
    rm -f "${_tmpfile}"
    dialog ${dlgflags} --title "$AVATAR_PROJECT Boot Mode" --no-label "Boot via BIOS" --yes-label "Boot via UEFI" --yesno "${_msg}" 8 74
    return $?
}

install_loader()
{
    local _disk _disks
    local _mnt

    _mnt="$1"
    shift
    _disks="$*"

    # Tell GRUB we are booting from ZFS
    echo 'GRUB_CMDLINE_LINUX="root=ZFS=freenas-boot/ROOT/default"' >> ${_mnt}/etc/default/grub
    mkdir -p ${_mnt}/boot/efi
    chroot ${_mnt} update-initramfs -c -k $(uname -r) -v
    chroot ${_mnt} update-grub

    for _disk in $_disks
    do
	    echo "Stamping GPT loader on: /dev/${_disk}"
	    chroot ${_mnt} grub-install --target=i386-pc /dev/${_disk}

	    echo "Stamping EFI loader on: ${_disk}"
	    chroot ${_mnt} mkdosfs -F 32 -s 1 -n EFI /dev/${_disk}2
	    chroot ${_mnt} mount -t vfat /dev/${_disk}2 /boot/efi
	    chroot ${_mnt} grub-install --target=x86_64-efi \
		    --efi-directory=/boot/efi \
		    --bootloader-id=debian --recheck --no-floppy
	    chroot ${_mnt} mkdir -p /boot/efi/EFI/boot
	    chroot ${_mnt} cp /boot/efi/EFI/debian/grubx64.efi /boot/efi/EFI/boot/bootx64.efi
	    chroot ${_mnt} umount /boot/efi
    done

    return 0
}

save_serial_settings()
{
    _mnt="$1"

    # If the installer was booted with serial mode enabled, we should
    # save these values to the installed system

    dmesg|grep "Kernel command line"|grep -q "console=ttyS"
    USESERIAL=$?
    if [ "$USESERIAL" -ne 0 ] ; then return 0; fi

    chroot ${_mnt} sqlite3 /data/freenas-v1.db "update system_advanced set adv_serialconsole = 1"

    tty=$(dmesg|grep -E "ttyS[0-9] at I/O"|head -1|gawk '{print $4}')
    SERIALSPEED=$(setserial -G /dev/$tty|gawk '{print $9}')
    if [ -n "$SERIALSPEED" ] ; then
       chroot ${_mnt} sqlite3 /data/freenas-v1.db "update system_advanced set adv_serialspeed = $SERIALSPEED"
       unit=$(echo $tty|sed 's/[^0-9]//g')
       echo "serial --unit=$unit --speed=$SERIALSPEED --word=8 --parity=no --stop=1" > ${_mnt}/boot/grub/grub.cfg.new
       cat ${_mnt}/boot/grub/grub.cfg >> ${_mnt}/boot/grub/grub.cfg.new
       mv ${_mnt}/boot/grub/grub.cfg.new ${_mnt}/boot/grub/grub.cfg
       sed -i -E "s/(linux.*root=.*)/\\1 console=ttyS0,$SERIALSPEED console=tty0/g" ${_mnt}/boot/grub/grub.cfg
    fi
    SERIALPORT=$(setserial -G /dev/$tty|gawk '{print $5}')
    if [ -n "$SERIALPORT" ] ; then
       chroot ${_mnt} sqlite3 /data/freenas-v1.db "update system_advanced set adv_serialport = '$SERIALPORT'"
    fi

}

mount_disk()
{
	local _mnt

	if [ $# -ne 1 ]; then
		return 1
	fi

	_mnt="$1"
	mkdir -p "${_mnt}"
	mount -t zfs -o noatime freenas-boot/ROOT/${BENAME} ${_mnt}
	mkdir -p ${_mnt}/data
	return 0
}

create_partitions()
{
    local _disk="$1"
    local sector_size=$(blkid -i /dev/${_disk} | sed "s/.*LOGICAL_SECTOR_SIZE=\"\([0-9]\+\)\"/\1/")
    local alignment_multiple=$(expr 4096 / $sector_size)

    # Create BIOS boot partition
    if ! sgdisk -a${alignment_multiple} -n1:0:+1024K -t1:EF02 /dev/${_disk}; then
            return 1
    fi

    # Create EFI partition (Even if not used, allows user to switch to UEFI later)
    if ! sgdisk -n2:0:+524288K -t2:EF00 /dev/${_disk}; then
	    return 1
    fi

    if is_swap_safe; then
        if ! sgdisk -n4:0:+16777216K -t4:8200 /dev/${_disk}; then
            return 1
        fi
    fi

    # Create boot pool
    if ! sgdisk -n3:0:0 -t3:BF01 /dev/${_disk}; then
	    return 1
    fi

    return 0
}

get_minimum_size()
{
    local _min=0
    local _disk
    local _size

    for _disk
    do
	if ! create_partitions ${_disk} 1>&2; then
	    echo "Could not do anything with ${_disk}, skipping" 1>&2
	    continue
	fi
    done

    echo ${_min}
}

# Minimum required space for an installation.
# Docs state 8 GiB is the bare minimum, but we specify 8 GB here for wiggle room.
# That should leave enough slop for alignment, boot partition, etc.
: ${MIN_ZFS_PARTITION_SIZE:=$((8 * GB))}; readonly MIN_ZFS_PARTITION_SIZE

partition_disks()
{
    local _disks _disksparts
    local _mirror
    local _minsize
    local _size

    # Create and destroy existing pool (if exists)
    zpool import -N -f freenas-boot || true
    zpool destroy -f freenas-boot || true

    _disks=$*

    # Erase both typical metadata area.
    for _disk in ${_disks}; do
	sgdisk -Z ${_disk} >/dev/null 2>&1 || true
	sgdisk -Z ${_disk} >/dev/null 2>&1 || true
	#dd if=/dev/zero of=/dev/${_disk} bs=1M count=2 >/dev/null
	#_size=$(diskinfo ${_disk} | cut -f 3)
	#dd if=/dev/zero of=/dev/${_disk} bs=1M oseek=$((_size / MiB - 2)) >/dev/null || true
    done

    check_is_swap_safe ${_disks}
    #_minsize=$(get_minimum_size ${_disks})

    #if [ ${_minsize} -lt ${MIN_ZFS_PARTITION_SIZE} ]; then
    #	echo "Disk is too small to install ${AVATAR_PROJECT}" 1>&2
    #	return 1
    #fi

    _disksparts=$(for _disk in ${_disks}; do
	create_partitions ${_disk} >&2
	echo /dev/${_disk}3
    done)

    if [ $# -gt 1 ]; then
	_mirror="mirror"
    else
	_mirror=""
    fi
    zpool create -f -o cachefile=/tmp/zpool.cache -o ashift=12 -d \
		-o feature@async_destroy=enabled \
		-o feature@bookmarks=enabled \
		-o feature@embedded_data=enabled \
		-o feature@empty_bpobj=enabled \
		-o feature@enabled_txg=enabled \
		-o feature@extensible_dataset=enabled \
		-o feature@filesystem_limits=enabled \
		-o feature@hole_birth=enabled \
		-o feature@large_blocks=enabled \
		-o feature@lz4_compress=enabled \
		-o feature@spacemap_histogram=enabled \
		-o feature@userobj_accounting=enabled \
		-O acltype=posixacl -O canmount=off -O compression=lz4 -O devices=off -O mountpoint=none \
		-O normalization=formD -O relatime=on -O xattr=sa \
		freenas-boot ${_mirror} ${_disksparts}
    zfs set compression=on freenas-boot
    zfs create -o canmount=off freenas-boot/ROOT
    zfs create -o mountpoint=legacy freenas-boot/ROOT/${BENAME}

    return 0
}

disk_is_freenas()
{
    local _disk="$1"
    local _rv=1
    local upgrade_style=""
    local os_part=""
    local data_part=""

    # We have two kinds of potential upgrades here.
    # The old kind, with 4 slices, and the new kind,
    # with two partitions.

    mkdir -p /tmp/data_old
    if [ -c /dev/${_disk}s4 ]; then
	os_part=/dev/${_disk}s1a
	data_part=/dev/${_disk}s4
	upgrade_style="old"
    elif [ -c /dev/${_disk}p2 ]; then
	os_part=/dev/${_disk}p2
	upgrade_style="new"
    else
	return 1
    fi

    if [ "${upgrade_style}" = "new" ]; then
	# This code is very clumsy.  There
	# should be a way to structure it such that
	# all of the cleanup happens as we want it to.
	zdb -l ${os_part} | grep -qF "name: 'freenas-boot'" || return 1
	zpool import -N -f freenas-boot || return 1

	# Now we want to figure out which dataset to use.
	DS=$(zpool list -H -o bootfs freenas-boot | head -n 1 | cut -d '/' -f 3)
	if [ -z "$DS" ]; then
	    zpool export freenas-boot || true
	    return 1
	elif mount -t zfs freenas-boot/ROOT/"${DS}" /tmp/data_old; then
	    # If the active dataset doesn't have a database file,
	    # then it's not FN as far as we're concerned (the upgrade code
	    # will go badly).
	    # We also check for the Corral database directory.
	    if [ ! -f /tmp/data_old/data/freenas-v1.db -o \
		   -d /tmp/data_old/data/freenas.db ]; then
		umount /tmp/data_old || true
		zpool export freenas-boot || true
		return 1
	    fi
	    cp -pR /tmp/data_old/data/. /tmp/data_preserved
	    # Don't want to keep the old pkgdb around, since we're
	    # nuking the filesystem
	    rm -rf /tmp/data_preserved/pkgdb
	    if [ -f /tmp/data_old/conf/base/etc/hostid ]; then
		cp -p /tmp/data_old/conf/base/etc/hostid /tmp/
	    fi
	    if [ -d /tmp/data_old/root/.ssh ]; then
		cp -pR /tmp/data_old/root/.ssh /tmp/
	    fi
	    if [ -d /tmp/data_old/boot/modules ]; then
		mkdir -p /tmp/modules
		for i in `ls /tmp/data_old/boot/modules`
		do
		    cp -p /tmp/data_old/boot/modules/$i /tmp/modules/
		done
	    fi
	    if [ -d /tmp/data_old/usr/local/fusionio ]; then
		cp -pR /tmp/data_old/usr/local/fusionio /tmp/
	    fi
	    if [ -f /tmp/data_old/boot.config ]; then
		cp /tmp/data_old/boot.config /tmp/
	    fi
	    if [ -f /tmp/data_old/boot/loader.conf.local ]; then
		cp /tmp/data_old/boot/loader.conf.local /tmp/
	    fi
	    umount /tmp/data_old || return 1
	    zpool export freenas-boot || return 1
	    return 0
	fi # elif mount ...
    fi # if [ "${upgrade_style}" = "new" ]

    # This is now legacy code, to support the old
    # partitioning scheme (freenas-9.2 and earlier)
    if ! mount "${data_part}" /tmp/data_old ; then
	return 1
    fi

    ls /tmp/data_old > /tmp/data_old.ls
    if [ -f /tmp/data_old/freenas-v1.db ]; then
	_rv=0
    fi
    # XXX side effect, shouldn't be here!
    if [ ! -d "/tmp/data_preserved" ] ; then
        mkdir /tmp/data_preserved
    fi
    cp -pR /tmp/data_old/. /tmp/data_preserved
    umount /tmp/data_old
    if [ $_rv -eq 0 ]; then
	# For GUI upgrades, we only have one OS partition
	# that has conf/base/etc.  For ISO upgrades, we
	# have two, but only one is active.
	slice=$(gpart show ${_disk} | gawk '/\[active\]/ { print $3 }')
	if [ -z "${slice}" ]; then
	    # We don't have an active slice, so something is wrong.
	    return 1
	fi
	mount /dev/${_disk}s${slice}a /tmp/data_old
	ls /tmp/data_old > /dev/null
	if [ ! -d /tmp/data_old/conf/base/etc ]
	then
	    # Mount the other partition
	    if [ "${slice}" -eq 1 ]; then
		slice=2
	    else
		slice=1
	    fi
	    umount /tmp/data_old
	    mount /dev/${_disk}s${slice}a /tmp/data_old
	    ls /tmp/data_old > /dev/null
	fi
	if [ -f /tmp/data_old/conf/base/etc/hostid ]; then
	    cp -p /tmp/data_old/conf/base/etc/hostid /tmp/
	fi
	if [ -d /tmp/data_old/root/.ssh ]; then
	    cp -pR /tmp/data_old/root/.ssh /tmp/
	fi
	if [ -d /tmp/data_old/boot/modules ]; then
	    mkdir -p /tmp/modules
	    for i in `ls /tmp/data_old/boot/modules`
	    do
		cp -p /tmp/data_old/boot/modules/$i /tmp/modules/
	    done
	fi
	if [ -d /tmp/data_old/usr/local/fusionio ]; then
	    cp -pR /tmp/data_old/usr/local/fusionio /tmp/
	fi
	if [ -f /tmp/data_old/boot.config ]; then
	    cp /tmp/data_old/boot.config /tmp/
	fi
	if [ -f /tmp/data_old/boot/loader.conf.local ]; then
	    cp /tmp/data_old/boot/loader.conf.local /tmp/
	fi
	umount /tmp/data_old
    fi
    rmdir /tmp/data_old
    return $_rv
}

prompt_password()
{

    local values value password="" password1 password2 _counter _tmpfile="/tmp/pwd.$$"

    cat << __EOF__ > /tmp/dialogconf
bindkey formfield TAB FORM_NEXT
bindkey formfield DOWN FORM_NEXT
bindkey formfield UP FORM_PREV
bindkey formbox DOWN FORM_NEXT
bindkey formbox TAB FORM_NEXT
bindkey formbox UP FORM_PREV
__EOF__

    export DIALOGRC="/tmp/dialogconf"

    while true; do
	dialog --insecure \
	    --output-fd 3 \
	    --visit-items \
	    --passwordform "Enter your root password; cancel for no root password" \
	    10 50 0 \
	    "Password:" 1 1 "" 0 20 25 50 \
	    "Confirm Password:" 2 1 "" 2 20 25 50 \
	    3> ${_tmpfile}

	if [ $? -ne 0 ]; then
	    rm -f ${_tmpfile}
	    return 1
	fi

	{ read password1 ; read password2; } < ${_tmpfile}
	rm -f ${_tmpfile}

	if [ "${password1}" != "${password2}" ]; then
	    dialog --msgbox "Passwords do not match." 7 60 2> /dev/null
	elif [ -z "${password1}" ]; then
	    dialog --msgbox "Empty password is not secure" 7 60 2> /dev/null
	else
	    password="${password1}"
	    break
	fi

    done

    rm -f ${DIALOGRC}
    unset DIALOGRC


    echo -n "${password}" 1>&2

}

create_be()
{
  if [ $# -ne 1 ]; then
    return 1
  fi

  echo "Creating new Boot-Environment"

  # When upgrading, we will simply create a new BE dataset and install
  # fresh into that, so old datasets are not lost
  zpool import -N -f freenas-boot || return 1

  # Create the new BE
  zfs create -o mountpoint=legacy freenas-boot/ROOT/${BENAME} || return 1

  # Mount the new BE datasets
  mkdir -p ${1}
  mount -t zfs freenas-boot/ROOT/${BENAME} ${1} || return 1
  mkdir -p ${1}/data

  return 0
}

menu_install()
{
    local _action
    local _disklist
    local _tmpfile
    local _answer
    local _cdlist
    local _items
    local _disk
    local _disks=""
    local _realdisks=""
    local _disk_old
    local _config_file
    local _desc
    local _list
    local _msg
    local _i
    local _do_upgrade=""
    local _menuheight
    local _msg
    local _dlv
    local _password
    local os_part
    local data_part
    local upgrade_style="new"
    local whendone=""

    local readonly CD_UPGRADE_SENTINEL="/data/cd-upgrade"
    local readonly NEED_UPDATE_SENTINEL="/data/need-update"
    # create a sentinel file for post-fresh-install boots
    local readonly FIRST_INSTALL_SENTINEL="/data/first-boot"
    local readonly TRUENAS_EULA_PENDING_SENTINEL="/data/truenas-eula-pending"
    local readonly POOL="freenas-boot"

    _tmpfile="/tmp/answer"
    TMPFILE=$_tmpfile
    REALDISKS="/tmp/realdisks"

    while getopts "U:P:X:" opt; do
	case "${opt}" in
	    U)	if ${OPTARG}; then _do_upgrade=1 ; else _do_upgrade=0; fi
		;;
	    P)	_password="${OPTARG}"
		;;
	    X)	case "${OPTARG}" in
		    reboot)	whendone=reboot ;;
		    "wait")	whendone=wait ;;
		    halt)	whendone="shutdown now" ;;
		    *)		whendone="" ;;
		esac
		;;
	    *)	echo "Unknown option ${opt}" 1>&2
		;;
	esac
    done
    shift $((OPTIND-1))
    
    if [ $# -gt 0 ]
    then
	_disks="$@"
	INTERACTIVE=false
    else
	INTERACTIVE=true
    fi

    if ${INTERACTIVE}; then
	pre_install_check || return 0
    fi
    
    if ${INTERACTIVE}; then
	get_physical_disks_list
	_disklist="${VAL}"

	_list=""
	_items=0
	for _disk in ${_disklist}; do
	    _desc=$(get_media_description "${_disk}" | sed "s/'/'\\\''/g")
	    _list="${_list} ${_disk} '${_desc}' off"
	    _items=$((${_items} + 1))
	done
	    
	_tmpfile="/tmp/answer"
	if [ ${_items} -ge 10 ]; then
	    _items=10
	    _menuheight=20
	else
	    _menuheight=9
	    _menuheight=$((${_menuheight} + ${_items}))
	fi
	if [ "${_items}" -eq 0 ]; then
	    # Inform the user
	    eval "dialog --title 'Choose destination media' --msgbox 'No drives available' 5 60" 2>${_tmpfile}
	    return 0
	fi

	eval "dialog --title 'Choose destination media' \
		--checklist 'Select one or more drives where $AVATAR_PROJECT should be installed (use arrow keys to navigate to the drive(s) for installation; select a drive with the spacebar).' \
		${_menuheight} 60 ${_items} ${_list}" 2>${_tmpfile}
	[ $? -eq 0 ] || exit 1
    fi

    if [ -f "${_tmpfile}" ]; then
	_disks=$(eval "echo `cat "${_tmpfile}"`")
	rm -f "${_tmpfile}"
    fi

    if [ -z "${_disks}" ]; then
	${INTERACTIVE} && dialog --msgbox "You need to select at least one disk!" 6 74
	exit 1
    fi

    if disk_is_mounted ${_disks} ; then
        ${INTERACTIVE} && dialog --msgbox "The destination drive is already in use!" 6 74
        exit 1
    fi

    _action="installation"
    _upgrade_type="format"
    # This needs to be re-done.
    # If we're not interactive, then we have
    # to assume _disks is correct.
    # If we do have more than one disk given,
    # we should also do something if they're all
    # freenas disks.  But how do we figure out which
    # one to use?  The current code in disk_is_freenas
    # is very, very heavy -- it actually backs up the
    # data from a freenas installation.  It also does
    # a zpool import.
    for _disk in ${_disks}; do
    if disk_is_freenas ${_disk} ; then
        if ${INTERACTIVE}; then
	    if ask_upgrade ${_disk} ; then
		_do_upgrade=1
		_action="upgrade"
	    fi
	else
	    if [ "${_do_upgrade}" != "0" ]; then
		_do_upgrade=1
		_action="upgrade"
	    fi
	fi
	if [ -c /dev/${_disk}s4 ]; then
	    upgrade_style="old"
	elif [ -c /dev/${_disk}p2 ]; then
	    upgrade_style="new"
	else
	    echo "Unknown upgrade style" 1>&2
	    exit 1
	fi
	# Ask if we want to do a format or inplace upgrade
        if ${INTERACTIVE}; then
	    if ask_upgrade_inplace ; then
		_upgrade_type="inplace"
	    fi
	fi
	break
    fi
    done
    # If we haven't set _do_upgrade by now, we're not
    # doing an upgrade.
    if [ -z "${_do_upgrade}" ]; then
	_do_upgrade=0
    fi

    _realdisks=$_disks

    ${INTERACTIVE} && new_install_verify "$_action" "$_upgrade_type" ${_realdisks}

    if ${INTERACTIVE} && [ "${_do_upgrade}" -eq 0 ]; then
	prompt_password 2> /tmp/password
	if [ $? -eq 0 ]; then
	    _password="$(cat /tmp/password 2> /dev/null)"
	fi
    fi

    if [ ${_do_upgrade} -eq 0 ]; then
	# With the new partitioning, disk_is_freenas may
	# copy /data.  So if we don't need it, remove it,
	# or else it'll do an update anyway.  Oops.
	rm -rf /tmp/data_preserved
    fi
    # Start critical section.
    if ${INTERACTIVE}; then
	trap "set +x; read -p \"The $AVATAR_PROJECT $_action on ${_realdisks} has failed. Press enter to continue.. \" junk" EXIT
    else
#	trap "echo \"The ${AVATAR_PROJECT} ${_action} on ${_realdisks} has failed.\" ; sleep 15" EXIT
	trap "set +x; read -p \"The $AVATAR_PROJECT $_action on ${_realdisks} has failed. Press enter to continue.. \" junk" EXIT
    fi
    set -e
#    set -x

    #  _disk, _image, _config_file

    if [ ${_do_upgrade} -eq 1 ]
    then
        mkdir -p /tmp/data
	if [ "${upgrade_style}" = "old" ]; then
	    # For old style, we have two potential
	    # partitions to look at:  s1a and s2a.
	    # 
	    slice=$(gpart show ${_disk} | gawk '/\[active\]/ { print $3 }')
	    if [ -z "${slice}" ]; then
		# We don't have an active slice, so something is wrong.
		false
	    fi
	    mount /dev/${_disk}s${slice}a /tmp/data
	    ls /tmp/data > /dev/null
	    if [ ! -d /tmp/data/conf/base/etc ]
	    then
		# Mount the other partition
		if [ "${slice}" -eq 1 ]; then
		    slice=2
		else
		    slice=1
		fi
		umount /tmp/data
		mount /dev/${_disk}s${slice}a /tmp/data
		ls /tmp/data > /dev/null
	    fi
            umount /tmp/data
	elif [ "${upgrade_style}" != "new" ]; then
		echo "Unknown upgrade style" 1>&2
		false
	fi
        rmdir /tmp/data
    else
	# Destroy existing partition table, if there is any but tolerate
	# failure.
	for _disk in ${_realdisks}; do
	    sgdisk --zap-all ${_disk} || true
	done
    fi

    if [ ${_do_upgrade} -eq 1 -a ${upgrade_style} = "new" -a "${_upgrade_type}" = "inplace" ]
    then
      # Set the boot-environment name
      BENAME="default-`date +%Y%m%d-%H%M%S`"
      export BENAME

      # When doing new-style upgrades, we can keep the old zpool
      # and instead do a new BE creation
      create_be /tmp/data
    else
      # Set the boot-environment name
      BENAME="default"
      export BENAME

      # We repartition on fresh install, or old upgrade_style
      # This destroys all of the pool data, and
      # ensures a clean filesystems.
      partition_disks ${_realdisks}
      mount_disk /tmp/data
    fi

    if [ -d /tmp/data_preserved ]; then
	cp -pR /tmp/data_preserved/. /tmp/data/data
	# we still need the newer version we are upgrading to's
	# factory-v1.db, else issuing a factory-restore on the
	# newly upgraded system completely horks the system
	cp /data/factory-v1.db /tmp/data/data/
	chown www:www /tmp/data/data/factory-v1.db
    fi

    local OS=FreeNAS
    if is_truenas; then
        OS=TrueNAS
    fi

    # Start the installation
    if ! debootstrap --no-check-gpg \
	    --include=busybox,grub-pc,grub-efi-amd64,shim-signed,zfs-modules-$(uname -r),zfsutils-linux,dosfstools,zfs-initramfs \
	    truenas \
	    /tmp/data file:///cdrom/; then
	    echo "ERROR: Failed debootstrap truenas"
    fi

    # Do apt installation now
    mkdir /tmp/data/cdrom
    mount --rbind /cdrom /tmp/data/cdrom
    # Set local APT source location
    echo "deb [trusted=yes check-date=no] file:///cdrom/ truenas main" \
	    > /tmp/data/etc/apt/sources.list
    chroot /tmp/data apt update
    chroot /tmp/data env DEBIAN_FRONTEND=noninteractive apt install -yq truenas
    umount /tmp/data/cdrom

    # Set remote APT source location
    echo "deb http://apt.tn.ixsystems.com/truenas/unstable/ truenas main" \
	    > /tmp/data/etc/apt/sources.list

    # Always import zfs pool
    cat >/tmp/data/etc/systemd/system/zfs-import-bootpool.service << EOF
[Unit]
DefaultDependencies=no
Before=zfs-import-scan.service
Before=zfs-import-cache.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/sbin/zpool import -N -o cachefile=none freenas-boot || true

[Install]
WantedBy=zfs-import.target
EOF
    # Enable systemd service
    chroot /tmp/data systemctl enable zfs-import-bootpool.service

    if [ "${_do_upgrade}" -ne 0 ]; then
	#if [ -f /tmp/hostid ]; then
        #    cp -p /tmp/hostid /tmp/data/conf/base/etc
	#fi
	if [ -d /tmp/.ssh ]; then
            cp -pR /tmp/.ssh /tmp/data/root/
	fi
	
	# TODO: this needs to be revisited.
	if [ -d /tmp/modules ]; then
            for i in `ls /tmp/modules`
            do
		# If it already exists, simply don't copy it.
		cp -np /tmp/modules/$i /tmp/data/boot/modules || true
            done
	fi
	if [ -d /tmp/fusionio ]; then
            cp -pR /tmp/fusionio /tmp/data/usr/local/
	fi
	if [ -f /tmp/boot.config ]; then
	    cp /tmp/boot.config /tmp/data/
	fi
	if [ -f /tmp/loader.conf.local ]; then
	    cp /tmp/loader.conf.local /tmp/data/boot/
	    # TODO: #7042 - Don't use debug kernel
	    sed -i '' -e 's,^module_path=.*,module_path="/boot/kernel;/boot/modules;/usr/local/modules",g' \
		-e '/^kernel=.*/d' /tmp/data/boot/loader.conf /tmp/data/boot/loader.conf.local
	fi
    fi
    
    # beadm will need a devfs
    mount --rbind /dev /tmp/data/dev
    mount --rbind /proc /tmp/data/proc
    mount --rbind /sys /tmp/data/sys
    # Set default boot filesystem
    zpool set bootfs=freenas-boot/ROOT/${BENAME} freenas-boot
    install_loader /tmp/data ${_realdisks}
    # Save current serial console settings into database.
    save_serial_settings /tmp/data
    
#    set +x
    if [ -d /tmp/data_preserved ]; then
	# Instead of sentinel files, let's just migrate!
	# Unfortunately, this doesn't seem to work well.
	# This should be investigated.
#	chroot /tmp/data /bin/sh -c "/usr/bin/yes | \
#		/usr/local/bin/python
#		      /usr/local/www/freenasUI/manage.py migrate --all --merge"
	# Create upgrade sentinel files
	: > /tmp/data/${CD_UPGRADE_SENTINEL}
	: > /tmp/data/${NEED_UPDATE_SENTINEL}
	${INTERACTIVE} && dialog --msgbox "The installer has preserved your database file.
#$AVATAR_PROJECT will migrate this file, if necessary, to the current format." 6 74
    elif [ "${_do_upgrade}" -eq 0 ]; then
	if [ -n "${_password}" ]; then
		# Set the root password
		chroot /tmp/data echo "root:${_password}" | chroot /tmp/data chpasswd
		chroot /tmp/data /etc/netcli reset_root_pw "${_password}"
	fi
    fi
    : > /tmp/data/${FIRST_INSTALL_SENTINEL}
    if is_truenas && [ "${_do_upgrade}" -eq 0 ]; then
        : > /tmp/data/${TRUENAS_EULA_PENDING_SENTINEL}
    fi

    umount /tmp/data/dev/pts || true
    umount /tmp/data/dev
    umount /tmp/data/proc
    umount /tmp/data/sys
    umount /tmp/data/

    # Export the pool now
    zpool export freenas-boot
    # End critical section.
    set +e

    trap - EXIT

    _msg="The $AVATAR_PROJECT $_action on ${_realdisks} succeeded!\n"
    _msg="${_msg}Please reboot and remove the installation media."
    if ${INTERACTIVE}; then
	dialog --msgbox "$_msg" 6 74
    elif [ -n "${whendone}" ]; then
	case "${whendone}" in
	    halt)	shutdown now ;;
	    "wait")	dialog --msgbox "$_msg" 6 74 ;;
	esac
	return 0
    fi

    return 0
}

menu_shell()
{
    /bin/sh
}

menu_reboot()
{
    echo "Rebooting..."
    reboot >/dev/null
}

menu_shutdown()
{
    echo "Halting and powering down..."
    shutdown now >/dev/null
}

main()
{
    local _tmpfile="/tmp/answer"
    local _number

    if [ $# -gt 0 ]; then
	# $1 will have the device name
	menu_install "$@"
	exit $?
    fi

    while :; do

        dialog --clear --title "$AVATAR_PROJECT $AVATAR_VERSION Console Setup" --menu "" 12 73 6 \
            "1" "Install/Upgrade" \
            "2" "Shell" \
            "3" "Reboot System" \
            "4" "Shutdown System" \
            2> "${_tmpfile}"
        _number=`cat "${_tmpfile}"`
        case "${_number}" in
            1) menu_install ;;
            2) menu_shell ;;
            3) menu_reboot ;;
            4) menu_shutdown ;;
        esac
    done
}

# Parse a config file.
# We don't do much in the way of error checking.
# Format is very simple:
# <opt>=<value>
# <value> may be a list (e.g., disk devices)
# The output is suitable to be used as the arguments
# to main(), which will directl ycall menu_install().

yesno()
{
    # Output "true" or "false" depending on the argument
    if [ $# -ne 1 ]; then
	echo "false"
	return 0
    fi
    case "$1" in
	[yY][eE][sS] | [tT][rR][uU][eE])	echo true ;;
	*)	echo false;;
    esac
    return 0
}

getsize()
{
    # Given a size specifier, convert it to bytes.
    # No suffix, or a suffix of "[bBcC]", means bytes;
    # [kK] is 1024, etc.
    if [ $# -ne 1 ]; then
	echo 0
	return 0
    fi
    case "$1" in
	*[bB][cC])	expr "$1" : "^\([0-9]*\)[bB][cC]" || echo 0;;
	*[kK])	expr $(expr "$1" : "^\([0-9]*\)[kK]") \* 1024 || echo 0;;
	*[mM])	expr $(expr "$1" : "^\([0-9]*\)[gG]") \* 1024 \* 1024 || echo 0;;
	*[gG])	expr $(expr "$1" : "^\([0-9]*\)[gG]") \* 1024 \* 1024 \* 1024 || echo 0;;
	*[tT])	expr $(expr "$1" : "^\([0-9]*\)[tT]") \* 1024 \* 1024 \* 1024 \* 1024 || echo 0;;
	*) expr "$1" : "^\([0-9]*\)$" || echo 0;;
    esac
    return 0
}
	
main "$@"
