#!/bin/bash

# usage
# ./zip.sh -dirs <dir1> <dir2> ... (to zip only within specified directories)
# ./zip.sh --all-dirs (to zip in all directories)

PASSWORD="sadtimesforthesetimes"

## Function to zip files and folders
#zip_files_and_folders() {
#    local password=$1
#    shift
#    local directories=("$@")
#
#    if [ ${#directories[@]} -eq 0 ]; then
#        # Zip in all directories if no specific directories are provided
#        find . -type d \( -name 'raw_*' -o -name 'private_*' -o -name 'batch' -o -name 'evalinfo' -o -name 'evalresult' -o -name 'evals' -o -name 'evalspecs' -o -name 'logs' -o -name 'exportextensions' -o -name 'exportspecs' -o -name 'exporttemplates' -o -name 'pile' -o -name 'pilepreviews' -o -name 'prompttemplates' \) -exec bash -c 'cd "$1"; zip -r --password $2 "../$(basename "$1").zip" .' _ {} $password \;
#    else
#        # Zip only within specified directories
#        for dir in "${directories[@]}"; do
#            find "$dir" -type d \( -name 'raw_*' -o -name 'private_*' -o -name 'batch' -o -name 'evalinfo' -o -name 'evalresult' -o -name 'evals' -o -name 'evalspecs' -o -name 'logs' -o -name 'exportextensions' -o -name 'exportspecs' -o -name 'exporttemplates' -o -name 'pile' -o -name 'pilepreviews' -o -name 'prompttemplates' \) -exec bash -c 'cd "$1"; zip -r --password $2 "../$(basename "$1").zip" .' _ {} $password \;
#        done
#    fi
#}

# Function to zip files and folders
zip_files_and_folders() {
    local password="$1"
    shift
    local directories=("$@")

    if [ "${#directories[@]}" -eq 0 ]; then
        find . -type d \( -name 'raw_*' -o -name 'private_*' -o -name 'evals' -o -name 'structs' \) -exec bash -c 'cd "$1"; zip -r --password "$2" "../$(basename "$1").zip" .' _ {} "$password" \;
    else
        for dir in "${directories[@]}"; do
            find "$dir" -type d \( -name 'raw_*' -o -name 'private_*' -o -name 'evals' -o -name 'structs' \) -exec bash -c 'cd "$1"; zip -r --password "$2" "../$(basename "$1").zip" .' _ {} "$password" \;
        done
    fi
}


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

# Parse arguments
dirs=()
all_dirs=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
            break
            ;;
        *) 
            # Unknown option
            echo "Unknown option: $1"
            exit 1
            ;;
    esac
done

# Call the function
if [ "$all_dirs" = true ]; then
    zip_files_and_folders "$PASSWORD"
else
    zip_files_and_folders "$PASSWORD" "${dirs[@]}"
fi
