#!/bin/bash

# usage
# ./unzip.sh --dirs <dir1> <dir2> ... (to unzip only within specified directories)
# ./unzip.sh --all-dirs (to unzip in all directories)
# ./unzip.sh --exclude-evals (to unzip in all directories except directories containing "evals")

PASSWORD="sadtimesforthesetimes"

# Function to unzip files
unzip_files() {
    local password=$1
    local exclude_evals=$2
    shift 2
    local directories=("$@")

    if [ ${#directories[@]} -eq 0 ]; then
        # Unzip in all directories if no specific directories are provided
        if [ "$exclude_evals" = true ]; then
            find . -type f -name '*.zip' ! -path '*/evals.zip' -exec bash -c 'ZIPFILE="$1"; UNZIPDIR="${ZIPFILE%.zip}"; unzip -P $2 -o "$ZIPFILE" -d "$UNZIPDIR"' _ {} $password \;
        else
            find . -type f -name '*.zip' -exec bash -c 'ZIPFILE="$1"; UNZIPDIR="${ZIPFILE%.zip}"; unzip -P $2 -o "$ZIPFILE" -d "$UNZIPDIR"' _ {} $password \;
        fi
    else
        # Unzip only within specified directories
        for dir in "${directories[@]}"; do
            if [ "$exclude_evals" = true ]; then
                find "$dir" -type f -name '*.zip' ! -path '*/evals.zip' -exec bash -c 'ZIPFILE="$1"; UNZIPDIR="${ZIPFILE%.zip}"; unzip -P $2 -o "$ZIPFILE" -d "$UNZIPDIR"' _ {} $password \;
            else
                find "$dir" -type f -name '*.zip' -exec bash -c 'ZIPFILE="$1"; UNZIPDIR="${ZIPFILE%.zip}"; unzip -P $2 -o "$ZIPFILE" -d "$UNZIPDIR"' _ {} $password \;
            fi
        done
    fi
}

# Check if no arguments were provided
if [ $# -eq 0 ]; then
    echo "Usage: $0 --dirs <dir1> <dir2> ... | --all-dirs | --exclude-evals"
    exit 1
fi

# Parse arguments
dirs=()
all_dirs=false
exclude_evals=false
while [[ $# -gt 0 ]]; do
    case $1 in
        --dirs)
            shift # Remove '--dirs' from arguments
            while [[ $# -gt 0 ]] && [[ $1 != -* ]]; do
                dirs+=("$1")
                shift # Remove directory from arguments
            done
            ;;
        --all-dirs)
            all_dirs=true
            shift
            ;;
        --exclude-evals)
            exclude_evals=true            
            shift
            ;;
        *) 
            # Unknown option
            echo "Unknown option: $1, Usage: $0 --dirs <dir1> <dir2> ... | --all-dirs | --exclude-evals"
            exit 1
            ;;
    esac
done

echo $all_dirs
echo $exclude_evals

# Call function to unzip files
if [ "$all_dirs" = true ]; then
    unzip_files $PASSWORD $exclude_evals
else
    unzip_files $PASSWORD $exclude_evals "${dirs[@]}"
fi