#!/bin/bash
#
# Copyright (C) 2018 Purism SPC
#
# SPDX-License-Identifier: GPL-3.0+
#

packages="whiptail iperf3 wireless-tools network-manager powertop htop scdaemon"

usage() {
  echo "Usage : $1 "
  echo "    -d <kernel_deb> use this deb"
}

cwd=$(pwd)
build_prep=0
arch=arm64

while getopts "hd:b" opt; do
  case ${opt} in
    d)
      kernel_deb=$OPTARG
      ;;
    \?)
      echo "Invalid Option: -$OPTARG" 1>&2
      usage "$0"
      exit 0
      ;;
    h )
      usage "$0"
      exit 0
      ;;
  esac
done

if [ -z ${kernel_deb} ]; then
    kernel_deb=`ls -t files/linux-image-* | grep -v dbg | head -n1`
fi

echo "Installing kernel ${kernel_deb}"

[ ! -e build ] && mkdir -p build

package_list=""

for package in ${packages}; do
    package_list="${package_list} --package=${package}"
done

echo "packages ${packages}"
echo "package_list ${package_list}"

if [ ! -e build/test_roofs.tgz ];  then
    sudo vmdebootstrap \
             --arch=arm64 --no-extlinux --no-kernel\
             --no-update-initramfs --enable-dhcp \
             --foreign=/usr/bin/qemu-aarch64-static \
             --verbose --log "build/test_img.log" \
             --tarball="build/test_rootfs.tgz" \
             --configure-apt \
             --distribution "buster" \
             --serial-console-command "/sbin/getty -L ttymxc0 115200 vt100" \
             --root-password='root' --hostname='pureos-test' --user=purism/123456 --sudo \
             --owner "$(whoami)" ${package_list} \
             --custom-package=${kernel_deb}
fi


