#!/usr/bin/env bash
# Akış betiklerinde PHP yerine geçer: `artisan ...` komutunu önizleme sunucusunda
# site kullanıcısıyla koşar; `--gunluk` sunucunun laravel.log'unu yazar.
#   UZAK=root@104.247.171.84 UZAK_DIZIN=/home/psdevtr/public_html/okul \
#     PHP=bin/akis/uzak-php.sh bin/akis/zincir.sh <cikti> https://okul.psdev.tr
set -euo pipefail
: "${UZAK:?UZAK (ssh hedefi) gerekli}" "${UZAK_DIZIN:?UZAK_DIZIN gerekli}"
UZAK_KULLANICI=${UZAK_KULLANICI:-psdevtr}
UZAK_PHP=${UZAK_PHP:-/opt/cpanel/ea-php84/root/usr/bin/php}

ssh_kos() {
  LC_ALL=C LANG=C ssh -o BatchMode=yes -o ControlMaster=auto \
    -o ControlPath="$HOME/.ssh/cm-%r@%h-%p" -o ControlPersist=10m "$UZAK" "$1"
}

if [[ ${1:-} == --gunluk ]]; then
  ssh_kos "cat $UZAK_DIZIN/storage/logs/laravel.log 2>/dev/null || true"
  exit
fi
komut=$(printf '%q ' "$@")
ssh_kos "cd $UZAK_DIZIN && runuser -u $UZAK_KULLANICI -- $UZAK_PHP $komut"
