#!/bin/bash
# 🚀 Script for deploying transfer credit files to Tapi University Server

HOST="110.164.69.50"
USER="pongpon"
PASS="P@ngP@n29012520"
REMOTE_STAGING="/home/pongpon/transfer_deploy"
REMOTE_LIVE="/var/www/html/transfer"

echo "--------------------------------------------------------"
echo "📦 1. Packaging modified files..."
echo "--------------------------------------------------------"
tar -czf deploy.tar.gz \
  index.php \
  includes/header.php \
  includes/functions.php \
  students/list.php \
  students/add.php \
  students/delete.php \
  students/import_csv.php \
  students/view.php \
  students/edit.php \
  curriculum/dt.php \
  curriculum/dbc.php \
  users/profile.php \
  users/profile_edit.php \
  api/add_manual_course.php \
  api/delete_course.php \
  api/update_grade.php \
  api/upload_pdf.php \
  api/upload_text.php \
  pic/dashboard_curriculum.png \
  pic/dashboard_student_dt.png \
  pic/dashboard_student_dbc.png \
  pic/dashboard_student_all.png \
  pic/dashboard_transfer_credits.png

if [ $? -ne 0 ]; then
  echo "❌ Error: Failed to create tarball."
  exit 1
fi
echo "✓ Tarball deploy.tar.gz created successfully."

echo ""
echo "--------------------------------------------------------"
echo "📤 2. Uploading tarball to server..."
echo "🔑 Please enter password: $PASS"
echo "--------------------------------------------------------"
scrap_file="deploy.tar.gz"
scp $scrap_file $USER@$HOST:$REMOTE_STAGING.tar.gz

if [ $? -ne 0 ]; then
  echo "❌ Error: SCP upload failed."
  rm -f deploy.tar.gz
  exit 1
fi
echo "✓ Upload completed."

echo ""
echo "--------------------------------------------------------"
echo "⚙ 3. Extracting and deploying to live webroot..."
echo "🔑 Password for ssh and sudo is: $PASS"
echo "--------------------------------------------------------"
ssh $USER@$HOST "
  echo 'Extracting staging files...' && \
  rm -rf $REMOTE_STAGING && \
  mkdir -p $REMOTE_STAGING && \
  tar -xzf $REMOTE_STAGING.tar.gz -C $REMOTE_STAGING && \
  rm -f $REMOTE_STAGING.tar.gz && \
  \
  echo 'Copying files and setting permissions...' && \
  echo '$PASS' | sudo -S bash -c \"
    mkdir -p $REMOTE_LIVE/includes $REMOTE_LIVE/students $REMOTE_LIVE/curriculum $REMOTE_LIVE/users $REMOTE_LIVE/api $REMOTE_LIVE/pic && \
    cp $REMOTE_STAGING/index.php $REMOTE_LIVE/index.php && \
    cp $REMOTE_STAGING/includes/header.php $REMOTE_LIVE/includes/header.php && \
    cp $REMOTE_STAGING/includes/functions.php $REMOTE_LIVE/includes/functions.php && \
    cp $REMOTE_STAGING/students/list.php $REMOTE_LIVE/students/list.php && \
    cp $REMOTE_STAGING/students/add.php $REMOTE_LIVE/students/add.php && \
    cp $REMOTE_STAGING/students/delete.php $REMOTE_LIVE/students/delete.php && \
    cp $REMOTE_STAGING/students/import_csv.php $REMOTE_LIVE/students/import_csv.php && \
    cp $REMOTE_STAGING/students/view.php $REMOTE_LIVE/students/view.php && \
    cp $REMOTE_STAGING/students/edit.php $REMOTE_LIVE/students/edit.php && \
    cp $REMOTE_STAGING/curriculum/dt.php $REMOTE_LIVE/curriculum/dt.php && \
    cp $REMOTE_STAGING/curriculum/dbc.php $REMOTE_LIVE/curriculum/dbc.php && \
    cp $REMOTE_STAGING/users/profile.php $REMOTE_LIVE/users/profile.php && \
    cp $REMOTE_STAGING/users/profile_edit.php $REMOTE_LIVE/users/profile_edit.php && \
    cp $REMOTE_STAGING/api/add_manual_course.php $REMOTE_LIVE/api/add_manual_course.php && \
    cp $REMOTE_STAGING/api/delete_course.php $REMOTE_LIVE/api/delete_course.php && \
    cp $REMOTE_STAGING/api/update_grade.php $REMOTE_LIVE/api/update_grade.php && \
    cp $REMOTE_STAGING/api/upload_pdf.php $REMOTE_LIVE/api/upload_pdf.php && \
    cp $REMOTE_STAGING/api/upload_text.php $REMOTE_LIVE/api/upload_text.php && \
    cp $REMOTE_STAGING/pic/dashboard_curriculum.png $REMOTE_LIVE/pic/dashboard_curriculum.png && \
    cp $REMOTE_STAGING/pic/dashboard_student_dt.png $REMOTE_LIVE/pic/dashboard_student_dt.png && \
    cp $REMOTE_STAGING/pic/dashboard_student_dbc.png $REMOTE_LIVE/pic/dashboard_student_dbc.png && \
    cp $REMOTE_STAGING/pic/dashboard_student_all.png $REMOTE_LIVE/pic/dashboard_student_all.png && \
    cp $REMOTE_STAGING/pic/dashboard_transfer_credits.png $REMOTE_LIVE/pic/dashboard_transfer_credits.png && \
    chown -R www-data:www-data $REMOTE_LIVE/index.php $REMOTE_LIVE/includes/header.php $REMOTE_LIVE/includes/functions.php $REMOTE_LIVE/students $REMOTE_LIVE/curriculum $REMOTE_LIVE/users $REMOTE_LIVE/api $REMOTE_LIVE/pic && \
    chmod 644 $REMOTE_LIVE/index.php $REMOTE_LIVE/includes/header.php $REMOTE_LIVE/includes/functions.php $REMOTE_LIVE/students/*.php $REMOTE_LIVE/curriculum/*.php $REMOTE_LIVE/users/*.php $REMOTE_LIVE/api/*.php $REMOTE_LIVE/pic/*.png && \
    chmod 755 $REMOTE_LIVE/students $REMOTE_LIVE/curriculum $REMOTE_LIVE/users $REMOTE_LIVE/api $REMOTE_LIVE/pic
  \" && \
  \
  echo '✓ Deployment completed successfully!'
"

# Clean up local tarball
rm -f deploy.tar.gz
echo ""
echo "🎉 Done! All changes are live on transfer.tapee.ac.th!"
