#!/bin/sh

# Copyright (c) ULBSD Project Team. All rights reserved.
# $Id: ulbsd-clone 2 2020-04-06 16:44:15Z sv $

mnt_dir="/mnt"
DIALOG="cdialog"
shutdown="shutdown -p now"
reboot="shutdown -r now"
lang=$1

console="`tty | awk -F '/dev/ttyv' '{print $2}'`"

case "${console}" in
    0|1|2|3|4|5|6|7|8)
        console="`expr -e 0${console} + 1`"
        vidcontrol -s${console}
    ;;
esac

lang="`echo $LANG | awk -F '.' '{print $1}'`"

# ---------- Select language ----------

if [ "${lang}" == "ru_RU" ]
then
    . ${0}-msg-ru
else
    . ${0}-msg-en
fi

# ---------- Main menu ----------

input="`${DIALOG} --stdout --no-collapse --no-cancel --backtitle "${backtitle}" --menu "${main_menu_msg}" 11 44 4 'Clone' "${main_menu_item_clone}" 'Reboot' "${main_menu_item_reboot}" 'Shutdown' "${main_menu_item_shutdown}" 'Exit' "${main_menu_item_exit}"`"

answer="$?"
case "${answer}" in
    0)
    ;;
    *)
    $0 $lang && exit
    ;;
esac

if [ "${input}" == "Reboot" ]
then
    ${reboot} && exit
fi

if [ "${input}" == "Shutdown" ]
then
    ${shutdown} && exit
fi

if [ "${input}" == "Exit" ]
then
    exit
fi

if [ "${input}" == "Clone" ]
then

# ---------- Select destination disk ----------

    avail_disks="`sysctl -n kern.disks | sed -E 's/[[:<:]](cd|ar)[0-9]+//g'`"
    avail_disks_info=""
    list_disks=""
    count_disks="0"

    for cur_disk in ${avail_disks}
    do
        count_disks="`expr -e ${count_disks} + 1`"
        disk_size="`diskinfo ${cur_disk} | awk -F ' ' '{print $3}'`"
        disk_size="`expr -e 0${disk_size} / 1024 / 1024 / 1024`"
        list_disks="${list_disks} ${cur_disk} ${disk_size}GB"
        cur_disk_sn="`camcontrol identify ${cur_disk} 2>&1 | grep 'serial number' | tr -d 'serial number'`"
        if [ "${cur_disk_sn}" == "" ]
        then
            cur_disk_sn="`camcontrol inquiry ${cur_disk} -S`"
            if [ "${cur_disk_sn}" == "" ]
            then
                cur_disk_sn='UNKNOWN'
            fi
        fi
        avail_disks_info="${avail_disks_info}${cur_disk}: `camcontrol devlist | grep -w ${cur_disk} | grep -o '<.*>'` SN:${cur_disk_sn}\n"
    done

    if [ "${avail_disks}" == "" ]
    then
        ${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --msgbox "${disk_no_avail_msgbox_msg}" 5 40
        $0 $lang && exit
    fi

    break_cycle="0"
    while [ "$break_cycle" == "0" ]
    do
        dlg_size="`expr -e ${count_disks} + 7`"
        input="`${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --cancel-label "${cancel_label}" --help-button --help-label "${disk_menu_help_label}" --menu "${disk_menu_msg}" ${dlg_size} 48 ${count_disks} ${list_disks}`"

        answer="$?"
        case "${answer}" in
            2)
                avail_disks_info="`echo ${avail_disks_info} | tr '\t' ' '`"
                ${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --msgbox "${disk_info_msgbox_msg}\n\n${avail_disks_info}" "`expr -e ${count_disks} + 7`" 76
            ;;
            0)
                break_cycle="1"
            ;;
            *)
            $0 $lang && exit
            ;;
        esac
    done

    work_disk="0"

    for cur_disk in ${avail_disks}
    do
        if [ "${cur_disk}" == "${input}" ]
        then
            work_disk="${input}"
        fi
    done

    if [ "${work_disk}" == "0" ]
    then
        ${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --msgbox "${incorrect_msgbox_msg}" 5 32
        $0 $lang && exit
    fi

# ---------- Enter swap size ----------

    swap_size="`sysctl hw.physmem | awk -F ':' '{print $2}'`"
    swap_size_min="`expr -e ${swap_size} / 1024 / 1024`"
    swap_size="`expr -e ${swap_size_min} + ${swap_size_min}`"
    swap_size_max="`expr -e ${swap_size} + ${swap_size} + ${swap_size}`"
    swap_size_min="0"

    break_cycle="0"
    while [ "$break_cycle" == "0" ]
    do

        input="`${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --cancel-label "${cancel_label}" --inputbox "${swap_inputbox_msg}" 12 52 ${swap_size}`"

        answer="$?"
        case "${answer}" in
            0)
                input="`echo ${input} | xargs`"
                check="`echo ${input} | tr -d '[0-9]'`"
                if [ "${check}" == "" ]
                then
                    input="`expr -e 0${input} + 0`"
                fi
                if [ "${check}" == "" ] && [ "${input}" -le ${swap_size_max} ] && [ "${input}" -ge ${swap_size_min} ]
                then
                    swap_size="${input}"
                    break_cycle="1"
                else
                    ${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --msgbox "${incorrect_msgbox_msg}" 5 32
                fi
            ;;
            *)
            $0 $lang && exit
            ;;
        esac
    done

# ---------- Select file system ----------

    avail_file_systems="UFS ZFS"
    input="`${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --cancel-label "${cancel_label}" --menu "${fs_menu_msg}" 9 59 2 'UFS' "${fs_menu_item_ufs}" 'ZFS' "${fs_menu_item_zfs}"`"

    answer="$?"
    case "${answer}" in
        0)
        ;;
        *)
        $0 $lang && exit
        ;;
    esac

    file_system="0"

    for cur_fs in ${avail_file_systems}
    do
        if [ "${cur_fs}" == "${input}" ]
        then
            file_system="${input}"
        fi
    done

    if [ "${file_system}" == "0" ]
    then
        ${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --msgbox "${incorrect_msgbox_msg}" 5 32
        $0 $lang && exit
    fi

# ---------- Enter root partition label and ZFS pool name ----------

    label="root"
    break_cycle="0"
    while [ "$break_cycle" == "0" ]
    do

        input="`${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --cancel-label "${cancel_label}" --inputbox "${label_inputbox_msg}" 13 48 ${label}`"

        answer="$?"
        case "${answer}" in
            0)
                input="`echo ${input} | xargs`"
                check="`echo ${input} | tr -d '[a-zA-Z0-9_]'`"
                if [ "${check}" == "" ] && [ "${input}" != "" ] && [ "${input}" != "swap" ]
                then
                    label="${input}"
                    break_cycle="1"
                else
                    ${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --msgbox "${incorrect_msgbox_msg}" 5 32
                fi
            ;;
            *)
            $0 $lang && exit
            ;;
        esac
    done

# ---------- Start installation ----------

    ${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --yes-label "${yes_label}" --no-label "${no_label}" --yesno "${final_yesno_msg_part1}${work_disk}${final_yesno_msg_part2}" 6 52

    answer="$?"
    case "${answer}" in
        0)
        ;;
        *)
        $0 $lang && exit
        ;;
    esac

    install_script="${0}-ufs"

    if [ "${file_system}" == "ZFS" ]
    then
        install_script="${0}-zfs"
    fi

    ${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --prgbox "${clone_prgbox_msg}" "${install_script} ${work_disk} ${swap_size} ${mnt_dir} ${label} 2>&1" 24 74

    ${DIALOG} --stdout --no-collapse --backtitle "${backtitle}" --msgbox "${alldone_msgbox_msg}" 7 48
    $0 $lang && exit
fi
