#!/bin/sh

if [ ! -f /etc/blogi/config.yaml ]; then

    if [ ! -d /etc/blogi ]; then
        mkdir /etc/blogi
    fi

    CLIENTSECRET=$(pwgen -1)

    /usr/local/bin/authcli addclient -h $CLIENTURL -n $(hostname) -u $USERNAME -p $PASSWORD -s $CLIENTSECRET

    sed  -e "s|%CLIENTURL%|$CLIENTURL|g" \
    -e "s|%CLIENTNAME%|$(hostname)|g" \
    -e "s|%CLIENTSECRET%|$CLIENTSECRET|g" \
    -e "s|%HOSTNAME%|$(hostname)|g" \
    -e "s|%DBDRIVER%|$DBDRIVER|g" \
    -e "s|%DBOPTIONS%|$DBOPTIONS|g" \
    -e "s|%PORT%|$PORT|g" \
    -e "s|%BINDADDR%|$BINDADDR|g" \
    -e "s|%MAXCON%|$MAXCON|g" \
    -e "s|%PREFIX%|$PREFIX|g" \
    -e "s|%REMOTEURL%|$REMOTEURL|g" \
    -e "s|%DOMAIN%|$DOMAIN|g" \
    -e "s|%COOKIEDOMAIN%|$COOKIEDOMAIN|g" \
    -e "s|%THEMEPATH%|$THEMEPATH|g" \
    /usr/local/share/blogi/docker-config.yaml > /etc/blogi/config.yaml
fi

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/smtpclient

/usr/local/bin/blogi -F true -c "yaml://etc/blogi/config.yaml"
