#!/bin/bash
# Auto-generate version.h from Git commit info (matches CMake behavior)
# Output: mpp/version.h with MPP_VERSION / MPP_VER_HIST_CNT / MPP_VER_HIST_X

# -------------------------- Configuration --------------------------
# Max git history entries (match your CMake VERSION_MAX_CNT)
VERSION_MAX_CNT=10
# Project root path
PROJECT_SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Output header path
VERSION_H="${1}/version.h"
# Git hooks path
GIT_HOOK_SRC="${PROJECT_SOURCE_DIR}/tools/hooks/"
GIT_HOOK_DST="${PROJECT_SOURCE_DIR}/.git/hooks/"

# Initialize variables
VERSION_CNT=0
MPP_VERSION="\"unknown\""

# -------------------------- Check Git & Get Info --------------------------
if [ -d "${PROJECT_SOURCE_DIR}/.git" ] && command -v git &>/dev/null; then
    echo "STATUS: Found Git, collecting version information..."

    # Get latest commit info
    GIT_LOG_FORMAT="%h author: %<|(30)%an %cd %s"
    VERSION_INFO_RAW=$(git -C "${PROJECT_SOURCE_DIR}" log -1 --date=short --pretty=format:"${GIT_LOG_FORMAT}" 2>/dev/null)
    if [ -n "${VERSION_INFO_RAW}" ]; then
        echo "STATUS: current version:"
        echo "STATUS: ${VERSION_INFO_RAW}"
        VERSION_INFO_ESC=$(echo "${VERSION_INFO_RAW}" | sed 's/"/\\"/g')
        MPP_VERSION="\"${VERSION_INFO_ESC}\""
    fi

    # Get git history
    echo "STATUS: git version history:"
    GIT_LOG_HIST="%h author: %<|(30)%an %cd %s %d"
    unset HIST_LINES
    for ((i=0; i<VERSION_MAX_CNT; i++)); do
        LOG=$(git -C "${PROJECT_SOURCE_DIR}" log HEAD~$i -1 --date=short --pretty=format:"${GIT_LOG_HIST}" 2>/dev/null)
        if [ -n "${LOG}" ]; then
            LOG_ESC=$(echo "${LOG}" | sed 's/"/\\"/g')
            HIST_LINES[$i]="\"${LOG_ESC}\""
            echo "STATUS: ${LOG}"
            VERSION_CNT=$((VERSION_CNT + 1))
        else
            HIST_LINES[$i]="\"\""
        fi
    done
    echo "STATUS: total ${VERSION_CNT} git version recorded"

    # Install git hooks
    if [ -d "$GIT_HOOK_SRC" ] && [ -d "$GIT_HOOK_DST" ]; then
        cp -rf "${GIT_HOOK_SRC}"* "${GIT_HOOK_DST}"
        echo "STATUS: Install git hooks done"
    fi
fi

# -------------------------- Generate version.h --------------------------
cat > "${VERSION_H}" << EOF
/*
 * Copyright (C) 2026 Rockchip Electronics Co., Ltd.
 *
 * 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.
 */

#ifndef __VERSION_H__
#define __VERSION_H__

#define MPP_VERSION             ${MPP_VERSION}
#define MPP_VER_HIST_CNT        ${VERSION_CNT}
#define MPP_VER_HIST_0          ${HIST_LINES[0]}
#define MPP_VER_HIST_1          ${HIST_LINES[1]}
#define MPP_VER_HIST_2          ${HIST_LINES[2]}
#define MPP_VER_HIST_3          ${HIST_LINES[3]}
#define MPP_VER_HIST_4          ${HIST_LINES[4]}
#define MPP_VER_HIST_5          ${HIST_LINES[5]}
#define MPP_VER_HIST_6          ${HIST_LINES[6]}
#define MPP_VER_HIST_7          ${HIST_LINES[7]}
#define MPP_VER_HIST_8          ${HIST_LINES[8]}
#define MPP_VER_HIST_9          ${HIST_LINES[9]}

#endif /*__VERSION_H__*/
EOF

echo "STATUS: Successfully generated ${VERSION_H}"