#!/bin/bash -x

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

container="buildcont"

set -e

# Only run the post build check for aarch64
[ "$(uname -m)" == "aarch64" ] || exit 0

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
truncate -s $((${img_size}*1024*1024)) rpi.img

# create a primary FAT partition with 128MB (for kernel + firmware)
# create a primary Linux partition covering the remainder
sfdisk rpi.img <<EOF
2048,+128M
;
EOF
# FAT32 LBA
sfdisk --part-type rpi.img 1 c
# Linux
sfdisk --part-type rpi.img 2 83
# Activate
sfdisk --activate rpi.img 1

# find first free loop device
loop=`losetup -f`
# connect it to rpi.img, scan partition table
losetup --partscan ${loop} rpi.img

# boot partition
#
mkfs -t vfat -n RPI_BOOT ${loop}p1
# create a mountpoint and mount the FAT partition
mkdir img
mount ${loop}p1 img
# copy bootloader
cp -a iso/EFI img
cp -a iso/boot img
# and firmware files to the FAT partition
# 'old' firmware
if [ -d rootfs/boot/vc ]; then
  cp -a rootfs/boot/vc/* img
else
# systemready firmware
  cp rootfs/boot/*.fd img
  cp rootfs/boot/*.dtb img
  cp rootfs/boot/config.txt img
  cp rootfs/boot/fixup4.dat img
  cp rootfs/boot/start4.elf img
  cp -a rootfs/boot/firmware img
  cp -a rootfs/boot/overlays img
fi

umount img

# root partition
#
# label it COS_LIVE
mkfs -t ext3 -L COS_LIVE ${loop}p2
mount ${loop}p2 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
if [ -d rootfs/boot/vc ]; then
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
else
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 /run/cos/workingtree/boot/*.fd /run/cos/efi
      - cp /run/cos/workingtree/boot/*.dtb /run/cos/efi
      - cp /run/cos/workingtree/boot/config.txt /run/cos/efi
      - cp /run/cos/workingtree/boot/fixup4.dat /run/cos/efi
      - cp /run/cos/workingtree/boot/start4.elf /run/cos/efi
      - cp -a /run/cos/workingtree/boot/firmware /run/cos/efi
      - cp -a /run/cos/workingtree/boot/overlays /run/cos/efi
    after-reset:
    - <<: *copyfirmware
HOOK
fi
# undo all mounts, loopback devices, etc.

umount img
rmdir img
umount rootfs
rmdir rootfs

losetup -d ${loop}
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}"
