#!/usr/bin/env bash if [ -z "${BASH_VERSION:-}" ]; then exec bash "$0" "$@" fi set -euo pipefail REPOSITORY_URL="ssh://git@git.hdrdevs.com.ar:2222/horacio/turnosxpress.git" DEFAULT_BRANCH="main" DEFAULT_TARGET_DIR="$HOME/PRODUCCION/turnosxpress" if [[ -t 1 ]]; then BLUE="\033[0;34m" GREEN="\033[0;32m" YELLOW="\033[1;33m" CYAN="\033[0;36m" RED="\033[0;31m" RESET="\033[0m" else BLUE="" GREEN="" YELLOW="" CYAN="" RED="" RESET="" fi step() { echo -e "${BLUE}▶ $1${RESET}" } success() { echo -e "${GREEN}✅ $1${RESET}" } skip() { echo -e "${YELLOW}⏭️ $1${RESET}" } info() { echo -e "${CYAN}ℹ️ $1${RESET}" } warn() { echo -e "${RED}⚠️ $1${RESET}" } DIRECTORIES_TO_REMOVE=( ".atl" ".codegraph" ".opencode" "backupper" "bot-admin-api" "com.hdrdevs.turnosxpress" "tx-android" "file-manager" "mongo-db" "notification-sender" "openspec" "sysadmin" "sysadmin-cli" "txadmin" "txbot" "working-web" ) FILES_TO_REMOVE=( ".dockerignore" ".gitignore" "Dockerfile.alpine" "Dockerfile.debian" ) SERVER_DIRECTORIES_TO_REMOVE=( "node_modules" ) SERVER_FILES_TO_REMOVE=( ".env" ".env.android" ".env.dev" ".env.omv" ".env.txlocal" ) TXCLIENT_DIRECTORIES_TO_REMOVE=( ".next" "node_modules" ) TXCLIENT_FILES_TO_REMOVE=( ".env" ".env.android" ".env.dev" ".env.omv" ".env.txlocal" ) branch="${1:-$DEFAULT_BRANCH}" target_dir="${2:-$DEFAULT_TARGET_DIR}" timestamp="$(date +%Y%m%d-%H%M)" image_archive="turnosxpress-${timestamp}.tar" if [[ "$branch" == "-h" || "$branch" == "--help" ]]; then cat < .env" else skip "Server file not found: .env.prod" fi else warn "Server directory not found. Skipping server cleanup" fi txclient_dir="$target_dir/txclient" if [[ -d "$txclient_dir" ]]; then step "Removing excluded txclient directories and files" for directory in "${TXCLIENT_DIRECTORIES_TO_REMOVE[@]}"; do path="$txclient_dir/$directory" if [[ -d "$path" ]]; then rm -rf -- "$path" success "Removed txclient directory: $directory" else skip "Txclient directory not found: $directory" fi done for file in "${TXCLIENT_FILES_TO_REMOVE[@]}"; do path="$txclient_dir/$file" if [[ -f "$path" ]]; then rm -f -- "$path" success "Removed txclient file: $file" else skip "Txclient file not found: $file" fi done if [[ -f "$txclient_dir/.env.prod" ]]; then mv -- "$txclient_dir/.env.prod" "$txclient_dir/.env" success "Renamed txclient file: .env.prod -> .env" else skip "Txclient file not found: .env.prod" fi else warn "Txclient directory not found. Skipping txclient cleanup" fi step "Building Docker image" ( cd "$target_dir" info "Stopping container: turnosxpress" docker stop turnosxpress || true info "Removing container: turnosxpress" docker rm turnosxpress || true info "Removing image: turnosxpress:latest" docker rmi turnosxpress:latest || true info "Building image: turnosxpress:latest" docker build -t turnosxpress . ) success "Docker image built" step "Exporting Docker image" ( cd "$HOME/PRODUCCION" info "Saving image to: $image_archive" docker save turnosxpress:latest -o "$image_archive" info "Compressing image: ${image_archive}.gz" gzip -f "$image_archive" ) success "Docker image exported to '$HOME/PRODUCCION/${image_archive}.gz'" success "Done. Repository is ready at '$target_dir' on branch '$branch'"