#!/bin/bash
# Duong dan dung: wp-content/plugins/datvetaucaotoc-booking/datvetaucaotoc-booking.php
#
#   bash install-on-linux.sh /tmp/datvetaucaotoc-booking.zip
#   bash install-on-linux.sh /tmp/datvetaucaotoc-booking.zip /var/www/html/wp-content/plugins
#
set -euo pipefail

ZIP="${1:-}"
PLUGINS_DIR="${2:-/var/www/html/wp-content/plugins}"

if [[ -z "$ZIP" || ! -f "$ZIP" ]]; then
  echo "Thieu file zip. Vi du:"
  echo "  bash install-on-linux.sh /tmp/datvetaucaotoc-booking.zip"
  exit 1
fi

TARGET="${PLUGINS_DIR}/datvetaucaotoc-booking"

echo "Xoa ban cu trong ${PLUGINS_DIR}..."
rm -rf "${PLUGINS_DIR}"/datvetaucaotoc-booking*

echo "Giai nen vao ${TARGET}..."
mkdir -p "$TARGET"
unzip -o "$ZIP" -d "$TARGET"

MAIN="${TARGET}/datvetaucaotoc-booking.php"
if [[ ! -f "$MAIN" ]]; then
  echo "LOI: Khong thay datvetaucaotoc-booking.php trong ${TARGET}"
  ls -la "$TARGET"
  exit 1
fi

if id apache &>/dev/null; then
  chown -R apache:apache "$TARGET" 2>/dev/null || true
elif id www-data &>/dev/null; then
  chown -R www-data:www-data "$TARGET" 2>/dev/null || true
fi

echo "OK: ${MAIN}"
echo "WordPress -> Plugins -> Kich hoat (1 dong, khong co -xxxxx trong duong dan)."
