takserver/scripts/tak-extract.sh
Merith-TK d7a6b77e57
All checks were successful
Build Docker Image on Commit / build-and-publish (push) Successful in 2m53s
it builds, and hopefully runs?
2025-07-05 03:36:34 +01:00

193 lines
5.2 KiB
Bash

#!/bin/bash
# TAK Server Extraction Script
# Colors for logging
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log_info() {
echo -e "${GREEN}[EXTRACT-INFO]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_warn() {
echo -e "${YELLOW}[EXTRACT-WARN]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_error() {
echo -e "${RED}[EXTRACT-ERROR]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
log_debug() {
echo -e "${BLUE}[EXTRACT-DEBUG]${NC} $(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Extract TAK server from zip file
extract_tak_server() {
local zip_file="$1"
local target_dir="$2"
if [ -z "$zip_file" ] || [ -z "$target_dir" ]; then
log_error "Usage: extract_tak_server <zip_file> <target_dir>"
return 1
fi
if [ ! -f "$zip_file" ]; then
log_error "ZIP file does not exist: $zip_file"
return 1
fi
log_info "Extracting TAK server from: $(basename $zip_file)"
log_debug "Target directory: $target_dir"
# Create temporary extraction directory
local temp_dir="/tmp/tak_extract_$$"
mkdir -p "$temp_dir"
log_debug "Using temporary directory: $temp_dir"
# Extract ZIP file
cd "$temp_dir"
if ! unzip -q "$zip_file"; then
log_error "Failed to extract ZIP file"
rm -rf "$temp_dir"
return 1
fi
log_debug "ZIP extraction completed"
# Find the tak directory - it could be at root level or nested
local tak_source_dir=""
# First check if tak directory is at root level
if [ -d "$temp_dir/tak" ]; then
tak_source_dir="$temp_dir/tak"
log_info "Found 'tak' directory at root level"
else
# Look for tak directory in subdirectories
log_debug "Looking for 'tak' directory in subdirectories..."
tak_source_dir=$(find "$temp_dir" -type d -name "tak" -print -quit)
if [ -n "$tak_source_dir" ] && [ -d "$tak_source_dir" ]; then
log_info "Found 'tak' directory at: $tak_source_dir"
else
log_error "Expected 'tak' directory not found in ZIP file"
log_debug "Available directories:"
find "$temp_dir" -type d -maxdepth 3 | while read dir; do
log_debug " $dir"
done
rm -rf "$temp_dir"
return 1
fi
fi
# Ensure target directory exists
mkdir -p "$target_dir"
# Copy TAK files to target directory
log_debug "Copying files from $tak_source_dir/ to $target_dir/"
if cp -r "$tak_source_dir/"* "$target_dir/"; then
log_info "TAK files copied successfully"
else
log_error "Failed to copy TAK files"
rm -rf "$temp_dir"
return 1
fi
# Show what was extracted
log_debug "Files extracted to $target_dir:"
find "$target_dir" -maxdepth 2 -type f | head -10 | while read file; do
log_debug " $(basename $file)"
done
# Clean up
rm -rf "$temp_dir"
log_debug "Temporary directory cleaned up"
return 0
}
# Verify TAK installation
verify_tak_installation() {
local install_dir="$1"
log_info "Verifying TAK installation in: $install_dir"
# Check for required files
local required_files=(
"configureInDocker.sh"
"takserver.sh"
)
for file in "${required_files[@]}"; do
if [ -f "$install_dir/$file" ]; then
log_debug "Found required file: $file"
else
log_warn "Missing file: $file"
fi
done
# Check for key directories
local key_dirs=(
"bin"
"conf"
"lib"
)
for dir in "${key_dirs[@]}"; do
if [ -d "$install_dir/$dir" ]; then
log_debug "Found directory: $dir"
else
log_warn "Missing directory: $dir"
fi
done
# Main verification
if [ -f "$install_dir/configureInDocker.sh" ]; then
log_info "TAK installation verification successful"
return 0
else
log_error "TAK installation verification failed - configureInDocker.sh not found"
return 1
fi
}
# Set proper permissions
set_tak_permissions() {
local install_dir="$1"
local owner="${2:-tak:tak}"
log_info "Setting permissions on TAK installation"
# Make scripts executable
find "$install_dir" -name "*.sh" -exec chmod +x {} \; 2>/dev/null
# Set ownership
chown -R "$owner" "$install_dir" 2>/dev/null
log_debug "Permissions set successfully"
}
# Main execution if script is run directly
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
case "${1:-extract}" in
"extract")
extract_tak_server "$2" "$3"
;;
"verify")
verify_tak_installation "$2"
;;
"permissions")
set_tak_permissions "$2" "$3"
;;
*)
echo "Usage: $0 [extract|verify|permissions] <args>"
echo " extract <zip_file> <target_dir> - Extract TAK server from ZIP"
echo " verify <install_dir> - Verify TAK installation"
echo " permissions <install_dir> [owner] - Set TAK permissions"
exit 1
;;
esac
fi