#!/bin/sh
# Bare Metal Alchemist, 2022

############################################# 
#                 Tin - ♃                   # 
############################################# 

# Tin is frequently combined with lead to form solder, which is an
# important component of wiring up hardware.

# This ingredient is to be used when you're interacting with physical
# hardware like GPIO pins or USB drives

sha256_check() {
    # Args: <sha256_hash> <filename>
    #
    if check_exists sha256sum; then
        echo "${1}  ${2}" | sha256sum -c
    elif check_exists sha256; then
        if [ "$(uname)" = "FreeBSD" ]; then
            sha256 -c "${1}" "${2}"
        else
            echo "${1}  ${2}" | sha256 -c
        fi
    else
        echo "${1}  ${2}" | shasum -a 256 -c
    fi
}

http_get() {
    # Args: <url> <filename> <sha256_hash>
    #
    # It's acceptable that we don't require SSL here because we manually verify
    # content hashes below.
    #
    if [ -f "${2}" ]; then
        echo "File ${2} already exists; not downloading again"
    elif check_exists curl; then
        curl --insecure --retry 5 "${1}" -o "${2}"
    else
        wget --no-check-certificate "${1}" -O "${2}"
    fi

    sha256_check "${3}" "${2}"
}

TIN=1