#!/usr/bin/env bash set -euo pipefail CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/yara724" CONFIG_FILE="$CONFIG_DIR/fanavaran-car-parts.env" usage() { cat <<'USAGE' Usage: scripts/get-fanavaran-car-parts.sh [--reset] Fetches Fanavaran's complete car-components lookup: GET /BimeApi/v2.0/car/base-info/car-components On first run, prompts for the Fanavaran credentials and request header values, then saves them with owner-only permissions under ~/.config/yara724/. Use --reset to enter new values. USAGE } die() { printf 'Error: %s\n' "$*" >&2 exit 1 } encode() { printf '%s' "$1" | base64 | tr -d '\n' } decode() { printf '%s' "$1" | base64 --decode } prompt_value() { local label="$1" variable="$2" secret="${3:-false}" value if [[ "$secret" == true ]]; then read -r -s -p "$label: " value printf '\n' >&2 else read -r -p "$label: " value fi [[ -n "$value" ]] || die "$label is required" printf -v "$variable" '%s' "$value" } save_config() { mkdir -p "$CONFIG_DIR" chmod 700 "$CONFIG_DIR" local temporary_file temporary_file="$(mktemp "$CONFIG_DIR/.fanavaran-car-parts.XXXXXX")" chmod 600 "$temporary_file" { printf 'BASE_URL=%s\n' "$(encode "$base_url")" printf 'APP_NAME=%s\n' "$(encode "$app_name")" printf 'APP_SECRET=%s\n' "$(encode "$app_secret")" printf 'USERNAME=%s\n' "$(encode "$fanavaran_username")" printf 'PASSWORD=%s\n' "$(encode "$fanavaran_password")" printf 'CORP_ID=%s\n' "$(encode "$corp_id")" printf 'CONTRACT_ID=%s\n' "$(encode "$contract_id")" printf 'LOCATION=%s\n' "$(encode "$location")" } > "$temporary_file" mv -f "$temporary_file" "$CONFIG_FILE" } load_config() { [[ -f "$CONFIG_FILE" ]] || return 1 # Config values are base64 encoded, so this file is safe to parse as data. while IFS='=' read -r key value; do case "$key" in BASE_URL) base_url="$(decode "$value")" ;; APP_NAME) app_name="$(decode "$value")" ;; APP_SECRET) app_secret="$(decode "$value")" ;; USERNAME) fanavaran_username="$(decode "$value")" ;; PASSWORD) fanavaran_password="$(decode "$value")" ;; CORP_ID) corp_id="$(decode "$value")" ;; CONTRACT_ID) contract_id="$(decode "$value")" ;; LOCATION) location="$(decode "$value")" ;; esac done < "$CONFIG_FILE" [[ -n "${base_url:-}" && -n "${app_name:-}" && -n "${app_secret:-}" && \ -n "${fanavaran_username:-}" && -n "${fanavaran_password:-}" && \ -n "${corp_id:-}" && -n "${contract_id:-}" && -n "${location:-}" ]] || die "Saved config is incomplete. Run with --reset to re-enter it." } prompt_config() { local client printf 'Choose Fanavaran tenant (parsian, tejaratno, moallem).\n' prompt_value 'Tenant' client case "${client,,}" in parsian|tejaratno) base_url_default='https://apimanager.iraneit.com/BimeApiManager/api' ;; moallem) base_url_default='https://api.inssw.ir/BimeApiManager/api' ;; *) die 'Tenant must be parsian, tejaratno, or moallem' ;; esac read -r -p "API manager base URL [$base_url_default]: " base_url base_url="${base_url:-$base_url_default}" base_url="${base_url%/}" [[ "$base_url" == https://* ]] || die 'Base URL must use HTTPS' prompt_value 'App name' app_name prompt_value 'App secret' app_secret true prompt_value 'Fanavaran username' fanavaran_username prompt_value 'Fanavaran password' fanavaran_password true prompt_value 'CorpId' corp_id prompt_value 'ContractId' contract_id prompt_value 'Location' location save_config printf 'Credentials saved privately to %s\n' "$CONFIG_FILE" >&2 } read_response_header() { local wanted="$1" file="$2" awk -F: -v wanted="$wanted" ' tolower($1) == tolower(wanted) { sub(/^[^:]*:[[:space:]]*/, "") sub(/\r$/, "") print exit } ' "$file" } check_http_status() { local status="$1" body_file="$2" label="$3" if [[ ! "$status" =~ ^2[0-9][0-9]$ ]]; then printf '%s failed (HTTP %s). Response:\n' "$label" "$status" >&2 cat "$body_file" >&2 printf '\n' >&2 exit 1 fi } reset_config=false if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then usage exit 0 elif [[ "${1:-}" == "--reset" ]]; then reset_config=true elif [[ $# -gt 0 ]]; then usage >&2 exit 2 fi if [[ "$reset_config" == true ]] || ! load_config; then prompt_config fi command -v curl >/dev/null || die 'curl is required' command -v base64 >/dev/null || die 'base64 is required' command -v node >/dev/null || die 'node is required to parse the login response' temporary_dir="$(mktemp -d)" chmod 700 "$temporary_dir" trap 'rm -rf "$temporary_dir"' EXIT printf 'Requesting Fanavaran app token...\n' >&2 cat > "$temporary_dir/app-token.headers" <&2 cat > "$temporary_dir/login.headers" < "$temporary_dir/lookup.headers" <&2 lookup_status="$(curl --silent --show-error --output "$temporary_dir/lookup.body" \ --write-out '%{http_code}' --request GET --header "@$temporary_dir/lookup.headers" \ "$base_url/BimeApi/v2.0/car/base-info/car-components")" check_http_status "$lookup_status" "$temporary_dir/lookup.body" 'Car-parts lookup' cat "$temporary_dir/lookup.body" printf '\n'