#!/usr/bin/sh

if [[ $EUID -ne 0 ]]; then
    echo "Must run as root!" >&2;
    exit 1
fi

# It will create a new encription key if it doesn't exists.
systemd-creds setup

if [[ $1 ]]; then
    char_num=$1;
else
    char_num=64;
fi

service_override=$(pkg-config systemd --variable=systemdsystemconfdir)/dwelling-upload.service.d/override.conf

if [ ! -f $service_override ]; then
    echo "[Service]" > $service_override;
fi

cat /dev/urandom | tr -dc 'a-zA-Z0-9!@#$%^&*' | fold -w $char_num | head -n 1 \
    | systemd-creds encrypt -qp --name=salt - - 2> /dev/null >> $service_override;

systemctl daemon-reload