#!/bin/bash
# common functionality for flashing scripts

get_kpart_and_pdisk(){

    # getting where root is mounted from, /dev/mmcblk0p4, /dev/sda4, or /dev/mapper/encrypted
    root=$(findmnt -n -o SOURCE /)

    # encrypted partitions are inside of /dev/mapper
    if echo $root | grep -q "mapper"; then
        rawname=$(echo $root | sed 's/\/dev\/mapper\///') # first, we remove the useless parts
        root=/dev/$(lsblk -no NAME --raw | grep -B1 $rawname | head -n 1) # then, we get the partition before it's encrypted on, in lsblk
    fi

    # obtains the /dev/mmcblk1p, /dev/sda, or similar
    kpart=$(echo $root | sed 's/[0-9]*$//')

    # obtains parent disk, since sometimes there is p it otherwise would be tricky
    pdisk=$(lsblk -no PKNAME $root | head -n 1)
}

get_partid_kpart_and_pdisk_from_drive(){
    lsblk -no PKNAME $1 > /dev/null 2> /dev/null

    if [ "$?" != "0" ];then
        echo "sorry, I can't flash to $1"
        exit
    fi

    pdisk=$(lsblk -no PKNAME $1 | head -n 1)

    kpart=$(echo -n $1 | sed 's/[0-9]*$//')

    id="${1//$kpart/}"
}

file_size(){
    stat --format="%s" $1
}

partition_size(){
    lsblk -b -n -o SIZE -p $1
}

flash_procedure(){
    if [ "$#" == "2" ] && [ "${2}" != "yes" ]; then
        get_partid_kpart_and_pdisk_from_drive $2
    else
        load_config
        
        if [ "$part" == "auto" ]; then
            get_kpart_and_pdisk
        elif [ "$part" == "none" ]; then
            echo "partition set to none, can't flash"
            exit
        else
            get_partid_kpart_and_pdisk_from_drive $part
        fi
    fi

    if [ "${2}" == "yes" ];then
        echo "flashing the image to ${kpart}${id}"
    else
        echo "Are you sure you want to flash the image to ${kpart}${id} ?"
        need_confirmation
    fi

    image_size=$(file_size "/boot/vmlinux.kpart-initrd-${kver}")
    part_size=$(partition_size ${kpart}${id})

    # check if image is bigger than the partition
    if [ "$image_size" -gt "$part_size" ]; then
        echo "The image (/boot/vmlinux.kpart-initrd-${kver} $image_size) is too big"
        echo "to flash onto partition (${kpart}${id} $part_size)"
        echo ""
        echo "FLASHING ABORTED"
        echo ""
        echo "try putting"
        echo ""
        echo "COMPRESS=xz"
        echo "XZ_OPT='-9 --check=crc32 –memlimit-compress=25%'"
        echo ""
        echo "in /etc/initramfs-tools/conf.d/compress"
        echo ""
        echo "or"
        echo ""
        echo "putting all the required dtbs inside /boot/dtbs file"
        echo "\"vthelp dtbs\" for more details"
        echo "Note. only do this if you know what you're doing"
        echo ""
        echo "THEN build image with vtbuild <version> and try flashing it again"

        exit 0;
    fi

    echo "Flashing"
    echo "image (/boot/vmlinux.kpart-initrd-${kver} $image_size)"
    echo "partition (${kpart}${id} $part_size)"

    dd if=/boot/vmlinux.kpart-initrd-${kver} of=${kpart}${id}


    if [ $? == "1" ]; then
        flashing_error
        exit
    fi
}

check_flashable_kver() {
    if [ ! -e "/boot/vmlinux.kpart-initrd-${kver}" ]; then
        echo "Error. kernel ${kver} is not flashable, try building it with vtbuild <version>"
        echo "Note. you can list available kernel versions using the \"vtlist\" command"
        exit
    fi
}

flashing_error(){
    echo "WARNING"
    echo ""
    echo "There was a flashing issue, your device might be"
    echo "UNABLE TO BOOT."
    echo ""
    echo "If you understand please type \"understood\"."

    read reply
    if [ "$reply" != "understood" ];then
        echo "THIS IS IMPORTANT"
        flashing_error
    fi;
}
