#!/usr/bin/env zsh

output_directory="CzechQuests"

wow_versions=(
  "Classic ERA"   # 1 - classic_era
  "Retail"        # 2 - retail
)

toc_files=(
  "CzechQuests_Vanilla.toc"
  "CzechQuests_Mainline.toc"
)


read_toc_version() {
  local wow_version=$1
  local toc_file=${toc_files[$wow_version]}
  local current_version=0
  if [[ -f "$toc_file" ]]; then
      current_version=$(grep "## Version:" "$toc_file" | cut -d ' ' -f 3)
  fi
  echo "$current_version"
}

write_toc_version() {
  local wow_version=$1
  local next_version=$2
  local toc_file=${toc_files[$wow_version]}
  if [[ -f "$toc_file" ]]; then
      sed -i '' "s/## Version: $current_version/## Version: $next_version/" "$toc_file"
  fi
}

make_zip() {
  local wow_version=$1
  local current_version=$2

  local zip_filename=""
  local files_to_zip=()

  # prepare output directory
  mkdir -p $output_directory
  mkdir -p "${output_directory}/Addon/Data/"

  # Includes classic era continents
  if [ "$wow_version" = "1" ]; then
    cp -r "./Addon/Data/classic_era/" "${output_directory}/Addon/Data/"
    cp "./CzechQuests_Vanilla.toc" "${output_directory}"
    zip_filename="czech-quests-classic_era-v$current_version.zip"
  fi

  # Includes retail continents
  if [ "$wow_version" = "2" ]; then
    cp -r "./Addon/Data/retail/" "${output_directory}/Addon/Data/"
    cp "./CzechQuests_Mainline.toc" "${output_directory}"
    zip_filename="czech-quests-retail-v$current_version.zip"
  fi

  # includes frames
  while IFS= read -r line; do
      files_to_zip+=("$line")
  done < <(find . -name "*.lua" -path "./Addon/Frames/*")

  # includes code
  while IFS= read -r line; do
      files_to_zip+=("$line")
  done < <(find . -name "*.lua" -path "./Addon/Code/*")

  # includes assets
  while IFS= read -r line; do
      files_to_zip+=("$line")
  done < <(find . -name "*" -path "./Assets/*")

  # includes other files
  files_to_zip+=("./Addon/Data/other.lua" "./Addon/Addon.lua" "./CzechQuests.lua")

  # prepare output directory
  for file in "${files_to_zip[@]}"; do
    rsync -R "$file" "$output_directory"
  done

  # Remove invalid files
  find "$output_directory" -name ".DS_Store" -type f -delete

  # create zip file
  zip -r "$zip_filename" $output_directory

  # remove output directory
  rm -r $output_directory
}

release_wow() {
  local wow_version=$1

  # Read current version from file for wow version
  current_version=$(read_toc_version "$wow_version")

  # Increase version
  new_version="$current_version"
  vared -p "Choose version: " new_version

  # Write to file
  write_toc_version "$wow_version" "$new_version"

  # Generate zip
  make_zip $wow_version $new_version
}

init() {
  echo "Choose Wow:"
  select opt in "${wow_versions[@]}"
  do
      case $opt in
          "Classic ERA")
              echo "You chose Classic ERA"
              release_wow 1
              return 0
              ;;
          "Retail")
              echo "You chose Retail"
              release_wow 2
              return 0
              ;;
      esac
  done
}

init