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

usage() {
  echo "Usage : $1 "
  echo "    -h Display this help message."
  echo "    -T [current|legacy|next] use the artifacts from this kernel version"
}

build_type=52
pureos_board=devkit

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

[ ! -e files ] && mkdir files

case ${build_type} in
	legacy)
		kernel_url=https://arm01.puri.sm/job/Kernel_builds/job/kernel_linux-next_imx8-legacy-librem5/
		uboot0_url=https://arm01.puri.sm/job/u-boot_builds/job/uboot_devkit_build/
		uboot1_url=https://arm01.puri.sm/job/u-boot_builds/job/uboot_librem5_build/
		;;
	current)
		kernel_url=https://arm01.puri.sm/job/Kernel_builds/job/kerne_linux-next_imx8-current-librem5/
		uboot0_url=https://arm01.puri.sm/job/u-boot_builds/job/uboot_devkit_build/
		uboot1_url=https://arm01.puri.sm/job/u-boot_builds/job/uboot_librem5_build/
		;;
	next)
		kernel_url=https://arm01.puri.sm/job/Kernel_builds/job/kernel_linux-next_imx8-next-librem5/
		uboot0_url=https://arm01.puri.sm/job/u-boot_builds/job/uboot_devkit_build/
		uboot1_url=https://arm01.puri.sm/job/u-boot_builds/job/uboot_librem5_build/
		;;
esac

wget -N "${kernel_url}lastSuccessfulBuild/artifact/*zip*/archive.zip"  -P files
cd files
unzip -oj archive.zip
cd ..
wget -N "${uboot0_url}lastSuccessfulBuild/artifact/*zip*/archive.zip"  -P files
cd files
unzip -oj archive.zip
cd ..
wget -N "${uboot1_url}lastSuccessfulBuild/artifact/*zip*/archive.zip"  -P files
cd files
unzip -oj archive.zip
