#!/usr/bin/env bash
# ============================================================
# Zip ONE project folder for download — safely.
#
# Makes a single .tar.gz of a folder under /home/ashraffarid2010 so it
# downloads as one file (fast) instead of thousands of small files.
# Checks free disk FIRST (never repeat the pkgacct disk-full incident),
# then prints the scp command. Delete the archive after downloading.
#
#   ops/zip-for-download.sh <folder>            # zip one folder
#   ops/zip-for-download.sh <folder> --full     # include node_modules/.git too
#   ops/zip-for-download.sh --list              # list foldable projects
#
# Archives go to /home/ashraffarid2010/_downloads/ (chmod 700).
# By default excludes node_modules/.git/cache (regenerable) to stay small.
# ============================================================
set -uo pipefail
HOME_DIR="/home/ashraffarid2010"
STAGE="$HOME_DIR/_downloads"
MARGIN_GB=10   # keep at least this much free after zipping

log(){ echo -e "\033[1;36m[zip]\033[0m $*"; }
ok(){ echo -e "\033[1;32m[ ok ]\033[0m $*"; }
die(){ echo -e "\033[1;31m[FAIL]\033[0m $*" >&2; exit 1; }

[ $# -ge 1 ] || die "usage: ops/zip-for-download.sh <folder> [--full]  |  --list"

if [ "$1" = "--list" ]; then
  cd "$HOME_DIR" && ls -d */ 2>/dev/null | sed 's#/##' \
    | grep -viE '^(mail|etc|ssl|logs|access-logs|tmp|_downloads|\.trash|public_ftp|perl5|backups|.*-backups|\.cpanel|\.config|\.ssh|\.pki|\.gnupg)$'
  exit 0
fi

FOLDER="$1"; FULL=0
[ "${2:-}" = "--full" ] && FULL=1
SRC="$HOME_DIR/$FOLDER"
[ -d "$SRC" ] || die "folder not found: $SRC"

mkdir -p "$STAGE" 2>/dev/null || true; chmod 700 "$STAGE" 2>/dev/null || true
OUT="$STAGE/${FOLDER}.tar.gz"

# ---- disk safety: estimate source size, ensure it fits with margin ----
log "Checking disk before zipping…"
if [ "$FULL" = 1 ]; then
  SRC_KB=$(du -sk "$SRC" 2>/dev/null | cut -f1)
  EXCLUDES=()
else
  SRC_KB=$(du -sk --exclude=node_modules --exclude=.git --exclude=cache --exclude='*.log' "$SRC" 2>/dev/null | cut -f1)
  EXCLUDES=(--exclude='node_modules' --exclude='.git' --exclude='cache' --exclude='*.log')
fi
FREE_KB=$(df -Pk "$STAGE" | awk 'NR==2{print $4}')
NEED_KB=$(( SRC_KB + MARGIN_GB*1024*1024 ))   # source (compresses smaller, so this is worst-case) + margin
SRC_GB=$(( SRC_KB/1024/1024 )); FREE_GB=$(( FREE_KB/1024/1024 ))
log "folder ~${SRC_GB}GB (uncompressed), free ${FREE_GB}GB"
if [ "$FREE_KB" -lt "$NEED_KB" ]; then
  die "not enough free disk (need ~${SRC_GB}GB + ${MARGIN_GB}GB margin, have ${FREE_GB}GB). Download+delete other archives first, or use rsync streaming instead."
fi

# ---- zip ----
log "Zipping $FOLDER → $OUT …"
tar czf "$OUT" -C "$HOME_DIR" "${EXCLUDES[@]}" "$FOLDER" 2>/dev/null \
  && ok "created $(du -h "$OUT" | cut -f1): $OUT" \
  || die "tar failed"
chmod 600 "$OUT"; chown ashraffarid2010:ashraffarid2010 "$OUT" 2>/dev/null || true

cat <<EOF

Download it FROM YOUR MACHINE:
  scp -P 21098 ashraffarid2010@203.161.35.97:$OUT "F:\\SERVER 17-7\\"

Then delete the archive from the server (keeps the original folder):
  ssh -p 21098 ashraffarid2010@203.161.35.97 "rm -f '$OUT'"
EOF
