#!/bin/bash -e
# generic preseeding of inithooks.conf if it doesn't exist

[ -n "$_TURNKEY_INIT" ] && exit 0

[ -e $INITHOOKS_CONF ] && exit 0

# generate (pseudo) random 8 char base64 string & check that it meets
# complexity requirments (at least one uppercase, lowercase & number).
unset check
while [ -z "$check" ]; do
    MASTERPASS="$(openssl rand -base64 6)"
    [[ "$MASTERPASS" == *[A-Z]* && \
        "$MASTERPASS" == *[a-z]* && \
        "$MASTERPASS" == *[0-9]* ]] \
    && check=good
done

cat>$INITHOOKS_CONF<<EOF
export ROOT_PASS=$MASTERPASS
export DB_PASS=$MASTERPASS
export APP_PASS=$MASTERPASS
export APP_EMAIL=admin@example.com
export APP_DOMAIN=DEFAULT
export HUB_APIKEY=SKIP
export SEC_ALERTS=SKIP
export SEC_UPDATES=SKIP
EOF

chmod +x /usr/lib/inithooks/firstboot.d/??turnkey-init-fence*

