
systemctl daemon-reload


START=0

if [ "$1" = "configure" ] && [ "$2" = "" ]; then

    type cscli

    if [ "$?" -eq "0" ] ; then
        START=1
        echo "cscli/crowdsec is present, generating API key"
        unique=`date +%s`
        API_KEY=`cscli -oraw bouncers add CustomBouncer-${unique}`
        if [ $? -eq 1 ] ; then
            echo "failed to create API token, service won't be started."
            START=0
            API_KEY="<API_KEY>"
        else
            echo "API Key : ${API_KEY}"
        fi

        TMP=`mktemp -p /tmp/`
        cp /etc/crowdsec/bouncers/crowdsec-custom-bouncer.yaml ${TMP}
        API_KEY=${API_KEY} envsubst < ${TMP} > /etc/crowdsec/bouncers/crowdsec-custom-bouncer.yaml
        rm ${TMP}
    fi
else
    START=1
fi


if [ ${START} -eq 0 ] ; then
    echo "no api key was generated"
fi

echo "please enter the binary path in '/etc/crowdsec/bouncers/crowdsec-custom-bouncer.yaml' and start the bouncer via 'sudo systemctl start crowdsec-custom-bouncer' "
