#!/bin/bash
# usage: webhook [message/mode] [notify]
##  webhook bootup # for bootup mode
##   webhook "silly soemething" # for normal webhook message
##   webhook "silly osmething" true # to ping with normal message
# usage in cron
##  @reboot bash /usr/share/customscripts/ifnet "/usr/share/customscripts/webhook bootup"

discord_webhook=$(cat /usr/share/customscripts/webhook.txt)
ipaddr=$(hostname -I | grep -E -o "10.0.0.[0-9]{1,3}" | tr '\n' ' ')
hostname=$(hostname)
date=$(date "+%d/%m/%Y %H:%M:%S %Z (%s)")
# to tag role run \@rolename and copy the code that is like <@something>
tag=$(cat /usr/share/customscripts/tag.txt)
bootup_string="bootrred $hostname on $date\n\tip addrs: $ipaddr\n\t$tag\n\n"

if [[ -z $1 ]]; then
    content='Default Ping!'
elif [[ "$1" == "bootup" ]]; then
        content="$bootup_string"
else
    content="$1"
fi

# any value in $2 will do ping mode
if [ ! -z $2 ]; then
    content+="\n$tag"
fi

post_data() {
cat <<EOF
    {
        "username":"$hostname",
        "content":"$content"
    }
EOF
}

echo -e "$content" # local echo
curl -H "Content-Type: application/json" -X POST -d "$(post_data)" "$discord_webhook" # send the webhook