#!/bin/sh
ip_addr=$1
ping_count="3"
ping_interval="10"

update_ha_state() {
    local active=$1
    curl -X POST \
        -H "Content-Type: application/json" \
        -d "{\"active\": $active}" \
        http://localhost:8668/update_ha_state
}

if [ -z "$ip_addr" ]; then
    echo "usage: $(basename $0) ip_addr"
    exit 1
fi

ping -c $ping_count -i $ping_interval $ip_addr
if [ $? -eq 0 ]; then
    update_ha_state false
else
    update_ha_state true
fi
