#!/usr/bin/env bash # Reassembles base64 chunks and extracts the c4 deployment tarball. # Expects chunk files in the current directory. set -euo pipefail cd "$HOME" echo "=== C4 Deployment Reassembler ===" echo "Working directory: $HOME" echo "" #============================================================================== # Find and validate chunk files #============================================================================== echo "--- Finding chunk files ---" CHUNKS=($(ls -1 c4-deploy-*_b64_part*.txt 2>/dev/null | sort)) if [[ ${#CHUNKS[@]} -eq 0 ]]; then echo "ERROR: No chunk files found matching c4-deploy-*_b64_part*.txt" exit 1 fi echo "Found ${#CHUNKS[@]} chunks:" for chunk in "${CHUNKS[@]}"; do echo " - $chunk ($(du -h "$chunk" | cut -f1))" done #============================================================================== # Reassemble and decode #============================================================================== echo "" echo "--- Reassembling chunks ---" TIMESTAMP=$(echo "${CHUNKS[0]}" | sed -E 's/c4-deploy-([0-9]+-[0-9]+)_b64_part.*/\1/') TARBALL="c4-deploy-${TIMESTAMP}.tar.gz" COMBINED="combined_b64.txt" echo "Concatenating chunks..." cat "${CHUNKS[@]}" > "$COMBINED" echo " -> Created $COMBINED ($(du -h "$COMBINED" | cut -f1))" echo "Decoding base64..." base64 -d "$COMBINED" > "$TARBALL" echo " -> Created $TARBALL ($(du -h "$TARBALL" | cut -f1))" echo "Verifying tarball..." if tar -tzf "$TARBALL" > /dev/null 2>&1; then echo " -> Tarball is valid" else echo "ERROR: Tarball verification failed" exit 1 fi #============================================================================== # Archive existing source #============================================================================== echo "" echo "--- Archiving existing source ---" if [[ -d c4 ]]; then rm -rf c4.bak mv c4 c4.bak echo " -> Moved c4 -> c4.bak" else echo " -> No existing c4 directory" fi #============================================================================== # Extract #============================================================================== echo "" echo "--- Extracting tarball ---" tar -xzf "$TARBALL" echo " -> Extracted to ~/c4" #============================================================================== # Cleanup #============================================================================== echo "" echo "--- Cleaning up ---" rm -f "$COMBINED" "$TARBALL" "${CHUNKS[@]}" echo " -> Removed chunks, combined base64, and tarball" #============================================================================== # Next steps #============================================================================== echo "" echo "=== Reassembly Complete ===" echo "" echo "Next steps:" echo " cd ~/c4" echo " sudo ./deploy/setup.sh # first time only" echo " sudo ./deploy/deploy.sh"