#!/bin/bash

if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then
    set -x
fi
set -o errexit
set -o nounset
set -o pipefail


echo "############################################################"
echo "# create stack user with ssh-passwd access                 #"
echo "############################################################"

sed -i "s\ssh_pwauth:   0\ssh_pwauth:   1\g" /etc/cloud/cloud.cfg
sed -i "/users:/,3d" /etc/cloud/cloud.cfg
# passwd hash generated using: perl -e 'print crypt("<secret>","\$6\$saltsalt\$") . "\n"'
tee --append /etc/cloud/cloud.cfg <<EOF
users:
  - default
  - name: stack
    sudo: ALL=(ALL) NOPASSWD:ALL
    groups: users, admin
    ssh_import_id: None
    lock_passwd: false
    passwd: \$6\$saltsalt\$cA6Pf9Pjh4h/PiCtjblwdNgFNG/yI5JDnqEnPFCxSVi.57mbF0vBdVJ8lAEIB5dUyJ2NNao.8IDkIbTJEblKW0
    ssh_pwauth: True
    chpasswd: { expire: False }
EOF