#!/usr/bin/env bash

set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TIMESTAMP="$(date +"%Y%m%d-%H%M%S")"
BACKUP_ROOT="$ROOT_DIR/storage/app/backups/$TIMESTAMP"
TEMP_DB_DIR="$BACKUP_ROOT/database"
CODE_ZIP="$BACKUP_ROOT/codigo-repositorio-$TIMESTAMP.zip"
DB_SQL="$TEMP_DB_DIR/base-de-datos-$TIMESTAMP.sql"
DB_ZIP="$BACKUP_ROOT/base-de-datos-$TIMESTAMP.zip"
MANIFEST="$BACKUP_ROOT/SHA256SUMS.txt"

read_env() {
    local key="$1"
    local value

    value="$(grep -E "^${key}=" "$ROOT_DIR/.env" | sed -n '1p' | cut -d '=' -f 2- || true)"
    value="${value%\"}"
    value="${value#\"}"

    printf '%s' "$value"
}

require_command() {
    if ! command -v "$1" >/dev/null 2>&1; then
        printf 'Falta el comando requerido: %s\n' "$1" >&2
        exit 1
    fi
}

require_command zip
require_command shasum
require_command mysqldump

mkdir -p "$TEMP_DB_DIR"

DB_HOST="$(read_env DB_HOST)"
DB_PORT="$(read_env DB_PORT)"
DB_DATABASE="$(read_env DB_DATABASE)"
DB_USERNAME="$(read_env DB_USERNAME)"
DB_PASSWORD="$(read_env DB_PASSWORD)"

printf 'Creando respaldo en %s\n' "$BACKUP_ROOT"

printf '1/3 Empaquetando codigo limpio...\n'
(
    cd "$ROOT_DIR"
    zip -r -q "$CODE_ZIP" . \
        -x '.git/*' \
        -x 'node_modules/*' \
        -x 'vendor/*' \
        -x 'public/build/*' \
        -x 'public/hot' \
        -x 'bootstrap/cache/*.php' \
        -x 'storage/framework/*' \
        -x 'storage/logs/*' \
        -x 'storage/app/backups/*' \
        -x '*.sql' \
        -x '*.zip' \
        -x 'package-lock.json' \
        -x '.env' \
        -x '.env.local' \
        -x '.env.produccion' \
        -x '.DS_Store'
)

printf '2/3 Exportando base de datos...\n'
if [ -n "$DB_PASSWORD" ]; then
    MYSQL_PWD="$DB_PASSWORD" mysqldump \
        --host="$DB_HOST" \
        --port="$DB_PORT" \
        --user="$DB_USERNAME" \
        --single-transaction \
        --skip-lock-tables \
        "$DB_DATABASE" > "$DB_SQL"
else
    mysqldump \
        --host="$DB_HOST" \
        --port="$DB_PORT" \
        --user="$DB_USERNAME" \
        --single-transaction \
        --skip-lock-tables \
        "$DB_DATABASE" > "$DB_SQL"
fi

printf '3/3 Empaquetando dump y calculando hashes...\n'
(
    cd "$TEMP_DB_DIR"
    zip -r -q "$DB_ZIP" "$(basename "$DB_SQL")"
)

(
    cd "$BACKUP_ROOT"
    shasum -a 256 \
        "$(basename "$CODE_ZIP")" \
        "$(basename "$DB_ZIP")" > "$MANIFEST"
)

rm -rf "$TEMP_DB_DIR"

printf '\nRespaldo completado.\n'
printf 'Codigo: %s\n' "$CODE_ZIP"
printf 'DB:     %s\n' "$DB_ZIP"
printf 'Hashes: %s\n' "$MANIFEST"
