#!/usr/bin/env bash

set -e

if [ -f "${SNAP_DATA}/settings.conf" ]; then
    logger "Already copied ${SNAP}/settings.conf to ${SNAP_DATA}/settings.conf"
    exit 0
fi

logger "Copying ${SNAP}/settings.conf to ${SNAP_DATA}/settings.conf"
cp "${SNAP}/settings.conf" "${SNAP_DATA}/settings.conf"

if [ $? -ne 0 ]; then
    logger "Failed moving settings.conf!"
    exit 1
else
    logger "Moved settings.conf successfully!"
fi

logger "Read initial settings to snap"
$SNAP/bin/update-settings "${SNAP_DATA}/settings.conf" "install" | while read LINE; do
    logger "${LINE}"
    snapctl set "${LINE}"
done
logger "Settings set. See 'snap get ${SNAP_NAME} -l'"