#!/bin/bash set -e setup_user() { if ! getent group quartzgroup >/dev/null 2>&1; then addgroup -g "$GROUP_ID" quartzgroup fi if ! id -u quartz >/dev/null 2>&1; then adduser -D -u "$USER_ID" -G quartzgroup quartz fi } setup_cron() { # NEW_JOB="${BUILD_SCHEDULE} (cd /quartz/content && git pull) >/dev/null && echo && (cd /quartz/src && npx quartz build) >/dev/null 2>&1" NEW_JOB="${BUILD_SCHEDULE} (cd /quartz/content && git restore . && git pull) >/dev/null && (cd /quartz/src && npx quartz build) >/dev/null && echo 'Content Updated.'" if [[ "$ENABLE_CRON" == "true" ]]; then echo "Cron is enabled..." echo "Setting up cron job..." echo "$NEW_JOB" >/etc/crontabs/root /usr/sbin/crond -f & else echo "Cron is disabled" fi } verify_quartz() { if [ ! -d "/quartz/src" ]; then echo "Cloning Quartz..." git clone https://github.com/jackyzha0/quartz.git /quartz/src cd /quartz/src rm -rf /quartz/src/content ln -s /quartz/content /quartz/src git config --global --add safe.directory /quartz/src echo "Installing Quartz dependencies..." npm install >/dev/null echo "Building Quartz..." npx quartz build >/dev/null fi } download_content() { mkdir -p /quartz/content # Ensure directory exists if [ -n "${CONTENT_REPO}" ] && [ -z "$(ls -A /quartz/content 2>/dev/null)" ]; then echo "Downloading content..." git clone "${CONTENT_REPO}" /quartz/content fi git config --global --add safe.directory /quartz/content } main() { echo "Setting up Quartz environment..." setup_user setup_cron verify_quartz download_content rm -rf /quartz/src/content ln -s /quartz/content /quartz/src echo "Setting correct permissions..." chown -R "${USER_ID}":"${GROUP_ID}" /quartz chmod -R 755 /quartz echo "NGINX variable substitution..." /docker-entrypoint.d/20-envsubst-on-templates.sh >/dev/null echo "Building Quartz..." su quartz -c "bash -c 'cd /quartz/src && npx quartz build >/dev/null'" echo "Starting Nginx..." exec nginx -g "daemon off; error_log warn;" } main