#!/bin/sh
# SPDX-License-Identifier: Apache-2.0
# Generated for the exact 0.1.1 installer asset.
# Everything runs from main, called on the last line with an end marker after
# the caller's arguments: a download cut short, even within that line, defines
# functions at most or reaches main without the marker, and runs nothing.
set -eu
umask 077

fail() {
    printf 'error: %s\n' "$2" >&2
    exit "$1"
}

check_options() {
    bootstrap_channel=stable
    bootstrap_risk=false
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --dry-run|--yes|--with-python|--with-devel) ;;
            --accept-experimental-risk) bootstrap_risk=true ;;
            --channel)
                shift
                [ "$#" -gt 0 ] || fail 2 '--channel requires stable or experimental'
                bootstrap_channel=$1 ;;
            --channel=*) bootstrap_channel=${1#*=} ;;
            *) fail 2 'unknown or misplaced installer argument; use --help' ;;
        esac
        shift
    done
    case "$bootstrap_channel:$bootstrap_risk" in
        stable:false|experimental:true) ;;
        *) fail 2 'experimental requires --accept-experimental-risk; stable rejects that flag' ;;
    esac
}

main() {
bootstrap_last=
for bootstrap_arg in "$@"; do bootstrap_last=$bootstrap_arg; done
if [ "$#" -eq 0 ] || [ "$bootstrap_last" != '--end-of-install-sh' ]; then
    fail 20 'install.sh was not downloaded completely'
fi
bootstrap_count=$(($# - 1))
bootstrap_index=0
for bootstrap_arg in "$@"; do
    if [ "$bootstrap_index" -eq 0 ]; then set --; fi
    bootstrap_index=$((bootstrap_index + 1))
    if [ "$bootstrap_index" -le "$bootstrap_count" ]; then set -- "$@" "$bootstrap_arg"; fi
done

if [ "$#" -eq 1 ]; then
    case "$1" in
        --version) printf 'intel-npu-stack-install %s\n' '0.1.1'; exit 0 ;;
        --help)
            printf '%s\n' 'Usage: install.sh [--dry-run] [--yes] [--channel stable|experimental]' \
                '  [--accept-experimental-risk] [--with-python] [--with-devel]' \
                '  [--version] [--help]' \
                'Run as a normal user. Experimental requires explicit risk acknowledgement.'
            exit 0 ;;
    esac
fi
check_options "$@"

bootstrap_uid=$(id -u) || fail 2 'cannot determine effective user ID'
case "$bootstrap_uid" in
    ''|*[!0-9]*) fail 2 'cannot determine effective user ID' ;;
    0) fail 2 'run as a normal user; privilege is requested only for the approved transaction' ;;
esac
for bootstrap_tool in curl sha256sum mktemp chmod rm; do
    command -v "$bootstrap_tool" >/dev/null 2>&1 || fail 20 'a required bootstrap tool is unavailable'
done
bootstrap_directory=$(mktemp -d) || fail 20 'cannot create a private download directory'
trap 'rm -rf -- "$bootstrap_directory"' EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
bootstrap_payload=$bootstrap_directory/installer

if ! curl --disable --fail --location --proto '=https' --proto-redir '=https' \
    --connect-timeout 15 --max-time 180 --max-filesize 67108864 \
    --output "$bootstrap_payload" -- https://archledger.github.io/intel-npu-stack/0.1.1/intel-npu-stack-install; then
    fail 20 'installer download did not complete'
fi
[ -f "$bootstrap_payload" ] && [ ! -L "$bootstrap_payload" ] || fail 20 'download is not a regular file'
if ! printf '%s  %s\n' '6c2f388bbb3bbb66fc2c143257f58e8db5568cc60495e928cbb6d62a2f94380e' "$bootstrap_payload" | sha256sum --check --status; then
    fail 20 'installer checksum mismatch'
fi
chmod 700 "$bootstrap_payload" || fail 20 'cannot prepare the verified installer'
"$bootstrap_payload" "$@"
}

main "$@" --end-of-install-sh
