#!/bin/bash ################################################################################ # Script: archive-skeleton.sh # Description: Recursively copies a directory tree, but for archive files (.zip), # creates a new archive in the destination with zero-byte files for # every file in the original archive. Non-archive files become zero-byte # files. Directory structure is preserved. Useful for fast testing. # Usage: ./archive-skeleton.sh [--dry-run] [--exclude ] [-v|--verbose] # # Dependencies: # - logger.sh for consistent and color-coded logging. # - unzip, zip # # Example: # ./archive-skeleton.sh --dry-run /src /dst ################################################################################ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${PROJECT_ROOT}/core/logger.sh" log "DEBUG" "Starting script: $(basename "$0")" usage() { log "INFO" "Usage: $0 [--dry-run] [--exclude ] [-v|--verbose] " exit 1 } DRY_RUN=false VERBOSE=false EXCLUDES=() # Parse options while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in --dry-run) DRY_RUN=true log "DEBUG" "DRY_RUN set to true" ;; --exclude) shift EXCLUDES+=("$1") log "DEBUG" "Added exclude pattern: $1" ;; -v|--verbose) VERBOSE=true log "DEBUG" "VERBOSE set to true" ;; --help) usage ;; *) usage ;; esac shift done if [ "$#" -ne 2 ]; then usage fi src="$1" dst="$2" if [ ! -e "$src" ]; then log "ERROR" "Source does not exist: $src" exit 1 fi should_exclude() { local path="$1" for pat in "${EXCLUDES[@]}"; do if [[ "$path" == $pat ]]; then return 0 fi if [[ "$(basename "$path")" == $pat ]]; then return 0 fi done return 1 } make_zero_zip() { local src_zip="$1" local dst_zip="$2" local tmpdir tmpdir=$(mktemp -d) unzip -qq -l "$src_zip" | awk 'NR>3 {if ($0!="" && $0!~/----/ && $0!~/files/) print $4}' | while read -r file; do [ -z "$file" ] && continue mkdir -p "$tmpdir/$(dirname "$file")" touch "$tmpdir/$file" done if [ "$DRY_RUN" = true ]; then log "INFO" "[DRY-RUN] Would create skeleton zip: $dst_zip" else (cd "$tmpdir" && zip -q -r "$dst_zip" .) log "DEBUG" "Created skeleton zip: $dst_zip" fi rm -rf "$tmpdir" if [ "$VERBOSE" = true ]; then echo "Skeleton zip: $dst_zip" fi } copy_skeleton() { local src_path="$1" local dst_root="$2" if should_exclude "$src_path"; then log "INFO" "Excluded: $src_path" return fi if [ -d "$src_path" ] && [ ! -L "$src_path" ]; then if [ "$DRY_RUN" = true ]; then log "INFO" "[DRY-RUN] Would create directory: $dst_root" else mkdir -p "$dst_root" fi if [ "$VERBOSE" = true ]; then echo "Directory: $dst_root" fi find "$src_path" -mindepth 1 -print0 | while IFS= read -r -d '' item; do rel_path="${item#$src/}" dst_item="$dst/$rel_path" copy_skeleton "$item" "$dst_item" done elif [ -f "$src_path" ]; then case "$src_path" in *.zip) make_zero_zip "$src_path" "$dst_root" ;; *) if [ "$DRY_RUN" = true ]; then log "INFO" "[DRY-RUN] Would create zero-byte file: $dst_root" else touch "$dst_root" fi if [ "$VERBOSE" = true ]; then echo "Zero-byte file: $dst_root" fi ;; esac elif [ -L "$src_path" ]; then target=$(readlink "$src_path") if [ "$DRY_RUN" = true ]; then log "INFO" "[DRY-RUN] Would symlink: $dst_root -> $target" else ln -s "$target" "$dst_root" fi if [ "$VERBOSE" = true ]; then echo "Symlink: $dst_root -> $target" fi else log "WARN" "Skipping unsupported file type: $src_path" fi } copy_skeleton "$src" "$dst" log "INFO" "Archive skeleton operation completed successfully." exit 0