#!/usr/bin/with-contenv bash

if [[ ! -f /data/dav.db ]]; then
    echo -e "\t\t\033[1;33m* creating /data/dav.db database\033[0m"

    cat /defaults/dav/sqlite.addressbooks.sql | sqlite3 /data/dav.db
    cat /defaults/dav/sqlite.calendars.sql | sqlite3 /data/dav.db
    cat /defaults/dav/sqlite.locks.sql | sqlite3 /data/dav.db
    cat /defaults/dav/sqlite.propertystorage.sql | sqlite3 /data/dav.db
fi

# removed stat --format '%U' ... and stat --format '%G' ... to get rid of statx bug
user="$(/bin/core-username "/data/dav.db")"
group="$(/bin/core-groupname "/data/dav.db")"
if [[ "mail" != "${user}" ]] || [[ "mail" != "${group}" ]] ; then
    chown mail:mail /data/dav.db
fi
