#!/bin/bash

# Type: Admin
# require: xorriso, isolinux, syslinux, awk
# by: echarte (echarte@cnti.gob.ve)

VERSION="0.1"
CANAIMA_ISO=$2
CANAIMA_BASE_NAME=${CANAIMA_ISO%.*}

LETTERS=("A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" \
	     "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" \
	     "Z" "YO" "PA" "TI" "CA" "SA" "MU" "LA" "EL" "LOS" \
	     "SI" "MI" "NO" "DA" "HI" "PI" "FE" "HU" "GE" "DI" \
	     "GUA" "FU" "RE" "EX" "RO" "ER" "CO" "LL" "PH" "ZO" \
	     "LI" "FA" "TE" "ES")

function my_seconds () {
    echo $(( ( RANDOM % 60 ) + 1 ))
}

function pseudo-random-string () {
    FIRST_FONT=$(date +'%m%d%y.%H%M%S')
    SECOND=$(my_seconds)
    NUMBER_TO_LETTER=$(echo "${LETTERS[$SECOND]}")
    SECOND_FONT=$(echo "$RANDOM")
    STATIC_STRING=$(echo "${FIRST_FONT}${NUMBER_TO_LETTER}${SECOND_FONT}" | sha512sum)
    RANDOM_STRING=$(echo ${STATIC_STRING:0:$SECOND})
    echo "${RANDOM_STRING}"
}

CAPTURE_RANDOM_STRING=$(pseudo-random-string)
STATIC_TEMP_STRING="$CAPTURE_RANDOM_STRING"

function where_am_i () {
    W_A_I=$(pwd .)
}

function use () {
    echo -e "Description: This script transform a iso-live to iso-live-uefi. Add the UEFI support."
    echo -e "Use: # > canaima-uefi i386|amd64 canaima-live.iso\n"
    echo -e "\n"
    echo -e "You'r need root privileges and the packages xorriso, isolinux and syslinux."
    echo -e "The version is $VERSION\n"
}

function checkroot () {
    if [ $(whoami) != "root" ]; then
	echo -e "You'r need root privileges.\n"
	sleep 2
	exit 1
    fi
}

function check_disk_space () {
    IMAGE_SIZE=$(du -s $CANAIMA_ISO | awk '{ print $1 }')
    DIRECTORY_SPACE=$(du -s ${W_A_I} | awk '{ print $1 }')
    NECESARY=$(bc <<< "$IMAGE_SIZE+$IMAGE_SIZE")

    if [ "$DIRECTORY_SPACE" -lt "$NECESARY" ]; then
	echo -e "Your need more space in the disk.\n"
	sleep 2
	exit 1
    fi
}

CANAIMA_32="/mnt/32-canaima-${STATIC_TEMP_STRING}"
CANAIMA_64="/mnt/64-canaima-${STATIC_TEMP_STRING}"
CANAIMA_UEFI="/mnt/iso-uefi-${STATIC_TEMP_STRING}"

function dirs_job () {
    mkdir -p {${CANAIMA_32},${CANAIMA_64},${CANAIMA_UEFI}}
}

function pre_capture_devices () {
    fdisk -l > /tmp/fdisk-pre-${STATIC_TEMP_STRING}
}

function post_capture_devices () {
    fdisk -l > /tmp/fdisk-post-${STATIC_TEMP_STRING}
}

function job_386 () {
    checkroot
    where_am_i
    dirs_job
    pre_capture_devices
    mount -o loop $CANAIMA_ISO $CANAIMA_32
    post_capture_devices
    cp -r ${CANAIMA_32}/isolinux/ ${CANAIMA_32}/live/ ${CANAIMA_32}/.disk/ ${CANAIMA_32}/md5sum.txt $CANAIMA_UEFI
    cp -r ${W_A_I}/i386/efi/ $CANAIMA_UEFI
    cp -r ${W_A_I}/i386/boot/ $CANAIMA_UEFI
}

function job_amd64 () {
    checkroot
    where_am_i
    dirs_job
    pre_capture_devices
    mount -o loop $CANAIMA_ISO $CANAIMA_64
    post_capture_devices
    cp -r ${CANAIMA_64}/isolinux/ ${CANAIMA_64}/live/ ${CANAIMA_64}/.disk/ ${CANAIMA_64}/md5sum.txt $CANAIMA_UEFI
    cp -r ${W_A_I}/amd64/efi/ $CANAIMA_UEFI
    cp -r ${W_A_I}/amd64/boot/ $CANAIMA_UEFI
}

function iso_hybrid () {
    checkroot
    cd $CANAIMA_UEFI
    xorriso -as mkisofs -iso-level 3 -full-iso9660-filenames \
	    -eltorito-boot isolinux/isolinux.bin \
	    -eltorito-catalog isolinux/boot.cat -no-emul-boot \
	    -boot-load-size 4 -boot-info-table \
	    -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin \
	    -eltorito-alt-boot -e boot/grub/efi.img \
	    -no-emul-boot -isohybrid-gpt-basdat \
	    -output ${CANAIMA_BASE_NAME}-uefi.iso .

}

function dev_loop () {
    diff /tmp/fdisk-pre-${STATIC_TEMP_STRING} /tmp/fdisk-post-${STATIC_TEMP_STRING} \
	 | awk '/\/dev\/*/ { print $2 }' | grep loop | cut -c-10
}

function job_clean () {
    umount $(dev_loop)
    rm -rf $CANAIMA_32 $CANAIMA_64
    cd $CANAIMA_UEFI
    rm -rf live/ isolinux/ isolinux/ .disk/ md5sum.txt boot/ efi/
    mv ${CANAIMA_BASE_NAME}-uefi.iso $W_A_I
    cd ..
    rm -rf $CANAIMA_UEFI
    rm -f /tmp/{fdisk-pre-${STATIC_TEMP_STRING},fdisk-post-${STATIC_TEMP_STRING}}
}

case $1 in
    i386)
	job_386
	iso_hybrid
	job_clean
	;;
    amd64)
	job_amd64
	iso_hybrid
	job_clean
	;;
    *)
	use
esac
