#!/usr/bin/env bash

# SETTINGS
ID=YOURID
PASS=YOURPASS
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/mingw64/bin:/c/WINDOWS/System32/Wbem

# FUNCTION
function get_seconds_since_boot {
    output=$(wmic os get lastbootuptime | awk 'NR==2')

    if [[ -z "$output" ]]; then
        return 1
    fi

    lastboot_date=${output:0:8} 
    lastboot_time=${output:8:6} 
    iso_date="${lastboot_date:0:4}-${lastboot_date:4:2}-${lastboot_date:6:2}T${lastboot_time:0:2}:${lastboot_time:2:2}:${lastboot_time:4:2}"

    lastboot_epoch=$(date -d "$iso_date" +%s)
    current_epoch=$(date +%s)

    echo $((current_epoch - lastboot_epoch))
}

# GET INFORMATION
UPTIME=$(get_seconds_since_boot)
if [[ -z "$UPTIME" ]]; then
    echo "Error: No uptime found"
    return 1
fi
OS=$(wmic os get caption,version,osarchitecture | awk 'NR==2' | tr -s ' ')
HOST=$(hostname)

# CALL API
curl -s --get \
  --data-urlencode "ID=${ID}" \
  --data-urlencode "UPTIME=${UPTIME}" \
  --data-urlencode "OS=${OS}" \
  --data-urlencode "PASS=${PASS}" \
  --data-urlencode "HOST=${HOST}" \
  https://horst.dyndns.org/api/update
