#!/bin/sh
set -e

bin=$SNAP/sshpiperd
config=$SNAP_DATA/sshpiperd.ini

args=""
for opt in $($bin options); do
    v=$(snapctl get $opt)

    if [ "$v" != "" ]; then
        v=$(eval echo $v)
        args="$args --$opt=$v"
    
    # special default
    elif [ "$opt" = "workingdir" ]; then
        mkdir -p $SNAP_COMMON/workingdir
        args="$args --$opt=$SNAP_COMMON/workingdir"
    elif [ "$opt" = "auditor-typescriptlogger-outputdir" ]; then
        mkdir -p $SNAP_COMMON/typescriptlog
        args="$args --$opt=$SNAP_COMMON/typescriptlog"
    elif [ "$opt" = "server-key" ]; then
        args="$args --$opt=$SNAP_COMMON/host_rsa_key"
    fi
done

echo ";DO NOT MODIFY, USE snap set sshpiperd key=val, AUTO GENERATED" $(date) > $config
$bin --config /dev/null $args dumpconfig >> $config
