#!/bin/sh
#
# logparse - caddy webserver JSON log parser
# dependencies: jq

show_help() {
  cat <<END
Usage: ${0##*/} [-c|--common] [-C|--combined] [-f|--files] [-h|--help] filename

Options:
  -c, --common     Apache Common Log Format (default)
  -C, --combined   Apache Combined Log Format
  -f, --files      Custom Log Format, focused on file transfers
  -h, --help       Show this help message and exit
END
}

# function to output logs in common log format
output_common_log_format() {
  jq --raw-output '
    . as $log |
    $log.request.client_ip as $h |
    "-" as $l |
    ($log.user_id // "-") as $u |
    ($log.ts | tostring | split(".") | .[1][:3]) as $ms |
    ($log.ts | floor | todateiso8601 | sub("T"; " ") | sub("Z"; "." + $ms + " +0000")) as $t |
    "\($log.request.method) \($log.request.uri) \($log.request.proto)" as $r |
    $log.status as $s |
    ($log.size // "-") as $b |
    "\($h) \($l) \($u)[\($t)] \"\($r)\" \($s) \($b)"
  ' "${1}"
}

# function to output logs in combined log format
output_combined_log_format() {
  jq --raw-output '
    . as $log |
    $log.request.client_ip as $h |
    "-" as $l |
    ($log.user_id // "-") as $u |
    ($log.ts | tostring | split(".") | .[1][:3]) as $ms |
    ($log.ts | floor | todateiso8601 | sub("T"; " ") | sub("Z"; "." + $ms + " +0000")) as $t |
    "\($log.request.method) \($log.request.uri) \($log.request.proto)" as $r |
    $log.status as $s |
    ($log.size // "-") as $b |
    ($log.request.headers.Referer[0] // "-") as $referrer |
    ($log.request.headers["User-Agent"][0] // "-") as $user_agent |
    "\($h) \($l) \($u)[\($t)] \"\($r)\" \($s) \($b) \"\($referrer)\" \"\($user_agent)\""
  ' "${1}"
}

output_file_log_format() {
  jq --raw-output '
    . as $log |
    $log.request.client_ip as $h |
    "-" as $l |
    ($log.user_id // "-") as $u |
    ($log.ts | tostring | split(".") | .[1][:3]) as $ms |
    ($log.ts | floor | todateiso8601 | sub("T"; " ") | sub("Z"; "." + $ms + " +0000")) as $t |
    "\($log.request.method) \($log.request.uri) \($log.request.proto)" as $r |
    $log.status as $s |
    ($log.size // "-") as $b |
    ($log.request.headers.Referer[0] // "-") as $referrer |
    ($log.request.headers["User-Agent"][0] // "-") as $user_agent |
    "\($h) \($l) \($u)[\($t)] \"\($r)\" \($s) \($b) \"\($referrer)\" \"\($user_agent)\""
  ' "${1}"
}

# function to list served files
list_served_files() {
  jq --raw-output '
    .request.client_ip as $client_ip |
    (.ts | floor | todateiso8601 | sub("T"; " ") | sub("Z"; " +0000")) as $datetime |
    .status as $status_code |
    (.size // "-") as $size |
    .request.uri as $filename |
    "\($client_ip) [\($datetime)] \($status_code) \($size) \"\($filename)\""
  ' "${1}"
}

# default to common log format
format="common"

# parse command line options
while [ "$#" -gt 0 ]; do
  case "$1" in
    -c | --common)
      format="common"
      shift
      ;;
    -C | --combined)
      format="combined"
      shift
      ;;
    -f | --files)
      format="files"
      shift
      ;;
    -h | --help)
      show_help
      exit 0
      ;;
    *)
      input="${1}"
      shift
      ;;
  esac
done

# check if a filename is provided
if [ -z "${input}" ]; then
  printf '%s\n' "Error: No input file provided."
  show_help
  exit 1
fi

# output the logs in the chosen format
if [ "${format}" = "common" ]; then
  output_common_log_format "${input}"
elif [ "${format}" = "files" ]; then
  list_served_files "${input}"
else
  output_combined_log_format "${input}"
fi

# apache common log format:
# "%h %l %u %t \"%r\" %>s %b"
#
# apache combined log format:
# "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
#
# %h is the remote host (client IP)
# %l is the remote logname (not used, so we'll use -)
# %u is the authenticated user
# %t is the time the request was received
# %r is the request line from the client ("method uri proto")
# %>s is the status code
# %b is the size of the object returned to the client
