#!/bin/bash

set -- /etc/debian_version "[00:00:00:000][01-01-2024]" "[23:59:59:999][31-12-2024]"

# Check if correct number of arguments are passed
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <log_file_name> <start_timestamp> <end_timestamp>"
    echo "Timestamps should be in the format '[HH:MM:SS:SSS][DD-MM-YYYY]'"
    exit 1
fi

log_file=$1
start_timestamp=$2
end_timestamp=$3

# Check if log file exists
if [ ! -f "$log_file" ]; then
    echo "File not found: $log_file"
    exit 1
fi

awk -v start="$start_timestamp" -v end="$end_timestamp" '
  function parsedate(date) {
        split(date, a, /[]:[-]+/)
        return a[8] "-" a[7] "-" a[6] "T" a[2] ":" a[3] ":" a[4] "." a[5]
  }
  BEGIN {
    st = parsedate(start)
    et = parsedate(end)
  }
  ((p = parsedate($0)) >= st) && (p <= et)' <<\:
[01:01:01:001][31-12-2023] First log line
[02:02:02:002][01-01-2024] Second log line
[03:03:03:003][31-12-2024] Third log line
[04:04:04:004][01-01-2025] Fourth log line
:
