#!/usr/bin/env bash
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

# add this pre-commit file into .git/hooks of your downloaded repo
# so that git automatically updates the lastUpdate field in the droneModels.json
# prior to each commit

if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=$(git hash-object -t tree /dev/null)
fi

# Redirect output to stderr.
exec 1>&2

# Get the absolute path to the root of the Git repository
REPO_ROOT=$(git rev-parse --show-toplevel)

# Define the path to the file relative to the root of the repository
FILE="$REPO_ROOT/droneModels.json"

# Define the current date format
CURRENT_DATE=$(date)

# Determine OS type
OS_TYPE=$(uname)

# Use sed to replace the placeholder with the current date
# Adjust the sed command based on the operating system
if [[ "$OS_TYPE" == "Darwin" ]]; then
    # macOS (BSD sed)
    /usr/bin/sed -i '' "s|\"lastUpdate\": \".*\"|\"lastUpdate\": \"$CURRENT_DATE\"|" "$FILE"
else
    # Linux (GNU sed)
    /usr/bin/sed -i "s|\"lastUpdate\": \".*\"|\"lastUpdate\": \"$CURRENT_DATE\"|" "$FILE"
fi

# Add the file to the commit
git add $FILE

# End the script
exit 0
