1#!/bin/bash
2# shellcheck disable=SC2154
3
4# Constants
5declare -rx DUMP_MANAGER='xyz.openbmc_project.Dump.Manager'
6declare -rx DUMP_PATH='/xyz/openbmc_project/dump/opdump/entry'
7declare -rx DUMP_PROP='xyz.openbmc_project.Common.Progress'
8
9# Variables
10declare -x DRIVER=""
11declare -x YAML_FILE=""
12declare -x ADDL_DATA_PATH=""
13declare -x START_TIME=0
14declare -x END_TIME=0
15
16# Initialize variables
17function initialize_variables() {
18    DRIVER=$(awk -F '[=()]' '/VERSION_ID/ {print $2}' /etc/os-release)
19    YAML_FILE="$content_path/info.yaml"
20    ADDL_DATA_PATH="$content_path/plat_dump/additional_data/"
21}
22
23# Function to get the dump start and completed time
24function dump_time_details() {
25    START_TIME=$(busctl get-property "$DUMP_MANAGER" "$DUMP_PATH/$dump_id" \
26        "$DUMP_PROP" StartTime | awk '{print $2}')
27    END_TIME=$(busctl get-property "$DUMP_MANAGER" "$DUMP_PATH/$dump_id" \
28        "$DUMP_PROP" CompletedTime | awk '{print $2}')
29
30    if [ -z "$START_TIME" ] || [ "$START_TIME" -eq 0 ]; then
31        echo "Could not fetch start time for $dump_id. Setting manually"
32        START_TIME=$(date +%s)
33    fi
34
35    start=$(date -d @"$START_TIME" +'%Y-%m-%d %H:%M:%S')
36    if [ -z "$END_TIME" ] || [ "$END_TIME" -eq 0 ]; then
37        echo "Could not fetch end time for $dump_id. Setting manually"
38        END_TIME=$(date +%s)
39    fi
40    end=$(date -d @"$END_TIME" +'%Y-%m-%d %H:%M:%S')
41
42    printf "dump-start-time: %s\n" "$start" >> "$YAML_FILE"
43    printf "dump-end-time: %s\n" "$end" >> "$YAML_FILE"
44}
45
46# Function to fetch additional details
47function get_addl_data() {
48    if [ -d "$ADDL_DATA_PATH" ]; then
49        for entry in "$ADDL_DATA_PATH"/*; do
50            while IFS= read -r line; do
51                echo "$line" >> "$YAML_FILE"
52            done < "$entry"
53        done
54    fi
55}
56
57# Function to write data to info.yaml file
58function write_to_info_file() {
59    {
60        printf "%s\n" "# SPDX-License-Identifier: GPL-2.0"
61        printf "%s\n" "%YAML 1.2"
62        printf "%s\n\n" "---"
63        printf "generation: p10\n"
64        printf "driver: %s\n" "$DRIVER"
65    } >> "$YAML_FILE"
66    dump_time_details
67    get_addl_data
68}
69
70# Run main
71initialize_variables
72write_to_info_file
73
74