#!/bin/bash -x

: ${TOPDIR:=/usr/src/packages}

container="buildcont"

set -e

echo "Generating image from ISO from container image"

# Build service provides the container image as a .tar file

img=$(ls ${TOPDIR}/DOCKER/*.tar)

[ -f "${img}" ] || exit 1
# Only consider images with 'iso' as part of the name
echo "${img}" | grep -q iso || exit 0

# import .tar with buildah

buildah from --name "${container}"  "docker-archive:${img}"

# mount container to access its content

mnt=$(buildah mount "${container}")

# there must be an .iso file within

iso=$(ls "${mnt}"/elemental-iso/*.iso)

# exit here if not

[ -f "${iso}" ] || exit 1

mkdir -p "${TOPDIR}/OTHER/image"

# create a mountpoint and mount the iso
mkdir iso
mount -o loop "${iso}" iso

# create a mountpoint and mount the rootfs.squashfs that's inside the iso
mkdir rootfs
mount -o loop iso/rootfs.squashfs rootfs


# extract the image size from the "build config"

img_size=$(grep -m 1 "%img_size" ~/.rpmmacros | tr -d "\n" | cut -d " " -f 2)

# extract two last project path elements ({Stable,Staging,Dev}:TealXX)
# replace colon with dash
project=$(grep -m 1 "%_project" ~/.rpmmacros | tr -d "\n" | rev | cut -d ":" -f 1,2 | rev | tr ":" "-")

# create an empty image file and a respective loop device
dd if=/dev/zero of=rpi.img bs=$((1*1024*1024)) count="${img_size}"
losetup /dev/loop42 ./rpi.img

# create a DOS label
parted --script -- /dev/loop42 mklabel msdos
# create a primary FAT partition with 135MB (for kernel + firmware)
parted --script -- /dev/loop42 mkpart primary fat32 2048s 135MB set 1 boot on set 1 lba on
# create a filesystem within the FAT partition
mkfs -t vfat -n RPI_BOOT /dev/loop42p1

# boot partition
#
# create a mountpoint and mount the FAT partition
mkdir img
mount /dev/loop42p1 img
# copy bootloader
cp -a iso/EFI img
cp -a iso/boot img
# and firmware files to the FAT partition
cp -a rootfs/boot/vc/* img
umount img

# root partition
#
# create an EXT3 partition, starting at 135MB and extending up to img_size
parted --script -- /dev/loop42 mkpart primary ext3 135MB "${img_size}"MB
# label it COS_LIVE
mkfs -t ext3 -L COS_LIVE /dev/loop42p2
mount /dev/loop42p2 img
# copy the rootfs into this partition
cp iso/rootfs.squashfs img

# Install hook to copy rpi firmware in EFI partition
mkdir -p img/iso-config
cat << HOOK > img/iso-config/01_rpi-install-hook.yaml
name: "Raspberry Pi after install hook"
stages:
    after-install:
    - &copyfirmware
      name: "Copy firmware to EFI partition"
      commands:
      - cp -a /run/cos/workingtree/boot/vc/* /run/cos/efi
    after-reset:
    - <<: *copyfirmware
HOOK

# undo all mounts, loopback devices, etc.

umount img
rmdir img
umount rootfs
rmdir rootfs

losetup -d /dev/loop42
umount iso
rmdir iso

# copy the image as rpi.raw (buildservice checks extensions)

mkdir -p "${TOPDIR}/OTHER"
mv rpi.img rpi.raw
sha256sum rpi.raw > "${TOPDIR}/OTHER/rpi.raw.sha256"
mv rpi.raw "${TOPDIR}/OTHER"
ln "${TOPDIR}/OTHER/rpi.raw" "${TOPDIR}/OTHER/rpi-${project}-`date +'%Y%m%d%H%M%S'`.raw"

# release the container

buildah umount "${container}"
buildah rm "${container}"
