#! /bin/bash
set +e

config_folder="$HOME/.config/deepin/dde-dock"
config_file=$config_folder/dde-dock.ini
dde_dock_entity="dde-dock"
args=$@

# If the file doesn't exist, it means this is the first startup required for login or restart
function check_tmp_file() {
    tmp_file="/tmp/_#_dde-dock_safemode"
    if [ ! -e "$tmp_file" ]; then
        rm -f $config_file
        touch $tmp_file
    fi
}

# Ensure the data folder exists.
function create_data_folder() {
    if [ ! -d "$config_folder" ]; then
        mkdir -p "$config_folder"
        echo "mkdir .config folder success！"
    fi
}

# clear_safemode_record.bash will be executed by startdde when logout
function try_to_create_clear_file() {
    clear_record_file="$HOME/.config/autostop/clear_safemode_record.bash"

    if [ ! -e "$clear_record_file" ]; then
        echo "#! /bin/bash" > $clear_record_file
        echo "rm -f ~/.config/deepin/dde-dock/dde-dock.ini" >> $clear_record_file
        chmod +x $clear_record_file
    fi
}

# Record the abnormal exit status and time. If it exits normally, clear the data.
function record() {
    local exit_status=$1
    if [ $exit_status -eq 0 ]; then
        rm -f $config_file
        return
    fi

    # Record abnormal exit time.
    local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
    echo $timestamp >> "$config_file"

    # Remove invalid record
    line_num=$(wc -l < "$config_file")
    if (( line_num >= 3 )); then
        lines_to_display=$((line_num - 1))
        # Read the first (lines_to_display) lines of the file and output their content
        head -n "$lines_to_display" "$config_file" | while IFS= read -r line; do
        local line_timestamp=$line
            local line_time=$(date -d "$line_timestamp" +%s)

        local current_timestamp=$(date +"%Y-%m-%d %H:%M:%S")
            local current_time=$(date -d "$current_timestamp" +%s)

        local time_diff=$((current_time - line_time))
        local threshold=540

        # If the recorded time exceeds 9 minutes (9 * 60s), discard it.
        if [ $time_diff -gt $threshold ]; then
            grep -v "$line" "$config_file" > "$config_file.tmp" && mv "$config_file.tmp" "$config_file"
        fi
        done
    fi
}

# Parse the configuration file and run the executable binary file of dde-dock.
function launch() {
    if [ -e $config_file ]; then
        line_count=$(wc -l < "$config_file")
        # Enter safe mode if the file content exceeds three lines (three consecutive abnormal exits).
        if (( line_count >= 3 )); then
            # reset config and into safe mode
            rm -f $config_file
            $dde_dock_entity -x
        else
            $dde_dock_entity $args
        fi
    else
        $dde_dock_entity $args
    fi
}

# Main
check_tmp_file

create_data_folder

try_to_create_clear_file

launch

# Record process abnormal exit situations.
exit_status=$?
if [ $exit_status -eq 0 ] || [ $exit_status -eq 1 ];then
    echo "Normal exit, exit code:" $exit_status
else
    echo "Oops, unknown situation, exit code:" $exit_status
fi

record $exit_status

set -e
