#!/bin/bash

# Get S3 credentials and domain from environment variables
S3KEY="${S3_ACCESS_KEY}"
S3SECRET="${S3_SECRET_KEY}"
S3_DOMAIN="${S3_DOMAIN}"

# Check if environment variables are set
if [ -z "$S3KEY" ] || [ -z "$S3SECRET" ] || [ -z "$S3_DOMAIN" ]; then
  echo "Error: S3 configuration not found in environment variables"
  echo "Please set S3_ACCESS_KEY, S3_SECRET_KEY, and S3_DOMAIN environment variables"
  exit 1
fi

function putS3
{
  path=$1
  file=$2
  bucket="oap-releases"
  date=`date -R`
  content_type="application/x-compressed-tar"
  string="PUT\n\n$content_type\n$date\n/$bucket/$file"
  signature=$(echo -en "${string}" | openssl sha1 -hmac "${S3SECRET}" -binary | base64)
  url="https://$S3_DOMAIN/$bucket/$file"
  curl -X PUT -T "$path/$file" \
    -H "Host: $S3_DOMAIN" \
    -H "Date: $date" \
    -H "Content-Type: $content_type" \
    -H "Authorization: AWS ${S3KEY}:$signature" \
    "$url"
}

# Check if mode parameter is provided
if [ $# -eq 0 ]; then
  echo "Usage: $0 [electron|tauri]"
  echo "  electron: Upload files from ./release and ./output (if they exist)"
  echo "  tauri: Upload files from ./src-tauri/target/release/bundle"
  exit 1
fi

mode=$1

# Get script directory and set source path based on mode
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
project_root="$(dirname "$script_dir")"

if [ "$mode" = "electron" ]; then
  source_paths=()
  if [ -d "$project_root/release" ]; then
    source_paths+=("$project_root/release")
  fi
  if [ -d "$project_root/output" ]; then
    source_paths+=("$project_root/output")
  fi
  if [ ${#source_paths[@]} -eq 0 ]; then
    echo "Error: No valid source directories found for electron mode"
    echo "Expected at least one of: $project_root/release, $project_root/output"
    exit 1
  fi
elif [ "$mode" = "tauri" ]; then
  source_paths=()
  if [ -d "$project_root/src-tauri/target/release/bundle" ]; then
    source_paths+=("$project_root/src-tauri/target/release/bundle")
  fi
  if [ -d "$project_root/src-tauri/target/x86_64-apple-darwin/release/bundle" ]; then
    source_paths+=("$project_root/src-tauri/target/x86_64-apple-darwin/release/bundle")
  fi
  if [ -d "$project_root/src-tauri/target/aarch64-apple-darwin/release/bundle" ]; then
    source_paths+=("$project_root/src-tauri/target/aarch64-apple-darwin/release/bundle")
  fi
  if [ ${#source_paths[@]} -eq 0 ]; then
    echo "Error: No valid source directories found for tauri mode"
    echo "Expected at least one of: $project_root/src-tauri/target/release/bundle, $project_root/src-tauri/target/x86_64-apple-darwin/release/bundle"
    exit 1
  fi
else
  echo "Error: Invalid mode. Use 'electron' or 'tauri'"
  exit 1
fi

echo "Mode: $mode"
echo "Source paths: ${source_paths[@]}"
echo "Looking for files with extensions: .exe, .AppImage, .dmg, .sig"
echo "Also looking for files starting with 'latest' and ending with .yml in: ${source_paths[@]}"
echo "Looking for files starting with 'latest' and ending with .json in: $project_root"

# Find and upload files with specific extensions (search up to 3 levels deep)
found_files=0

# Search for exe, AppImage, dmg, sig, and latest yml files in source_paths
for src_path in "${source_paths[@]}"; do
  while IFS= read -r -d '' file; do
    if [ -f "$file" ]; then
      filename=$(basename "$file")
      file_dir=$(dirname "$file")
      # Check if file has one of the target extensions and starts with dive/Dive, or matches latest yml pattern
      if ([[ "$filename" == dive*.exe ]] || [[ "$filename" == Dive*.exe ]] \
          || [[ "$filename" == dive*.AppImage ]] || [[ "$filename" == Dive*.AppImage ]] \
          || [[ "$filename" == dive*.dmg ]] || [[ "$filename" == Dive*.dmg ]] \
          || [[ "$filename" == dive*.sig ]] || [[ "$filename" == Dive*.sig ]] \
          || [[ "$filename" == latest*.yml ]]); then
        echo "Uploading: $filename (from $file_dir)"
        putS3 "$file_dir" "$filename"
        found_files=$((found_files + 1))
      fi
    fi
  done < <(find "$src_path" -maxdepth 3 -type f -print0)
done

# Search for latest json files in project_root (2 levels deep)
while IFS= read -r -d '' file; do
  if [ -f "$file" ]; then
    filename=$(basename "$file")
    file_dir=$(dirname "$file")
    # Check if file matches latest json pattern
    if [[ "$filename" == latest*.json ]]; then
      echo "Uploading: $filename (from $file_dir)"
      putS3 "$file_dir" "$filename"
      found_files=$((found_files + 1))
    fi
  fi
done < <(find "$project_root" -maxdepth 2 -type f -print0)

if [ $found_files -eq 0 ]; then
  echo "No files with target extensions found in ${source_paths[@]}"
else
  echo "Uploaded $found_files files successfully"
fi