#!/usr/bin/with-contenv bash
# shellcheck shell=bash

# create folders
mkdir -p \
    "${NEXTCLOUD_PATH}" \
    /config/crontabs

# install app
if [ ! -e "${NEXTCLOUD_PATH}/index.php" ]; then
    tar xf /app/nextcloud.tar.bz2 -C \
        "${NEXTCLOUD_PATH}" --strip-components=1
    chown abc:abc -R \
        "${NEXTCLOUD_PATH}"
    chmod +x "${NEXTCLOUD_PATH}/occ"
fi

# copy crontab if needed
if [[ ! -f /config/crontabs/root ]]; then
    cp /etc/crontabs/root /config/crontabs/
fi

sed -i "s|s6-setuidgid abc php[78] -f /config/www/nextcloud/cron.php|s6-setuidgid abc php -f /config/www/nextcloud/cron.php|" /config/crontabs/root

# import user crontabs
rm /etc/crontabs/*
cp /config/crontabs/* /etc/crontabs/

