#!/bin/bash

function execute_download() {
  declare file_url="$1" file_sha256sum="$2" file_path="$3"

  echo "[installer] download ${file_url}"
  wget "${file_url}" -O "${file_path}" || exit 255
  chmod +x "${file_path}" || exit 255

  echo "${file_sha256sum} ${file_path}" | sha256sum -c - >/dev/null 2>&1
  if [[ $? -ne 0 ]]; then
    echo >&2 "Checksum verification failed for ${file_path}."
    exit 255
  fi

}

execute_download \
  'https://raw.githubusercontent.com/novln/Kiss-my-Arch/master/bootstrap/xps13-9333-install/bootstrap' \
  '099911f1d0b0c3ed91f8c8da8d3204fcb287619cacc88fffda4aca9e5a6fe9e4' \
  '/usr/local/sbin/arch-bootstrap'

execute_download \
  'https://raw.githubusercontent.com/novln/Kiss-my-Arch/master/bootstrap/xps13-9333-install/configure' \
  'adba688d9295814478c7f71a88dec4ed24b6722c275fb606b9cc12454349f1b5' \
  '/usr/local/sbin/arch-configure'

timedatectl set-ntp true

/usr/local/sbin/arch-bootstrap
if [[ $? -ne 0 ]]; then
  exit 254
fi

exit 0