#!/bin/bash

#
# Copyright (C) 2012 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Generate all files we have templates for:
#   docs.html
#   ../src/camera_metadata_tag_info.c
#   ../src/camera_metadata_tags.h
#

if [[ -z $ANDROID_BUILD_TOP ]]; then
    echo "Please source build/envsetup.sh before running script" >& 2
    exit 1
fi

thisdir=$(cd "$(dirname "$0")"; pwd)
fwkdir="$ANDROID_BUILD_TOP/frameworks/base/core/java/android/hardware/camera2/"
out_files=()

function relpath() {
    python -c "import os.path; print os.path.relpath('$1', '$PWD')"
}

# Generates a file. Appends to $out_files array as a side effect.
function gen_file() {
    local in=$thisdir/$1
    local out=$thisdir/$2

    gen_file_abs "$in" "$out"
    return $?
}

function gen_file_abs() {
    local in="$1"
    local out="$2"

    python $thisdir/metadata_parser_xml.py $thisdir/metadata_properties.xml $in $out

    local succ=$?

    if [[ $succ -eq 0 ]]
    then
        echo "OK: Generated $(relpath "$out")"
        out_files+=$'\n'" $out"
    else
        echo "FAIL: Errors while generating $(relpath "$out")" >& 2
    fi

    return $succ
}

# Print a list of git repository paths which were affected after file generation
function affected_git_directories() {
    local input_files=($@)
    local git_directories=()

    for file in "${input_files[@]}"; do
        local dir_path="$(dirname "$file")"
        echo "Trying to cd into $dir_path" >& /dev/null
        # Absolute path to the git repository root of that file
        local git_path="$(cd "$dir_path";
                          git rev-parse --show-toplevel 2> /dev/null)"
        if [[ $? -eq 0 ]]; then
            # Both staged and unstaged changes
            local diff_result="$(cd "$dir_path";
                                 git status --porcelain | egrep -c -v '^[?][?]')"
            echo "Diff result was $diff_result" >& /dev/null
            echo "Diff result was $diff_result" >& /dev/null
            if [[ $diff_result -eq 0 ]]; then
                echo "No changes in ${git_path}" >& /dev/null
            else
                echo "There are changes in ${git_path}" >& /dev/null
                git_directories+=("$git_path")
            fi
        fi
    done

    # print as result the unique list of git directories affected
    printf %s\\n "${git_directories[@]}" | sort | uniq
}

$thisdir/metadata-check-dependencies || exit 1
$thisdir/metadata-validate $thisdir/metadata_properties.xml || exit 1
$thisdir/metadata-parser-sanity-check || exit 1
gen_file html.mako docs.html || exit 1
gen_file camera_metadata_tag_info.mako ../src/camera_metadata_tag_info.c || exit 1
gen_file camera_metadata_tags.mako ../include/system/camera_metadata_tags.h || exit 1
gen_file_abs CaptureResultKeys.mako "$fwkdir/CaptureResultKeys.java" || exit 1
gen_file_abs CaptureRequestKeys.mako "$fwkdir/CaptureRequestKeys.java" || exit 1
gen_file_abs CameraPropertiesKeys.mako "$fwkdir/CameraPropertiesKeys.java" || exit 1

echo ""
echo "===================================================="
echo "Successfully generated all metadata source files"
echo "===================================================="
echo ""

echo "****************************************************"
echo "The following git repositories need to be committed:"
echo "****************************************************"
echo ""
affected_git_directories "${out_files[@]}"
echo ""

exit 0
