#
#     Treble Info
#     Copyright (C) 2021-2023 Hackintosh Five
#
#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

# # Debugging
# 
# ## No authentication parameters were specified. These must be provided in order to authenticate with Google
# 
# Set SUPPLY_JSON_KEY to the path to the key
# 
# ## 500 Internal Server Error
# 
# Ensure the tag is pushed to GitLab and create the release manually (retry isn't possible)
#

default_platform(:android)

platform :android do
  desc "Build debug and test APK for screenshots"
  lane :build_and_screengrab do
    gradle(task: "assembleDebug assembleAndroidTest")
    capture_android_screenshots()
  end

  desc "Import translations and icons and generate screenshots if release-ready"
  lane :pre_release do
    gradle(task: "importTranslations importTranslationsForFastlane updateDrawables lintReportFreeDebug lintReportFreeRelease lintReportNonfreeDebug lintReportNonfreeRelease test")
    sh('! grep -qv "^No issues found.$" ../app/build/reports/lint-results-*.txt || ( cat ../app/build/reports/lint-results-*.txt; exit 1; )')
    build_and_screengrab()
  end

  desc "Deploy a new version"
  lane :release do |options|
    gradle(task: "clean assembleRelease")
    upload_to_play(options)
    release_to_gitlab(options)
  end

  def remove_empty_directories(base)
    return unless Dir.exist?(base)
    if Dir.empty?(base)
      Dir.unlink(base)
    else
      Dir.each_child(base) { |child| remove_empty_directories(File.join(base, child)) }
    end
  end

  lane :upload_to_play do |options|
    remove_empty_directories(Dir.new("metadata/android"))
    upload_to_play_store(track: options[:production] ? "production" : "beta",
                         release_status: "draft",
                         apk: "app/build/outputs/apk/nonfree/release/app-nonfree-release.apk",
                         skip_upload_images: true,
                         mapping: "app/build/outputs/mapping/nonfreeRelease/mapping.txt")
  end

  lane :release_to_gitlab do |options|
    upload_to_gitlab(version_name: options[:version] ? options[:version] : gradle(task: "versionName", flags: "--quiet", print_command: false, print_command_output: false).strip,
                     project_id: 30453147,
                     asset_paths: {
                                   "TrebleInfo-free.apk": "app/build/outputs/apk/free/release/app-free-release.apk",
                                   "TrebleInfo-free.apk.mapping.txt": "app/build/outputs/mapping/freeRelease/mapping.txt",
                                   "TrebleInfo-nonfree.apk": "app/build/outputs/apk/nonfree/release/app-nonfree-release.apk",
                                   "TrebleInfo-nonfree.apk.mapping.txt": "app/build/outputs/mapping/nonfreeRelease/mapping.txt",
                     },
                     token: File.read("../gitlab_token.txt").strip)
  end
end
