#!/bin/bash

set -e

source "$(dirname "$(readlink -f "$0")")/.global"

mkdir -p "$patches_dex_dir"
rm -rf "$patches_dex_dir"/*

build_one() {

    local patch_name="$1"
    local fileset_dirs=( "${@:2}" )

    echo "********* BUILD: $patch_name"
    echo

    local patch_tmp_dir="$tmp_dir/$patch_name"
    mkdir "$patch_tmp_dir"

    local patch_dex_dirs=()
    for fileset_dir in "${fileset_dirs[@]}"; do

        local fileset_name="$(basename "$fileset_dir")"

        echo "****** build: $patch_name"
        echo "****** against: $fileset_name"
        echo

        local patch_source_dir="$patches_source_dir/$patch_name"
        local api_level="$(cut -d '-' -f 1 <<<"$fileset_name")"
        local patch_dex_dir="$patch_tmp_dir/${patch_name}__$fileset_name"
        patch_dex_dirs+=( "$patch_dex_dir" )

        "$base_dir/build-patch" "$patch_source_dir" "$api_level" "$fileset_dir" "$patch_dex_dir"

        echo

    done

    echo "********* PUBLISH: $patch_name"
    echo "sha256sum:  "  && sha256sum -b "$patch_dex_dir"/services.jar.dex | cut -d " " -f 1 
    echo "********* BUILT AGAINST: $(basename "${fileset_dirs[0]}")"

    cp -r "${patch_dex_dirs[0]}" "$patches_dex_dir/$patch_name"

    echo
    echo

}

apply_one() {

    local patch_name="$1"
    local fileset_dirs=( "${@:2}" )

    echo "********* APPLY: $patch_name"
    echo

    local patch_dex_dir="$patches_dex_dir/$patch_name"
    local fileset_tmp_dir="$tmp_dir/${patch_name}__fs"
    mkdir "$fileset_tmp_dir"

    #patched_fileset_dirs=()
    for fileset_dir in "${fileset_dirs[@]}"; do

        local fileset_name="$(basename "$fileset_dir")"

        echo "****** apply: $patch_name"
        echo "****** to: $fileset_name"
        echo

        local api_level="$(cut -d '-' -f 1 <<<"$fileset_name")"
        local patched_fileset_dir="$fileset_tmp_dir/${fileset_name}__$patch_name"
        #patched_fileset_dirs+=( "$patched_fileset_dir" )

        "$base_dir/patch-fileset" "$patch_dex_dir" "$api_level" "$fileset_dir" "$patched_fileset_dir"

        if [ -d "$fileset_dir/dedex" ]; then
            ln -s -t "$patched_fileset_dir" "$fileset_dir/dedex"
        fi

        if [ -d "$fileset_dir/dedex-auto" ]; then
            ln -s -t "$patched_fileset_dir" "$fileset_dir/dedex-auto"
        fi

        echo

    done

    echo

}

build_and_apply_one() {

    build_one "$@"
    apply_one "$@"

}

generate_patch_sources() {

    echo "********* GENERATE PATCH SOURCES"
    "$base_dir/patches-src-gen/generate-patch-sources"
    echo
    echo

}

build_all() {

    shopt -s nullglob

    if [[ "$1" == "--quick" || "$1" == "-q" ]]; then
        local quick=1
    else
        local quick=0
    fi

    tmp_dir="$patches_dex_dir/tmp-dir"
    mkdir "$tmp_dir"
    echo "*" >"$tmp_dir/.gitignore"

    # Android versions and API levels

    #    API   Version    Codename
    #    30    11.0       R
    #    29    10.0       Q
    #    28    9.0        Pie
    #    27    8.1        Oreo
    #    26    8.0        Oreo
    #    25    7.1        Nougat
    #    24    7.0        Nougat
    #    23    6.0        Marshmallow
    #    22    5.1        Lollipop
    #    21    5.0        Lollipop
    #   (20)   -          -
    #    19    4.4        KitKat
    #    18    4.3        Jelly Bean
    #    17    4.2        Jelly Bean
    #    16    4.1        Jelly Bean
    #    15    4.0.3-4    Ice Cream Sandwich
    #    14    4.0.1-2    Ice Cream Sandwich
    #   (13)   3.2        Honeycomb
    #   (12)   3.1        Honeycomb
    #   (11)   3.0        Honeycomb
    #    10    2.3.3-7    Gingerbread
    #    09    2.3.0-2    Gingerbread
    #    08    2.2        Froyo
    #    07    2.1        Eclair
    #    06    2.0.1      Eclair
    #    05    2.0.0      Eclair
    #    04    1.6        Donut
    #    03    1.5        Cupcake
    #    02    1.1        -
    #    01    1.0        -

    # Generate patch sources

    generate_patch_sources

    # Build the hook patches

    if (( quick )); then
        build_one           sigspoof-hook-11.0 "$filesets_dir"/30-*
                  apply_one sigspoof-hook-11.0 "$filesets_dir"/30-*
    else
        build_and_apply_one sigspoof-hook-11.0 "$filesets_dir"/30-*
    fi

    # Build the core patch

    if (( quick )); then
        apply_one           sigspoof-core "$tmp_dir"/sigspoof-hook-11.0__fs/*
    else
        build_one           sigspoof-core "$tmp_dir"/sigspoof-hook-11.0__fs/*
                  apply_one sigspoof-core "$tmp_dir"/sigspoof-hook-11.0__fs/*
    fi


    rm -rf "$tmp_dir"

}

build_all "$@" 2>&1 | tee "$patches_dex_dir/bulk-patch-builder.log"
