#!/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
#

## Platform
# build/make
repopickchain 497048 # Enable PRODUCT_ENABLE_UFFD_GC by default

# frameworks/base
repopickchain 496008 # SystemUI: Fix battery icon not hiding from the status bar

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

# packages/apps/Launcher3
repopickchain 496882 # Launcher3: Don't double-apply top inset to All Apps search bar

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

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

## HAX
# lineage-sdk
repopick 494441 # sdk: Add org.lineageos.lib.phone to bootclass path

# packages/services/Telecomm
repopickchain 494440 # CallLogManager: Reflect SensitivePhoneNumbers

## backuptool
# system/update_engine
repopickchain 490928 # update_engine: Do not attempt to run backuptool in recovery

## legacy-telephony (gem)
# frameworks/opt/telephony
repopickchain 490678 # EuiccCardController: Avoid NPE for legacy euicc devices

wait
