#!/bin/bash

ANDROID_BUILD_TOP="$(dirname $(realpath "$0"))"
CHECK="$(echo $1 | grep -q check && echo 1)"
GERRIT_USERNAME="$(git config review.review.lineageos.org.username)"
GERRIT_SSH="ssh://$GERRIT_USERNAME@review.lineageos.org:29418"

function repopick() {
    if [[ "$CHECK" = "1" ]]; then
        echo -n "${@: -1} - " ; ssh $GERRIT_SSH gerrit query ${@: -1} --format=JSON | jq --slurp -r '.[0].status'
    else
        ${ANDROID_BUILD_TOP}/lineage/scripts/repopick/repopick.py -g $GERRIT_SSH $@ &
    fi
}

function repopickchain() {
    if [[ "$CHECK" = "1" ]]; then
        echo -n "$1 - " ; ssh $GERRIT_SSH gerrit query $1 --format=JSON | jq --slurp -r '.[0].status'
    else
        `${ANDROID_BUILD_TOP}/gen_chain $@`
    fi
}

function rc() {
    echo `${ANDROID_BUILD_TOP}/gen_chain $@ | cut -b10-`
}

#
# _examples_
#
# # repo/1
# changes=(
#     2137 # hello
#     `rc 2137` # hello (chain)
# )
# repopick ${changes[@]}
#
# # repo/2
# repopickchain 2137 # hello (chain)
#
# # repo/3
# repopick 2137 # hello
#

# android
repopickchain 491432 # manifest: Temporarily track lineage-23.2 branch for our repos

# bionic
repopick 490428 # libc: libstdc++: Introduce libstdc++_vendor

# bootable/deprecated-ota
repopickchain 490431 # updater: Support loading dynamic partition metadata from OTA

# bootable/recovery
repopickchain 494194 # recovery_ui: Adjust menu items style

# build/make
repopickchain 493887 # releasetools: skip disable_ublk on non-dynamic devices

# device/qcom/sepolicy
repopickchain 491886 # sepolicy: Move com.qualcomm.qti.poweroffalarm label to system_ext

# external/avb
repopickchain 491140 # DNM Revert "Add ANDROID_SECURE_STORAGE_CMD support in RSAPublicKey()"

# external/dng_sdk
repopickchain 490572 # dng_area_task: Provide backwards compatibility with legacy blobs

# external/libjxl
repopick 490598 # libjxl: Make it available to vendor

# external/setupcompat
repopickchain 490608 # Change some more defaults

# external/v4l2_codec2
repopickchain 491128 # Use the output pixel format requested by the decoder when available

# external/wpa_supplicant_8
repopick 490621 # wpa_supplicant: add support for bcmdhd SAE authentication offload

# frameworks/av
repopickchain 490648 # CAMX: CHI: Added support for handling jpeg debug data.

# frameworks/base
repopickchain 494272 # SystemUI: Allow to change brightness slider positioning in new compose QS

# frameworks/native
repopickchain 492289 # libui: Restore support for gralloc 2/3

# frameworks/opt/telephony
repopickchain 490678 # EuiccCardController: Avoid NPE for legacy euicc devices

# hardware/broadcom/libbt
repopickchain 490687 # libbt: Convert to Android.bp

# hardware/interfaces
repopickchain 492479 # compatibility_matrices: Add kernel_config_t_4.14

# hardware/libhardware
repopickchain 490712 # libhardware: Add new display types.

# hardware/qcom-caf/sm8750/audio/primary-hal
repopick 493305 # audio: primary-hal: Add support for acnMask

# hardware/qcom/wlan
repopickchain 490717 # legacy: Build wpa_supplicant.conf from make to soong

# lineage-sdk
repopick 492179 # sdk: Default to showing fastbootd in reboot menu

# packages/apps/Car/Settings
repopickchain 490729 # One does not simply become a Developer

# packages/apps/DocumentsUI
repopickchain 490738 # DocumentsUI: add icons for shortcuts

# packages/apps/EmergencyInfo
repopickchain 490740 # EmergencyInfo: Apply insets

# packages/apps/Launcher3
repopickchain 493818 # Launcher3: Kill OSE widget

# packages/apps/Settings
repopickchain 491435 # Require "tap to show" in SIM settings

# packages/apps/ThemePicker
repopickchain 490751 # ThemePicker: Remove Beta tag from themed icons toggle

# packages/apps/TvSettings
repopickchain 490777 # TvSettings: Support two button mute

# packages/apps/TvSystemUI
repopick 490778 # Implement USB debugging dialog

# packages/modules/Bluetooth
repopickchain 490791 # le_audio: Allow overriding the bit depth

# packages/modules/Connectivity
repopickchain 490802 # ConnectivityService: Disable "Close QUIC connection" feature

# packages/modules/Wifi
repopick 490810 # Wifi: Ingore miracast scan from connectivity manager

# packages/providers/DownloadProvider
repopickchain 490818 # DownloadProvider: Add support for manual pause/resume

# packages/providers/MediaProvider
repopickchain 490821 # Reject private paths with ignorable codepoints

# packages/services/DeviceAsWebcam
repopickchain 490834 # DeviceAsWebcam: jni: Make dwMaxPayloadTransferSize adjustable via property

# packages/services/Telecomm
repopickchain 491138 # DNM Revert "Telecom: Add sensitive phone numbers hooks"

# packages/services/Telephony
repopickchain 490845 # Telephony: Migrate RadioInfo from ScrollView to NestedScrollView

# system/chre
repopick 490848 # Restore MSM CHRE daemon support

# system/core
repopickchain 490876 # Partially Revert "usbd: Exit in case of charger mode."

# system/extras
repopick 490893 # Revert "lpmake: Remove --auto-slot-suffixing support."

# system/fs/fs_mgr
repopickchain 494093 # libsnapshot: Let dm-snapshot live

# system/media
repopick 490899 # audio: Allow opting out of speaker_layout_channel_mask field

# system/netd
repopickchain 490902 # VPN-covered DNS traffic may not fall through

# system/security
repopick 490903 # Handle key parameter conversion for FBE_ICE tag

# system/sepolicy
repopickchain 493666 # sepolicy: Resolve toolbox zram denials

# system/update_engine
repopickchain 490934 # Use ro.build.type in HardwareAndroid::AllowDowngrade()

# system/vold
repopickchain 490949 # vold: Add support for ISO9660/UDF CD-ROM

# vendor/qcom/opensource/audio-hal/st-hal-ar
repopickchain 493134 # Revert "st-hal: add support for forceRecognitionEvent"

# vendor/qcom/opensource/interfaces
repopickchain 493139 # Drop vendor.qti.hardware.qacs.ambientdatacapture
#repopickchain 493137 # ambientdatacapture: Fix vendor.qti.hardware.display.config import

wait
