#!/bin/bash -e
# set samba domain, realm and administrator password

. /etc/default/inithooks

[ -e $INITHOOKS_CONF ] && . $INITHOOKS_CONF

# support partially preseeded headless deployments by generating APP_REALM
if [ -z "$APP_REALM" ]; then
    if [ -n "$APP_PASS" ] && [ -n "$APP_DOMAIN" ]; then
        APP_REALM="$APP_DOMAIN.lan"
    fi
fi

# If APP_DOMAIN & APP_PASS are preseeded (but not APP_REALM) then APP_REALM
# generated as per above.
# If APP_DOMAIN, APP_PASS and APP_REALM are passed to domain-controller.py;
# then it will run non-interactively and will create a new domain.
# If APP_JOIN_NS is also set, then it will try to join an existing domain.
#
# For more info see './domain-controller.py --help'

$INITHOOKS_PATH/bin/domain-controller.py --domain="$APP_DOMAIN" \
                --realm="$APP_REALM" --pass="$APP_PASS" \
                --join_ns="$APP_JOIN_NS" --username="$APP_ADMIN_USER"
