#!/bin/bash # Script to pack CP02S resources directory containing only .bin files # Creates tar.gz with resources as top-level directory SOURCE_DIR="/Users/ching/develop/IonBridge/files/CP02S/littlefs/resources" OUTPUT_FILE="cp02s_resources.tar.gz" # Check if source directory exists if [ ! -d "$SOURCE_DIR" ]; then echo "Error: Source directory does not exist: $SOURCE_DIR" exit 1 fi # Create temporary directory structure TEMP_DIR=$(mktemp -d) TEMP_RESOURCES="$TEMP_DIR/resources" # Create resources directory in temp mkdir -p "$TEMP_RESOURCES" # Copy only .bin files preserving directory structure bin_count=0 find "$SOURCE_DIR" -name "*.bin" -type f | while read -r file; do # Get relative path from source directory relative_path="${file#$SOURCE_DIR/}" target_path="$TEMP_RESOURCES/$relative_path" # Create target directory if needed mkdir -p "$(dirname "$target_path")" # Copy the file cp "$file" "$target_path" ((bin_count++)) done # Check if any .bin files were found bin_files=$(find "$TEMP_RESOURCES" -name "*.bin" 2>/dev/null | wc -l) if [ "$bin_files" -eq 0 ]; then echo "Warning: No .bin files found in $SOURCE_DIR" rm -rf "$TEMP_DIR" exit 1 fi # Save current directory before changing ORIGINAL_DIR="$PWD" # Create tar.gz from temp directory cd "$TEMP_DIR" tar -czf "$OUTPUT_FILE" resources/ # Move the archive back to original directory mv "$OUTPUT_FILE" "$ORIGINAL_DIR/" # Return to original directory cd "$ORIGINAL_DIR" # Cleanup temp directory rm -rf "$TEMP_DIR" echo "Created $OUTPUT_FILE with resources/ containing $bin_files .bin files" # Upload to S3 echo "Uploading $OUTPUT_FILE to S3..." if aws --endpoint=https://s3.cn-southeast-1.ifanrprod.com --profile=iot s3 cp "$OUTPUT_FILE" s3://iot-private/batch_input/; then echo "Successfully uploaded $OUTPUT_FILE to S3" else echo "Error: Failed to upload to S3" exit 1 fi