#!/bin/sh
# Deploy the builds site to YunoHost (My Webapp, static).
#   ./deploy       push the site
#   ./deploy -n    dry run: show what would change, change nothing
USER=admin
HOST=social
DIR=/var/www/my_webapp__3/www

case "$DIR" in
    *my_webapp__N/*)
        echo "Set DIR in ./deploy first: replace my_webapp__N with this app's folder under /var/www on the server."
        echo "    ssh ${HOST} 'ls -d /var/www/my_webapp*'"
        exit 1 ;;
esac

DRY=""
[ "$1" = "-n" ] && DRY="--dry-run" && echo "Dry run: nothing will be changed."

cd "$(dirname "$0")" || exit 1

# Everything in this folder is the site, apart from the project's own files.
# --delete removes files on the server that no longer exist here.
rsync -avz --no-t --no-p --delete $DRY \
    --exclude '.git/' \
    --exclude '.gitignore' \
    --exclude '.claude/' \
    --exclude '.DS_Store' \
    --exclude 'deploy' \
    --exclude 'README.md' \
    --exclude 'CLAUDE.md' \
    --exclude '*.swp' --exclude '*.swo' --exclude '*~' \
    ./ ${HOST}:${DIR}/
STATUS=$?

# Make everything readable by the web server, whatever the local permissions were.
if [ $STATUS -eq 0 ] && [ -z "$DRY" ]; then
    ssh ${HOST} "chmod -R a+rX ${DIR}" || STATUS=$?
fi

exit $STATUS
