#!/usr/bin/env bash
# cli-installer for antiX
# written by Burt Holland
# heavily edited by anticapitalista

export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=cli-installer

# Default
DISTRO=MX-Linux
LIVEUSER=demo

reset_color() { echo -ne "\e[0m"; }
set_blue_color() { echo -ne "\e[34m"; }
set_cyan_color() { echo -ne "\e[36m"; }
set_green_color() { echo -ne "\e[32m"; }
set_red_color() { echo -ne "\e[31m"; }
set_yellow_color() { echo -ne "\e[33m"; }

title() {
    set_green_color
    echo $"CLI installer for $DISTRO"
    echo $"Version 2025.04.16"
    reset_color
    echo
}

header() {
    #set_cyan_color
    #echo $"Requirements for minimum installation:"
    #reset_color
    #echo
    #echo $"antiX-full: hard-disk 5.0GB  RAM 64MB"
    #echo
    #echo $"antiX-base: hard-disk 3.5GB  RAM 48MB"
    #echo
    #echo $"antiX-core: hard-disk 1.0GB  RAM 48MB"
    #echo
    #echo $"antiX-net:  hard-disk 0.7GB  RAM 48MB"
    #echo
    #echo $"Make sure you are connected to "
    #echo
    #echo $"the net BEFORE running this installer"
    #echo
    echo $"This installer does NOT offer the option for using a separate boot partition"
    echo
    echo $"This installer does NOT offer encryption"
    echo
    echo $"If you want the above mentioned features, use our gui installer"
    echo
    echo $"Press Ctrl+C to abort this script"
    echo
}

help_text() {
    echo $"Usage: cli-installer [drive]"
    echo $"Where drive is the name of the drive $DISTRO is to be installed on."
    echo $"   For example: cli-installer sdb"
    echo $"The default is correct if the computer has only one drive."
    echo
    # Tips for this version
    set_blue_color
    echo $"Pre-installation tips:"
    reset_color
    echo
    echo $"Set language at the boot screen by pressing F2"
    echo $"or by adding the line lang=xx where xx is your language code."
    echo $"eg lang=gr for Greek."
    echo
    echo $"Set timezone at the boot screen by pressing F3"
    echo
    echo $"Use kbd=dvorak for dvorak keyboard before installing"
    # End tips
    echo
    echo $"Press F1 at the boot screen for Help and list of cheatcodes for live use."
}

drive_check() {
    # Check for valid known drive device name schema
    drive=$1
    case $drive in
    hd*|sd*|vd*)
        root_prefix="${drive:0:2}"
        if [[ ${drive:2:1} =~ [a-z] ]]; then
            group_drv="${drive:2:1}"
            if [[ ${drive:3:2} =~ ([1-9]?[0-9]) ]]; then
                group_part="${drive:3:2}"
            fi
        fi
    ;;

    mmcblk*)
        root_prefix="${drive:0:6}"
        if [[ ${drive:6:1} =~ [0-9] ]]; then
            group_drv="${drive:6:1}"
            if [[ ${drive:7:3} =~ (p[1-9]?[0-9]) ]]; then
                group_part="${drive:7:3}"
            fi
        fi
    ;;

    nvme*)
        if [[ ${drive:4:1} =~ [0-9] ]]; then
            root_prefix="${drive:0:5}"
            if [[ ${drive:5:2} =~ n[1-9] ]]; then
                group_drv="${drive:5:2}"
                if [[ ${drive:7:3} =~ (p[1-9]?[0-9]) ]]; then
                    group_part="${drive:7:3}"
                fi
            fi
        fi
    ;;

    md*)
        root_prefix="${drive:0:2}"
        if [[ ${drive:2:1} =~ [0-9] ]]; then
            group_part="${drive:2:1}"
        fi
    ;;

    xvd*) # Qubes drives
        root_prefix="${drive:0:3}"
        if [[ ${drive:3:1} =~ [a-z] ]]; then
            group_drv="${drive:3:1}"
            if [[ ${drive:4:2} =~ ([1-9]?[0-9]) ]]; then
                group_part="${drive:4:2}"
            fi
        fi
    ;;

    esac
    drive_check_result="$root_prefix$group_drv$group_part"
}

get_home_partition() {
    while true; do
        read -rp "/home partition (hda1, sda2, etc): " home_drv

        drive_check "$home_drv"

        if [[ ("$home_drv" == "$root_drv") || ("$home_drv" != "$drive_check_result") || ! -b "/dev/$home_drv" ]]; then
            echo $"$home_drv invalid. Retry:"
            continue
        fi
        echo $"Using $home_drv as the home partition"
        unset drive_check_result
        break
    done
}

get_root_partition() {
    while true; do
        read -rp $"Root partition (hda1, sda2, etc): " root_drv

        drive_check "$root_drv"

        if [[ ("$root_drv" != "$drive_check_result") || ! -b "/dev/$root_drv" ]]; then
            echo $"$root_drv invalid. Retry"
            continue
        fi
        echo $"Using $root_drv as the root partition"
        unset drive_check_result
        break
    done
}

get_esp() {
    echo "Valid esp partitions"

    # Get a list of all physical disk devices
    disks=$(LANG=c lsblk -lo NAME,TYPE | grep "disk" | awk '{print $1}')

    for i in $disks; do
        partition-info find-esps="$i"
    done

    ans=1
    while [[ "$ans" -ne 0 ]]; do
        read -rp $"ESP partition (hda1, sda2, etc): " esp_drv

        drive_check "$esp_drv"

        if [[ ("$esp_drv" == "$root_drv") || ("$esp_drv" == "$home_drv") || ("$esp_drv" != "$drive_check_result") || ! -b "/dev/$esp_drv" ]]; then
            echo $"$esp_drv invalid. Retry"
        else
            echo "Using $esp_drv as the ESP partition"
            unset drive_check_result
            ans=0
        fi
    done
}

repartition() {
    if [[ -n "$1" ]]; then
        cfdisk /dev/"$1"
    else
        cfdisk
    fi
}

set_fs() {
    umount /dev/"$1" > /dev/null 2>&1
    echo $"Available file systems for $1 are:"
    echo "
    1)ext2
    2)ext3
    3)ext4
    4)jfs
    5)xfs
    6)btrfs"
    echo
    ans=1
    while [[ $ans -ne 0 ]]; do
        read -rp $"Enter your choice 1-8: " fs
        if [[ "$fs" == "" ]]; then
            fs=ext4
        fi
        ans=0
        echo $"You have chosen $fs for $1"
        echo
        echo $"Downloading necessary files for formatting partitions"
        echo
        case $fs in
            1) mkfs.ext2 /dev/"$1" > /dev/null 2>&1;;
            2) mkfs.ext3 /dev/"$1" > /dev/null 2>&1;;
            3) mkfs.ext4 /dev/"$1" > /dev/null 2>&1;;
            4) command -v mkfs.jfs >/dev/null || (apt-get update && apt-get -y install jfsutils) && mkfs.jfs -q /dev/"$1" > /dev/null 2>&1;;
            5) command -v mkfs.xfs >/dev/null || (apt-get update && apt-get -y install xfsprogs) && mkfs.xfs -f /dev/"$1" > /dev/null 2>&1;;
            6) command -v mkfs.btrfs >/dev/null || (apt-get update && apt-get -y install btrfs-progs) && root_btrfs=true; mkfs.btrfs -f /dev/"$1" > /dev/null 2>&1;;
            *) echo $"$fs invalid. Retry:"; ans=1;;
        esac
    done
    drive_check
    command -v parted >/dev/null || (apt-get update && apt-get -y install parted)
    [[ $root_btrfs != true ]] && parted --script /dev/"$root_prefix$group_drv" set "$group_part" bios_grub off > /dev/null 2>&1
}

setpw() {
    ans=1
    while [[ $ans -ne 0 ]]; do
        if (chroot /media/"$root_drv" passwd "$1"); then
            ans=0
        else
            set_red_color
            echo $"Passwords are not identical. Retry:"
            reset_color
        fi
    done
}

yn() {
    local x=1
    local prompt="$*"

    while [[ "$x" -eq 1 ]]; do
        x=0
        read -rp "$prompt? "
        case "${REPLY,,}" in
            y|yes)
                ans=1    # yes
                ;;
            n|no)
                ans=0    # no
                ;;
            "")
                ans=-1   # default
                ;;
            *)
                x=1
                echo $"Invalid; retry:"
                ;;
        esac
    done
}

getfs() {
    root_fs=$(blkid /dev/"$root_drv" | sed 's/.*TYPE="\([^"]*\)".*/\1/')
    home_fs=$(blkid /dev/"$home_drv" | sed 's/.*TYPE="\([^"]*\)".*/\1/')
}

prepare_esp() {
    mkdir -p /media/"$root_drv"/boot/efi
    mkdir -p /media/"$root_drv"/boot/uefi-mt
    get_esp
    yn $"Format the ESP partition? (y/N)"
    if [[ "$ans" -eq 1 ]]; then
        command -v mkfs.fat >/dev/null || (apt-get update && apt-get -y install dosfstools) && mkfs.fat /dev/"$esp_drv"
    fi
    mount /dev/"$esp_drv" /media/"$root_drv"/boot/efi
}

grub_install() {
    local XTRA_PARMS=""

    # Mount system directories to make chroot for grub
    mount -o tmpfs --bind /dev/ /media/"$root_drv"/dev/
    mount -o proc --bind /proc/ /media/"$root_drv"/proc/
    mount -o sysfs --bind /sys/ /media/"$root_drv"/sys/

    # Copy over non-live boot parameters to the installed system (FIX ME)
    #cmdline=$(/live/bin/non-live-cmdline | sed -e 's/\\/\\\\/' -e 's/[|]/\\|/')
    #[ $(uname -m) = x86_64 ] && cmdline="$cmdline"
    #cmdline="quiet $cmdline"
    #sed -r -i "s|^(GRUB_CMDLINE_LINUX_DEFAULT=).*|\1\$$cmdline|" /media/$root_drv/etc/default/grub

    # Install grub
    # 1=MBR, 2=Root Partion, 3=ESP, 4=EFI Stub
    echo
    if [ "$1" == 1 ]; then
        echo $"This may take some time. Please wait...."
        [[ $root_btrfs == true ]] && if parted --script /dev/"$root_prefix$group_drv" print | grep -q "Partition Table: gpt"; then
            parted --script /dev/"$root_prefix$group_drv" set "$group_part" bios_grub on > /dev/null 2>&1
        fi
        chroot /media/"$root_drv" grub-install --target=i386-pc --recheck --no-floppy --force /dev/"$root_prefix$group_drv" > /dev/null  2>&1
        chroot /media/"$root_drv" update-grub > /dev/null  2>&1
    elif [ "$1" == 2 ]; then
        echo $"This may take some time. Please wait...."
        chroot /media/"$root_drv" grub-install --target=i386-pc --recheck --no-floppy --force /dev/"$root_drv" > /dev/null  2>&1
        chroot /media/"$root_drv" update-grub > /dev/null  2>&1
    elif [ "$1" == 3 ]; then
        prepare_esp
        # Check if the UEFI boot NVRAM should be updated
        yn $"Do you want the UEFI Boot NVRAM updated? (y/N)"
        if [[ "$ans" -eq 0 ]]; then
            XTRA_PARMS="--no-nvram"
        fi

        # What os architecture are we running?
        case $(cat /sys/firmware/efi/fw_platform_size) in
            32)
                arch="i386"
                cp /live/boot-dev/boot/uefi-mt/mtest-32.efi /media/"$root_drv"/boot/uefi-mt
                ;;
            64)
                arch="x86_64"
                cp /live/boot-dev/boot/uefi-mt/mtest-64.efi /media/"$root_drv"/boot/uefi-mt
                ;;
        esac

        echo
        echo $"This may take some time. Please wait...."
        chroot /media/"$root_drv" grub-install $XTRA_PARMS --force-extra-removable --target="$arch-efi" --efi-directory=/boot/efi --bootloader-id="$DISTRO" --recheck
        chroot /media/"$root_drv" update-grub > /dev/null  2>&1
    elif [ "$1" == 4 ]; then
        efi_stub_install
    fi
    # Make fstab entries and convert from device names to uuid
    #/sbin/make-fstab --install /media/$root_drv --mntpnt=/media > /dev/null  2>&1  ### THIS IS DONE EARLIER IN THE SCRIPT
    chroot /media/"$root_drv" dev2uuid_fstab > /dev/null  2>&1

    # Update initramfs
    chroot /media/"$root_drv" update-initramfs -u -t -k all > /dev/null  2>&1

    echo $"Bootloader installed"
}

check_root_user() {
    if [[ $UID -ne 0 ]]; then
        set_yellow_color
        echo $"This script requires root privileges. Attempting to elevate..."
        reset_color
        echo
        # Try to elevate using sudo
        # shellcheck disable=SC1093
        exec sudo -S "$0"
        # If exec fails, exit with error
        set_red_color
        echo $"Failed to elevate privileges. Please run this script as root."
        reset_color
        echo
        exit 1
    else
        echo $"Elevated privileges successfully."
        echo
    fi

}

setup_environment() {
    # Make sure /live/aufs/dev, /live/aufs/sys, /live/aufs/proc exist when starting script
    mkdir -p /live/aufs/dev /live/aufs/sys /live/aufs/proc
    # Make sure fstab is ok and start swap - (needed for antixsnapshot)
    make-fstab
    swapon -a
}

check_grub_install() {
    if ! command -v grub-install >/dev/null; then
        set_yellow_color
        echo $"GRUB not found: grub-install command is missing"
        reset_color
        echo
        yn $"Do you want to install GRUB now? (n/Y)"
        if [[ "$ans" -eq 0 ]]; then
            echo $"Continuing without GRUB. You will need to install a bootloader"
            echo $"manually after installation or the system may not boot."
        else
            echo $"Installing GRUB packages..."
            if apt-get update && apt-get -y install grub2; then
                echo $"GRUB installation completed successfully."
            else
                set_red_color
                echo $"Error: Failed to install GRUB packages."
                echo $"Please check your internet connection and repository settings."
                reset_color
                echo $"Installation can continue, but you will need to install GRUB later or configure a different boot manager."
            fi
        fi
        echo
    fi
}

show_disk_info() {
    echo
    header
    echo
    echo $"====Disk and partition Information===="
    partition-info all
    echo
    echo
}

handle_partitioning() {
    # Repartition or not and set up file system via cfdisk
    yn $"Do you want to repartition the disk (y/N)"
    if [[ "$ans" -eq 1 ]]; then
        repartition "$1"
        echo '===='
        echo
        get_root_partition
        echo
        set_fs "$root_drv"
    else
        echo
        get_root_partition
        echo
    fi
}

prepare_root_partition() {
    yn $"The installer will now destroy the data on $root_drv  Do you want to continue (No will abort the installation) (n/Y)"
    echo
    if [[ "$ans" -eq 0 ]]; then
        exit 0
    fi
    echo
    echo $"Deleting the contents of the $root_drv partition."
    echo $"This may take some time. Please wait...."
    echo
    mkdir /media/"$root_drv" > /dev/null 2>&1
    mount /dev/"$root_drv" /media/"$root_drv" > /dev/null 2>&1
    rm -r /media/"${root_drv:?}"/* > /dev/null 2>&1
    echo
    echo "Done."
    echo
}

setup_home_partition() {
    # Set up separate /home and mount on /media/$home_drv
    yn $"Do you want to use a separate '/home' partition (y/N)"
    echo
    if [[ "$ans" -eq 1 ]]; then
        get_home_partition
        if [[ ! (-e /media/"$home_drv") ]]; then
            mkdir /media/"$home_drv"
        fi
        echo
        yn $"Is $home_drv a new '/home' partition (No will not destroy any data on $home_drv ) (y/N)"
        echo
        if [[ "$ans" -eq 1 ]]; then
            home_mount_type=2 # New /home partition, will be formatted
            echo
            yn $"Set file system for $home_drv  (y/N)"
            echo
            if [[ "$ans" -eq 1 ]]; then
                set_fs "$home_drv"
            fi
            echo
            yn $"The installer will now destroy the data on $home_drv  Do you want to continue (No will abort the install) (n/Y)"
            echo
            if [[ "$ans" -eq 0 ]]; then
                exit 0
            fi
            echo
            echo $"Deleting the contents of the $home_drv partition."
            echo
            mount /dev/"$home_drv" /media/"$home_drv" > /dev/null 2>&1
            rm -r /media/"${home_drv:?}"/* > /dev/null 2>&1
            echo
            echo $"Done."
            echo
        else
            home_mount_type=1 # Existing /home partition, no formatting needed
            mount /dev/"$home_drv" /media/"$home_drv"
        fi
    else
        home_mount_type=0 # No separate /home partition
    fi
    getfs
}

copy_system_files() {
    cd /media/"$root_drv" || exit
    echo
    echo $"$DISTRO will now be copied to $root_drv."
    echo
    yn $"Do you want to continue (No will abort the install) (n/Y)"
    echo
    if [[ "$ans" -eq 0 ]]; then
        exit 0
    fi
    echo $"This may take some time. Please wait...."
    if command -v rsync >/dev/null; then
        rsync -a --human-readable --no-inc-recursive --info=progress2 --exclude='/mnt/*' /live/aufs/ . || exit 1
    else
        find /live/aufs -mindepth 1 -maxdepth 1 -not -name "mnt" -exec cp -a {} . \; || exit 1
        mkdir mnt
    fi
    echo
}

setup_fstab() {
    echo '# Pluggable devices are handled by uDev, they are not in fstab' > etc/fstab.new
    echo "/dev/$root_drv / $root_fs defaults,relatime 0 1"  >>  etc/fstab.new
    grep swap etc/fstab >> etc/fstab.new
    if [[ "$home_mount_type" -gt 0 ]]; then
        echo "/dev/$home_drv /home $home_fs defaults,relatime 0 2" >> etc/fstab.new
    fi
    rm etc/fstab
    mv etc/fstab.new etc/fstab
    cp etc/group etc/group.bak
    cp etc/gshadow etc/gshadow.bak
    echo $"File copy done"
    echo
}

copy_kernel_files() {
    local kernel_version source_dir target_path vmlinuz initrd initramfs amd_ucode intel_ucode files_to_copy target_files file target_file

    kernel_version=$(uname -r)
    source_dir="/boot"
    target_path="/media/$root_drv/boot/efi/EFI/$DISTRO/stub"
    mkdir -p "$target_path"

    vmlinuz="$source_dir/vmlinuz-$kernel_version"
    initrd="$source_dir/initrd.img-$kernel_version"
    initramfs="$source_dir/initramfs-$kernel_version.img"
    amd_ucode="$source_dir/amd-ucode.img"
    intel_ucode="$source_dir/intel-ucode.img"

    # Define arrays for files to copy
    files_to_copy=("$vmlinuz" "$initrd" "$amd_ucode" "$intel_ucode")
    target_files=("/vmlinuz" "/initrd.img" "/amducode.img" "/intucode.img")

    # Copy each file
    for i in {0..3}; do
        file="${files_to_copy[$i]}"
        target_file="$target_path${target_files[$i]}"

        # Check if we need to use initramfs instead of initrd
        if [[ "$target_file" == *"initrd.img" && ! -f "$initrd" && -f "$initramfs" ]]; then
            file="$initramfs"
        fi

        # Skip microcode files if they don't exist
        if [[ ! -f "$file" && "$file" == *"ucode.img" ]]; then
            continue
        fi

        # Check if required files exist
        if [[ ! -f "$file" && "$file" != *"ucode.img" ]]; then
            echo "Source file does not exist: $file"
            return 1
        fi

        # Copy the file
        if ! cp "$file" "$target_file"; then
            echo "Failed to copy file: $file to $target_file"
            return 1
        fi
    done

    echo "Kernel files copied successfully"
    return 0
}

read_boot_options() {
    local boot_options
    boot_options=$(/live/bin/non-live-cmdline | sed -e 's/\\/\\\\/' -e 's/[|]/\\|/' | xargs)
    echo "$boot_options"
}

efi_stub_install() {
    prepare_esp
    # Check if efibootmgr is installed
    if ! command -v efibootmgr >/dev/null; then
        set_yellow_color
        echo $"efibootmgr not found: required for EFI stub installation"
        reset_color
        echo
        yn $"Do you want to install efibootmgr now? (n/Y)"
        if [[ "$ans" -eq 0 ]]; then
            echo $"Skipping efibootmgr installation"
            echo $"EFI stub installation cannot proceed without efibootmgr"
            return 1
        else
            echo $"Installing efibootmgr..."
            if ! (apt-get update && apt-get install -y efibootmgr); then
                set_red_color
                echo $"Failed to install efibootmgr"
                reset_color
                echo $"EFI stub installation cannot proceed"
                return 1
            fi
            echo $"efibootmgr installed successfully"
        fi
    fi

    if ! copy_kernel_files; then
        echo $"EFI stub installation failed"
        return 1
    fi
    # Set directory name for EFI stub installation
    local efi_dir entry_name
    efi_dir="stub"
    entry_name="${DISTRO} EFI stub"

    # Get ESP mount point
    local esp_path
    esp_path="/media/$root_drv/boot/efi/EFI/$DISTRO/$efi_dir"

    # Get disk and partition from esp_drv
    local disk_dev part_num
    disk_dev=${esp_drv//[0-9]/}
    part_num=${esp_drv##*[!0-9]}

    # Define paths for EFI references
    local initrd_efi amd_ucode_efi int_ucode_efi
    initrd_efi="initrd=\\EFI\\$DISTRO\\$efi_dir\\initrd.img"
    amd_ucode_efi="initrd=\\EFI\\$DISTRO\\$efi_dir\\amducode.img"
    int_ucode_efi="initrd=\\EFI\\$DISTRO\\$efi_dir\\intucode.img"

    # Check for microcode files
    initrd=""
    if [ -f "$esp_path/amducode.img" ]; then
        initrd="$amd_ucode_efi"
    fi

    if [ -f "$esp_path/intucode.img" ]; then
        if [ -z "$initrd" ]; then
            initrd="$int_ucode_efi"
        else
            initrd="$initrd $int_ucode_efi"
        fi
    fi

    # Always add the main initrd
    if [ -z "$initrd" ]; then
        initrd="$initrd_efi"
    else
        initrd="$initrd $initrd_efi"
    fi

    # Set boot options
    local boot_options
    boot_options="root=UUID=$(blkid -s UUID -o value /dev/"$root_drv") $(read_boot_options) $initrd"

    # Create the EFI boot entry
    echo $"Creating EFI boot entry..."
    if ! efibootmgr --disk "/dev/$disk_dev" --part "$part_num" --create \
        --label "$entry_name" --loader "\\EFI\\$DISTRO\\$efi_dir\\vmlinuz" \
        --unicode "$boot_options"; then
        echo $"Failed to create EFI boot entry"
        return 1
    fi

    echo $"EFI stub installation completed successfully"
}

install_bootloader() {
    # Check if system is EFI
    local is_efi_system has_grub_install
    is_efi_system=false
    [ -d /sys/firmware/efi/efivars ] && is_efi_system=true

    # Check if grub-install is available
    has_grub_install=false
    command -v grub-install >/dev/null && has_grub_install=true

    # If neither EFI nor grub-install is available, skip bootloader installation
    if ! $is_efi_system && ! $has_grub_install; then
        set_yellow_color
        echo $"Skipping bootloader installation: neither EFI nor grub-install is available"
        reset_color
        return
    fi

    # If EFI is available but grub-install is not, only do stub install
    if $is_efi_system && ! $has_grub_install; then
        echo $"No grub-install available, performing EFI stub installation only"
        efi_stub_install
        return
    fi

    # Normal installation with options
    echo $"Where should grub be installed?"
    echo $"1) MBR (Legacy - Master Boot Record)"
    echo $"2) Root Partition"

    local max_install_choice install_choice
    max_install_choice=2
    # Show EFI options if available
    if $is_efi_system; then
        echo $"3) ESP (EFI install)"
        echo $"4) Do a EFI stub installation (no grub)?"
        max_install_choice=4
    fi

    # Get user choice
    while true; do
        read -rp "Enter your choice (1-$max_install_choice): " install_choice

        if [[ "$install_choice" -ge 1 && "$install_choice" -le "$max_install_choice" ]]; then
            case $install_choice in
                1) echo $"Installing grub to the MBR";;
                2) echo $"Installing grub to the Root Partition";;
                3) echo $"Installing grub to the ESP";;
                4) echo $"Performing EFI stub installation";;
            esac
            grub_install "$install_choice"
            break
        else
            echo $"Invalid; retry:"
        fi
    done
}

setup_hostname() {
    echo
    chroot /media/"$root_drv" userdel "$LIVEUSER"
    rm -r /media/"$root_drv"/home/"$LIVEUSER" > /dev/null 2>&1
    read -rp $"Computer name (default is 'mx1')? " computer_name
    if [[ -n "$computer_name" ]]; then
        echo "$computer_name" > /media/"$root_drv"/etc/hostname
        sed -i "s/mx1/$computer_name/" /media/"$root_drv"/etc/hosts
    fi
}

configure_localization() {
    set_blue_color
    echo $"Time to set up localisation"
    reset_color
    echo $"System locale is set to ..."
    chroot /media/"$root_drv" cat /etc/default/locale
    echo
    yn $"Do you want to set up system localisation (y/N)"
    echo
    if [[ $ans -eq 1 ]]; then
        chroot /media/"$root_drv" dpkg-reconfigure locales
    fi
}

configure_keyboard() {
    set_blue_color
    echo $"Time to set keyboard layout"
    reset_color
    echo $"System keyboard is set to ..."
    chroot /media/"$root_drv" cat /etc/default/keyboard
    echo
    yn $"Do you want to set up keyboard (y/N)"
    echo
    if [[ "$ans" -eq 1 ]]; then
        chroot /media/"$root_drv" dpkg-reconfigure keyboard-configuration
    fi
}

configure_console() {
    set_blue_color
    echo $"Time to set console layout"
    reset_color
    echo $"System console is set to ..."
    chroot /media/"$root_drv" cat /etc/default/console-setup
    echo
    yn $"Do you want to set up console layout (y/N)"
    echo
    if [[ "$ans" -eq 1 ]]; then
        chroot /media/"$root_drv" dpkg-reconfigure console-setup
    fi
}

configure_timezone() {
    set_blue_color
    echo $"Time to set timezone"
    reset_color
    echo $"System timezone is set to ..."
    chroot /media/"$root_drv" cat /etc/timezone
    echo
    yn $"Do you want to set up system timezone (y/N)"
    echo
    if [[ "$ans" -eq 1 ]]; then
        chroot /media/"$root_drv" dpkg-reconfigure tzdata
    fi
}

configure_services() {
    set_blue_color
    echo $"Choose which services to run"
    reset_color

    command -v sysv-rc-conf >/dev/null && (
        yn $"Do you want to enable/disable startup services (y/N)"
        echo
        if [[ "$ans" -eq 1 ]]; then
            chroot /media/"$root_drv" sysv-rc-conf
        fi
        echo
        sleep 1
    )
}

handle_remastered_install() {
    echo
    yn $"Is this a remastered/snapshot install (y/N)"
    echo
    if [[ "$ans" -eq 1 ]]; then
        # Write code to get buildfstab -r to start on first boot via rc.local (if user wants it?)
        #mv etc/rc.local etc/rc.local2
        mv etc/udev/rules.d/90-fstab-automount.rules etc/udev/rules.d/90-fstab-automount.rules.live
        cp usr/share/antiX/rc.local.cli etc/rc.local
        # (also)Remove live system if it exists
        chroot /media/"$root_drv" dpkg -r live-init-antix 2>/dev/null
        rm /media/"$home_drv"/"$LIVEUSER"
        umount -l /media/"$root_drv"
        umount -l /dev/"$home_drv"
        echo $"Installation of $DISTRO finished!"
        echo
        echo $"Reboot computer without CD to boot into $DISTRO" "('sudo reboot')"
        echo
        exit
    fi
    sleep 1
}

setup_user_accounts() {
    set_blue_color
    echo $"Setting up user and root/admin accounts"
    reset_color
    name=''
    while [[ -z "$name" ]]; do
        read -rp $"Type in your default user name: " name
    done
    case $home_mount_type in
        0)chroot /media/"$root_drv" adduser --force-badname "$name";;
        1)echo;echo $"Note: $home_drv must contain a folder named '$name'."
        chroot /media/"$root_drv" adduser --force-badname "$name";;
        2)chroot /media/"$root_drv" adduser --force-badname "$name";;
    esac
    echo
    yn $"Setup root account? (y/N)"
    echo
    if [[ "$ans" -eq 1 ]]; then
        echo $"Type your Password for root: "
        echo
        setpw root
    else
        chroot /media/"$root_drv" passwd -l root
    fi
    sed -i "s/$LIVEUSER/$name/" etc/group
    sed -i "s/$LIVEUSER/$name/" etc/gshadow
    sed -i "s/$LIVEUSER/$name/" usr/share/slim/themes/antiX/slim-install.conf 2>/dev/null
    echo
}

cleanup_and_finalize() {
    # Copy live configurations to install. For all.
    echo
    echo $"Cleaning up"
    echo
    #/usr/sbin/live-to-installed /media/$root_drv
    #mv etc/rc.local etc/rc.local2
    mv etc/X11/xorg.conf etc/X11/xorg.conf.live 2>/dev/null
    cp usr/share/antiX/rc.local.cli etc/rc.local
    cp usr/share/slim/themes/antiX/slim-install.conf etc/slim.conf 2>/dev/null
    chroot /media/"$root_drv" dpkg -r live-init-antix 2>/dev/null
    cp -r etc/skel/.[a-zA-Z]* home/*/ 2>/dev/null
    cp -r etc/skel/* home/*/ 2>/dev/null
    rm -R etc/live/*
    sed -i '2,$d' /media/"$root_drv"/etc/issue
    if [[ "$home_mount_type" -gt 0 ]]; then
        if [[ "$home_mount_type" -eq 2 ]]; then
            mv home/* /media/"$home_drv"
        fi
        rm -r home
        mkdir home
        cp -r /media/"$root_drv"/etc/skel/.[a-zA-Z]* /media/"$home_drv"/*/ 2>/dev/null
        cp -r /media/"$root_drv"/etc/skel/* /media/"$home_drv"/*/ 2>/dev/null
        chown -R "$LIVEUSER".users /media/"$home_drv"/* 2>/dev/null
    fi
    chown -R "$LIVEUSER"."$LIVEUSER" home/* 2>/dev/null

    rm -rf media/sd*
    rm -rf media/hd*
    rmdir live
    umount -l /media/"$root_drv"/dev/
    umount -l /media/"$root_drv"/proc/
    umount -l /media/"$root_drv"/sys/
    umount -l /media/"$root_drv"
    if [[ "$home_mount_type" -ne 0 ]]; then
        umount -l /dev/"$home_drv"
    fi
    echo
    echo $"Installation of $DISTRO finished!"
    echo
    echo $"Reboot computer without CD to boot into $DISTRO" "('sudo reboot')"
}

main() {
    # Run Help
    if [[ "$1" == "-h" || "$1" == "--help" ]]; then
        help_text
        echo
        read -rp $"Press Enter to exit this script."
        echo
        exit
    fi

    check_root_user
    setup_environment
    show_disk_info
    handle_partitioning "$1"
    prepare_root_partition
    setup_home_partition
    check_grub_install
    copy_system_files
    setup_fstab
    install_bootloader
    setup_hostname
    configure_localization
    configure_keyboard
    configure_console
    configure_timezone
    configure_services
    handle_remastered_install
    setup_user_accounts
    cleanup_and_finalize
}

# Execute main function with all arguments
main "$@"
