#!/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 [ci|test] get this version of the image "
  echo "    -n don't extract the image"
}

build_type=test
pureos_board=devkit
no_extract=0

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

[ ! -e files ] && mkdir files

if [ "${build_type}" == "ci" ]; then
  suffix=""
  build_name="devkit_CI_image_build"
else
  suffix="-${build_type}"
  build_name="${pureos_board}-${build_type}_image_build"
fi

base_url=https://arm01.puri.sm/job/Images/job/${build_name}/lastSuccessfulBuild/artifact/
wget -N "${base_url}/${pureos_board}${suffix}.img.xz" -P files/

if [ "${no_extract}" == 0 ]; then
	xz -vdk -f files/${pureos_board}${suffix}.img.xz
fi

