#!/bin/bash

# Copyright (c) 2023-2024 Amin Bandali <[email protected]>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

set -eu

filename=
title=

usage()
{
   printf "Usage: $0 [filename] [title]\n"
}

usage()
{
   cat <<EOF
Usage: $0 [OPTIONS] [filename] [title]

The following OPTIONS are accepted:
-h, --help  print help and exit
EOF
 exit $1
}

main()
{
   temp=$(getopt -o 'h' --long 'help' -n "$0" -- "$@") || usage 1
   eval set -- "$temp"
   unset temp
   while true; do
       case "$1" in
           '-h'|'--help') usage 0 ;;
           '--') shift; break ;;
           *)
               printf "$0: internal error while parsing args\n" >&2
               usage 1
               ;;
       esac
   done
   read filename title <<<"$@"

   # Prompt for filename if it was not provided as an arg.
   while [ -z "$filename" ]; do
       read -erp "filename: " filename
   done

   # Exit with error if file already exists.
   if [ -e "$filename" ]; then
       printf "$0: error: file $filename already exists\n"
       exit 1
   fi

   # Check if we have a generate function for the file extension,
   # and drop out if not.
   ext="${filename##*.}"
   if ! type generate_"$ext" 2>/dev/null | \
           grep -q "generate_$ext is a function"; then
       printf "\
$0: error: no generate function for extension $ext
 please define a function named generate_$ext to handle
 creation of new $ext files\n"
       exit 1
   fi

   # Prompt for title if it was not provided as an arg.
   while [ -z "$title" ]; do
       read -erp "title: " title
   done

   # Run the generate function, which would normally generate the
   # output and write it to a new file with the given name.
   generate_"$ext"

   # Open the newly created file in editor.
   $EDITOR "$filename"
}

generate_txt()
{
   cat > "$filename" <<EOF
${title}
$(date -R)

TODO: write :-)

Take care, and so long for now.
EOF
}

generate_html()
{
   name="${filename##*/}"
   name_txt="${name%.${name##*.}}.txt"
   slashes="$(printf "$filename" | tr -cd '/')"
   depth="${#slashes}"
   up_path=
   if [ $depth -gt 0 ]; then
       up_path="$(printf "../%.0s" $(eval "echo {1..$depth}"))"
   fi
   cat > "$filename" <<EOF
<!DOCTYPE html><html lang="en"><head>
<title>$title - bandali</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="color-scheme" content="light dark">
<link rel="stylesheet" href="${up_path}bandali.css?v=20240301" type="text/css">
<link rel="alternate" href="$name_txt" title="plain text" type="text/plain">
</head><body>
<main>
<article>
<header><h1>$title</h1></header>
<p>$(date +'%a, %-d %b %Y')</p>

<p>TODO: write :-)</p>
</article>
</main>

<aside>
<div class="text-center">back to <a href="${up_path}home.html">home</a></div>
</aside>
</body></html>
EOF
}


main "$@"