11#! /usr/bin/env bash
22# ##############################################################################
3- # SPARROW Setup Script 31-October -2025
3+ # SPARROW Setup Script 18-November -2025
44# ##############################################################################
55set -euo pipefail
66
@@ -85,6 +85,10 @@ AUDIO_BIRDS_MODEL_FILENAME_FINAL="model.onnx"
8585REPO_URL=" https://github.com/microsoft/sparrow-client"
8686CLONE_DIR=" "
8787
88+ ONBOARDING_URL=" https://server.sparrow-earth.com/onboarding"
89+
90+ USERNAME=" "
91+
8892# ##############################################################################
8993# 2. -- UTILITY FUNCTIONS --
9094# ##############################################################################
@@ -108,6 +112,34 @@ generate_unique_id() {
108112 log " Generated UUID: $NEW_UUID "
109113}
110114
115+ get_hardware_id () {
116+ if [[ ! -f " $UUID_FILE " ]]; then
117+ log " UUID file not found at $UUID_FILE "
118+ return 1
119+ fi
120+
121+ local uuid
122+ uuid=$( tr -d ' \r\n' < " $UUID_FILE " )
123+
124+ if [[ -z " $uuid " ]]; then
125+ log " UUID in $UUID_FILE is empty"
126+ return 1
127+ fi
128+
129+ local hwid
130+ hwid=$( printf ' %s' " $uuid " | sha256sum | awk ' {print substr($1,1,12)}' )
131+
132+ if [[ -z " $hwid " ]]; then
133+ log " Failed to derive hardware id from UUID"
134+ return 1
135+ fi
136+
137+ # Log to file, but don't let log()'s stdout leak into command substitution
138+ log " Generated Hardware ID for onboarding: $hwid " > /dev/null
139+ echo " $hwid "
140+ }
141+
142+
111143install_curl () { command_exists curl || { apt-get update -y; apt-get install -y curl; } }
112144install_wget () { command_exists wget || { apt-get update -y; apt-get install -y wget; } }
113145install_git () { command_exists git || { apt-get update -y; apt-get install -y git; } }
@@ -522,8 +554,89 @@ configure_access_key() {
522554 mkdir -p " $SYSTEM_FOLDER /sparrow/config" " $SYSTEM_FOLDER /starlink/config"
523555 echo " $k1 " > " $SYSTEM_FOLDER /sparrow/config/access_key.txt"
524556 echo " $k1 " > " $SYSTEM_FOLDER /starlink/config/access_key.txt"
557+
558+ # Prompt for username (in-memory only)
559+ while true ; do
560+ USERNAME=$( _input " Enter Sparrow Username" )
561+ if [[ -z " $USERNAME " ]]; then
562+ _error " Username cannot be empty - try again."
563+ continue
564+ fi
565+ break
566+ done
525567}
526568
569+ onboard_device () {
570+ local max_retries=5
571+ local attempt=1
572+ local hwid api_key resp status_code body access_key_file json_payload
573+
574+ access_key_file=" $SYSTEM_FOLDER /sparrow/config/access_key.txt"
575+
576+ if [[ ! -f " $access_key_file " ]]; then
577+ log " Access key file not found at $access_key_file "
578+ _error " Cannot onboard: access key not found."
579+ return 1
580+ fi
581+
582+ # Strip any line endings from the key
583+ api_key=$( tr -d ' \r\n' < " $access_key_file " )
584+
585+ if [[ -z " $api_key " ]]; then
586+ _error " Access key file is empty; cannot onboard."
587+ return 1
588+ fi
589+
590+ if [[ -z " $USERNAME " ]]; then
591+ _error " Username is not set in memory; cannot onboard."
592+ return 1
593+ fi
594+
595+ hwid=$( get_hardware_id) || {
596+ _error " Cannot compute hardware id for onboarding."
597+ return 1
598+ }
599+
600+ # Build JSON payload safely (no shell interpolation weirdness)
601+ json_payload=$( printf ' {"unit_id":"%s"}' " $hwid " )
602+
603+ log " Onboarding JSON payload (len=${# json_payload} ): $json_payload "
604+
605+ while (( attempt <= max_retries )) ; do
606+ log " Onboarding attempt $attempt /$max_retries for unit_id=$hwid (username=$USERNAME )"
607+
608+ resp=$( curl -sS -w " %{http_code}" \
609+ -X POST " $ONBOARDING_URL " \
610+ -H " Content-Type: application/json" \
611+ -H " X-API-Key: $api_key " \
612+ -H " X-Username: $USERNAME " \
613+ --data-binary " $json_payload " 2> /dev/null || true)
614+
615+ status_code=${resp: -3}
616+ body=${resp:: -3}
617+
618+ if [[ " $status_code " == " 200" || " $status_code " == " 201" ]]; then
619+ log " Onboarding successful: HTTP $status_code , body: $body "
620+ _info " Onboarding successful for hardware ID: $hwid "
621+ return 0
622+ fi
623+
624+ log " Onboarding failed (HTTP $status_code ): $body "
625+
626+ if ! _yesno " Onboarding failed (HTTP $status_code ). Retry?" ; then
627+ _error " User aborted onboarding."
628+ return 1
629+ fi
630+
631+ attempt=$(( attempt+ 1 ))
632+ sleep 2
633+ done
634+
635+ _error " Onboarding failed after $max_retries attempts."
636+ return 1
637+ }
638+
639+
527640# ##############################################################################
528641# 4. -- MAIN SCRIPT LOGIC --
529642# ##############################################################################
@@ -557,6 +670,7 @@ setup_persistent_wifi_hotspot
557670install_smbus2
558671seed_ds3231
559672configure_access_key
673+ onboard_device || log " Onboarding did not complete successfully; continuing setup."
560674
561675if _yesno " Install TeamViewer Host (ARM64)?" ; then
562676 _progress " Installing TeamViewer Host..." bash -c '
0 commit comments