#!/usr/bin/env bash

iso_download(){
#args:
# $1 = iso url
# $2 = iso filename
# $3 = iso path
  printf '\n\nDownloading %s...\n' "${2}"
  printf 'url: %s\n' "${1}"
  printf 'destination file: %s%s\n' "${3}" "${2}"
  if [ ! -f "${3}${2}" ];then
    mkdir -p "${3}"
    curl --progress-bar -Lo "${3}${2}" "${1}"
  fi
}

#debian stable
debian_stable_iso_root='cdimage.debian.org/debian-cd/current-live/amd64/iso-hybrid/'
debian_stable_iso_list=$(curl -s "ftp://${debian_stable_iso_root}" | grep '.iso$' | awk '{print $9}' | grep -e 'kde' -e 'standard' -e 'lxqt' -e 'xfce')

for i in ${debian_stable_iso_list[@]};do
  iso_download "http://${debian_stable_iso_root}${i}" "${i}" "disk_images/operating_systems/linux/debian/stable/"
done

debian_testing_iso_root='cdimage.debian.org/cdimage/weekly-live-builds/amd64/iso-hybrid/'
debian_testing_iso_list=$(curl -s "ftp://${debian_testing_iso_root}" | grep '.iso$' | awk '{print $9}' | grep -e 'standard')

for i in ${debian_testing_iso_list[@]};do
  iso_download "http://${debian_testing_iso_root}${i}" "${i}" "disk_images/operating_systems/linux/debian/testing_$(date +%Y-%m-%d)/"
done

rescuezilla_iso_url=$(curl -s https://api.github.com/repos/rescuezilla/rescuezilla/releases/latest | jq -r '.assets[].browser_download_url' | grep '.iso$' | tail -n 1)
iso_download "${rescuezilla_iso_url}" "$(basename ${rescuezilla_iso_url})" "disk_images/diagnostic_tools/rescuezilla/"

memtest86_iso_url=$(curl https://memtest.org/ | grep -e '_64.iso' | grep -o -P '(?<= href=").*(?=" class)' | head -n 1)
iso_download "https://memtest.org${memtest86_iso_url}" "$(basename https://memtest.org${memtest86_iso_url})" "disk_images/diagnostic_tools/memtest86plus/"
unzip "disk_images/diagnostic_tools/memtest86plus/$(basename https://memtest.org${memtest86_iso_url})" -d "disk_images/diagnostic_tools/memtest86plus/"

#https://dl.google.com/dl/edgedl/chromeos/recovery/cloudready_recovery2.json
