#!/usr/bin/env bash

set -e
set -o pipefail

GODADDY_KEY="${GODADDY_KEY:-$(pass show godaddy.com/api/key)}"
GODADDY_SECRET="${GODADDY_SECRET:-$(pass show godaddy.com/api/secret)}"
GODADDY_COMMAND="${GODADDY_COMMAND:-lexicon godaddy}"

help_main()
{
    echo "\
Usage: godaddy COMMANDS ARGS...
Run COMMANDS with ARGS

COMMAND must be one of the sub-commands listed below:

   add
   list

Report bugs to: go.wigust@gmail.com."
}

help_add()
{
    echo "\
Usage: godaddy add COMMANDS ARGS...
Run COMMANDS with ARGS

COMMAND must be one of the sub-commands listed below:

   a
   cname godaddy add cname githunt guix.duckdns.org
   mx

Report bugs to: go.wigust@gmail.com."
}

help_add_a()
{
    echo "\
Usage: godaddy add a subdomain.example.com 1.2.3.4
Run COMMANDS with ARGS

Report bugs to: go.wigust@gmail.com."
}

help_add_cname()
{
    echo "\
Usage: godaddy add cname subdomain.example.com target.domain.com
Run COMMANDS with ARGS

Report bugs to: go.wigust@gmail.com."
}

help_add_mx()
{
    echo "\
Usage: godaddy add mx vm12345 vm12345.wugi.info 10
   or: godaddy add mx vm12345
Run COMMANDS with ARGS

Report bugs to: go.wigust@gmail.com."
}

help_delete()
{
    echo "\
Usage: godaddy delete COMMANDS ARGS...
Run COMMANDS with ARGS

COMMAND must be one of the sub-commands listed below:

   a
   cname godaddy delete cname githunt guix.duckdns.org

Report bugs to: go.wigust@gmail.com."
}

help_delete_a()
{
    echo "\
Usage: godaddy delete a subdomain.example.com 1.2.3.4
Run COMMANDS with ARGS

Report bugs to: go.wigust@gmail.com."
}

help_delete_cname()
{
    echo "\
Usage: godaddy delete cname subdomain.example.com target.domain.com
Run COMMANDS with ARGS

Report bugs to: go.wigust@gmail.com."
}

help_list()
{
    echo "\
Usage: godaddy list COMMANDS ARGS...
Run COMMANDS with ARGS

COMMAND must be one of the sub-commands listed below:

   a
   cname
   mx
   ns

Report bugs to: go.wigust@gmail.com."
}

case "$1" in
    --help)
        help_main
        ;;
    add)
        case "$2" in
            --help)
                help_add
                ;;
            a)
                if [[ -z "$3" ]]
                then
                    help_add_a
                    exit 1
                fi
                $GODADDY_COMMAND create --auth-key "$GODADDY_KEY" --auth-secret "$GODADDY_SECRET" "$GODADDY_DOMAIN_NAME" A --name "$3" --content "$4"
                ;;
            cname)
                if [[ -z "$3" ]]
                then
                    help_add_cname
                    exit 1
                fi
                $GODADDY_COMMAND create --auth-key "$GODADDY_KEY" --auth-secret "$GODADDY_SECRET" "$GODADDY_DOMAIN_NAME" CNAME --name "$3" --content "$4"
                ;;
            mx)
                if [[ -z "$3" ]]
                then
                    help_add_mx
                    exit 1
                fi
                $GODADDY_COMMAND create --auth-key "$GODADDY_KEY" --auth-secret "$GODADDY_SECRET" "$GODADDY_DOMAIN_NAME" MX --name "$3" --content "${4:-$3.wugi.info}" --priority "${5:-10}"
                ;;
            *)
                help_add
                ;;
        esac
        ;;
    delete)
        case "$2" in
            --help)
                help_delete
                ;;
            a)
                if [[ -z "$3" ]]
                then
                    help_delete_a
                    exit 1
                fi
                $GODADDY_COMMAND delete --auth-key "$GODADDY_KEY" --auth-secret "$GODADDY_SECRET" "$GODADDY_DOMAIN_NAME" A --name "$3" --content "$4"
                ;;
            cname)
                if [[ -z "$3" ]]
                then
                    help_delete_cname
                    exit 1
                fi
                $GODADDY_COMMAND delete --auth-key "$GODADDY_KEY" --auth-secret "$GODADDY_SECRET" "$GODADDY_DOMAIN_NAME" CNAME --name "$3" --content "$4"
                ;;
            *)
                help_delete
                ;;
        esac
        ;;
    list)
        case "$2" in
            --help)
                help_list
                ;;
            a)
                $GODADDY_COMMAND list --auth-key "$GODADDY_KEY" --auth-secret "$GODADDY_SECRET" "$GODADDY_DOMAIN_NAME" A "${@:3}"
                ;;
            cname)
                $GODADDY_COMMAND list --auth-key "$GODADDY_KEY" --auth-secret "$GODADDY_SECRET" "$GODADDY_DOMAIN_NAME" CNAME "${@:3}"
                ;;
            ns)
                $GODADDY_COMMAND list --auth-key "$GODADDY_KEY" --auth-secret "$GODADDY_SECRET" "$GODADDY_DOMAIN_NAME" NS "${@:3}"
                ;;
            mx)
                $GODADDY_COMMAND list --auth-key "$GODADDY_KEY" --auth-secret "$GODADDY_SECRET" "$GODADDY_DOMAIN_NAME" MX "${@:3}"
                ;;
            *)
                help_list
                ;;
        esac
        ;;
    *)
        help_main
        ;;
esac
