#!/bin/bash

# This script initiates restoration requests for Glacier objects listed in a specified file,
# but only if they haven't been restored yet, do not have an ongoing restoration request,
# and are still in the Glacier storage class. The script processes these objects in parallel
# using xargs to speed up the execution.
# It requires the bucket name, file name, and the number of days for the restoration.

# Example usage:
# ./restore-glacier-objects.sh my-bucket object-list.txt 3
# (Initiates restoration for objects listed in 'object-list.txt' in 'my-bucket' for 3 days)

# Check the number of arguments
if [[ $# -ne 3 ]]; then
    echo "Usage: $0 <bucket_name> <file_name> <number_of_days>" 
    exit 1
fi

# Retrieve arguments
BUCKET="$1"
FILE="$2"
DAYS="$3"

# Check if the file exists
if [[ ! -f "$FILE" ]]; then
    echo "Error: The file '$FILE' does not exist." 
    exit 1
fi

# Function to process each object
process_object() {
    KEY="$1"
    STORAGE_CLASS=$(aws s3api head-object --bucket "$BUCKET" --key "$KEY" --query 'StorageClass' --output text)
    STATUS=$(aws s3api head-object --bucket "$BUCKET" --key "$KEY" --query 'Restore' --output text)

    if [[ "$STORAGE_CLASS" != "GLACIER" ]]; then
        echo "Skipping $KEY: Already in $STORAGE_CLASS storage class."
    elif [[ "$STATUS" == *"ongoing-request=\"true\""* ]]; then
        # Skip if there's an ongoing restoration
        :
    else
        echo "Initiating restoration for: $KEY"
        aws s3api restore-object --restore-request Days=$DAYS --bucket "$BUCKET" --key "$KEY"
    fi
}

export -f process_object
export BUCKET
export DAYS

# Use xargs to run process_object in parallel for each line in the file
cat "$FILE" | xargs -P 4 -I {} bash -c 'process_object "$@"' _ {}
