#!/bin/bash
. /etc/porky.ini

pbo="-k $dns_porkbun_key -s $dns_porkbun_secret"
domain=${DOMAIN:-xolatile.top}

nginx() {
    user=$1
    echo -e \
         "server {\n" \
         "    server_name $user.$domain;\n" \
         "    root /home/$user/www;\n" \
         "    location / {\n" \
         "        index index.html;\n" \
         "        autoindex on;\n" \
         "    }\n" \
         "    include $domain.base;\n" \
         "}"
}

dns() {
    user=$1
    pkb-client $pbo create-dns-record xolatile.top ALIAS --name $user --ttl 86400 $domain > /dev/null 2>&1 || true
}

case "$1" in
    "regen" ) x=3 ;;
    "dns"   ) x=2 ;;
    "nginx" ) x=1 ;;
    *       ) x=0 ;;
esac

shift
if [[ $x = 0 ]] ; then
    echo -e \
        'Need one of:\n'\
        'regen [(/home/($users))+]\n' \
        'dns   [(/home/($users))+]\n' \
        'nginx [(/home/($users))+]'
else
    for user in $@ ; do
        [ $(( $x & 2 )) = 2 ] && dns $user
        [ $(( $x & 1 )) = 1 ] && nginx $user
    done
fi
