#! /bin/sh

# shell-script-helper-template v1.6
# Create a shell script stub using shell-script-helper
# Copyright (c) 2024 Raphaël Halimi <raphael.halimi@gmail.com>

# Source shell-script-helper
. /lib/shell-script-helper


#
# Variables
#

# Configuration files
SYSTEM_CONFIG_FILE="/etc/$(basename "$0").conf"
USER_CONFIG_FILE="$HOME/.$(basename "$0").conf"

# Options defaults
CRSS_FULLNAME="$USER"
CRSS_EMAIL="$USER@$(hostname -f)"


#
# Functions
#

print_usage () {
  printf "Usage: %s [OPTION]... NAME [DESCRIPTION]\n" "$(basename "$0")"
  printf "Create a shell script using shell-script-helper\n"
  printf "\nOPTIONS:\n"
  print_option "-f FULL_NAME" "Your full name"
  print_option "-e EMAIL_ADDRESS" "Your e-mail address"
  print_option "-v" "Verbose mode"
  print_option "-d" "Debug mode"
  print_option "-h" "Print this help message"
  printf "\nNAME:\n"
  printf "The name of the script you want to create.\n"
  printf "\nDESCRIPTION:\n"
  printf "A small description of your script.\n"
}


#
# Config files
#

[ -e "$SYSTEM_CONFIG_FILE" ] && . "$SYSTEM_CONFIG_FILE"
[ -e "$USER_CONFIG_FILE" ] && . "$USER_CONFIG_FILE"


#
# Options processing
#

while getopts "vdf:e:h" OPTION ; do
  case $OPTION in
    f) CRSS_FULLNAME="$OPTARG" ;;
    e) CRSS_EMAIL="$OPTARG" ;;
    v) enable_verbose ;;
    d) enable_debug ;;
    h) print_usage ; exit 0 ;;
    *) print_usage ; exit 1 ;;
  esac
done ; shift $((OPTIND-1))

SCRIPT_NAME="$1"
SCRIPT_DIRNAME="$(dirname "$SCRIPT_NAME")"
SCRIPT_BASENAME="$(basename "$SCRIPT_NAME")"
if printf "%s" "$SCRIPT_BASENAME" | grep -E -q "\s" ; then
  printf "Spaces not allowed in script name, replacing all spaces by hyphens\n"
  SCRIPT_BASENAME="$(printf "%s" "$(basename "$SCRIPT_NAME")" | tr " " "-")"
  SCRIPT_NAME="$SCRIPT_DIRNAME/$SCRIPT_BASENAME"
fi
SCRIPT_DESCRIPTION="${2:-"Script description"}"
CONFIG_FILE_BASENAME="$SCRIPT_BASENAME.conf"
SCRIPT_MANPAGE_NAME="$SCRIPT_NAME.1"
CONFIG_FILE_MANPAGE_NAME="$SCRIPT_NAME.conf.5"
print_debug "SCRIPT_NAME=$SCRIPT_NAME" "SCRIPT_BASENAME=$SCRIPT_BASENAME" "SCRIPT_DESCRIPTION=$SCRIPT_DESCRIPTION" "CONFIG_FILE_BASENAME=$CONFIG_FILE_BASENAME" "SCRIPT_MANPAGE_NAME=$SCRIPT_MANPAGE_NAME" "CONFIG_FILE_MANPAGE_NAME=$CONFIG_FILE_MANPAGE_NAME"


#
# Checks
#

[ $# -lt 1 ] && die "Please provide at least one argument"
[ $# -gt 2 ] && die "Please provide at most two arguments"
[ -e "$STUB_NAME" ] && die "File \"$STUB_NAME\" already exists"


#
# MAIN
#

#
# Script stub
#

printf "Creating shell script stub in %s\n" "$SCRIPT_NAME"
[ -e "$SCRIPT_NAME" ] && die "File \"$SCRIPT_NAME\" already exists"

cat << EOF > "$SCRIPT_NAME"
#! /bin/sh

# $SCRIPT_BASENAME v0.1
# $SCRIPT_DESCRIPTION
# Copyright (c) $(date +%Y) $CRSS_FULLNAME <$CRSS_EMAIL>

# Source shell-script-helper
. /lib/shell-script-helper


#
# Variables
#

# Configuration files
SYSTEM_CONFIG_FILE="/etc/\$(basename "\$0").conf"
USER_CONFIG_FILE="\$HOME/.\$(basename "\$0").conf"

# Options defaults
#VERBOSE=0
#DEBUG=0


#
# Functions
#

#on_exit () {
#  # Do some cleanup
#  print_verbose "Cleaning up"
#  delete_files \$TEMP_FILE \$OTHER_TEMP_FILE
#  # Do something else
#  ...
#}

print_usage () {
  printf "Usage: %s [OPTION]... [ARG]...\n" "\$(basename "\$0")"
  printf "$SCRIPT_DESCRIPTION\n"
  printf "\nOPTIONS:\n"
  print_option "-v" "Verbose mode"
  print_option "-d" "Debug mode"
  print_option "-h" "Print this help message"
  printf "\nARGS:\n"
  printf "Arguments description.\n"
}


#
# Configuration files
#

[ -e "\$SYSTEM_CONFIG_FILE" ] && . "\$SYSTEM_CONFIG_FILE"
[ -e "\$USER_CONFIG_FILE" ] && . "\$USER_CONFIG_FILE"


#
# Options processing
#

while getopts "vdh" OPTION ; do
  case \$OPTION in
    v) enable_verbose ;;
    d) enable_debug ;;
    h) print_usage ; exit 0 ;;
    *) print_usage ; exit 1 ;;
  esac
done ; shift \$((OPTIND-1))


#
# Checks
#

# root_only
# lock_script
#[ \$# -lt 1 ] && die "Please provide at least one argument"


#
# MAIN
#

# Do some things
EOF

print_debug "Adding executable bit"
chmod +x "$SCRIPT_NAME"


#
# Script manpage stub
#

printf "Creating manual page for script in %s\n" "$SCRIPT_MANPAGE_NAME"
[ -e "$SCRIPT_MANPAGE_NAME" ] && die "File \"$SCRIPT_MANPAGE_NAME\" already exists"
cat << EOF > "$SCRIPT_MANPAGE_NAME"
.\\" (C) Copyright $(date +%Y) $CRSS_FULLNAME <$CRSS_EMAIL>

.TH $(printf "%s" "$SCRIPT_BASENAME" | tr "[:lower:]" "[:upper:]") 1 "$(date --rfc-3339=date)"

.SH NAME
$SCRIPT_BASENAME - $(printf "%s" "$SCRIPT_DESCRIPTION" | tr "[:upper:]" "[:lower:]")

.SH SYNOPSIS
.B $SCRIPT_BASENAME
.RI [ option ]...
.RI [ arg ]...

.SH DESCRIPTION
\\fB$SCRIPT_BASENAME\\fR is a shell script that...

.SH OPTIONS
Options must be specified before arguments.
.TP
.BI -v
Enable verbose mode.
.TP
.BI -d
Enable debug mode.
.TP
.BI -h
Show summary of options.

.SH FILES
A system-wide \\fI/etc/$CONFIG_FILE_BASENAME\\fR and a per-user
\\fI~/.$CONFIG_FILE_BASENAME\\fR are parsed to set default variables. See the
\\fB$CONFIG_FILE_BASENAME\\fR(5) manpage for details.

.SH NOTES
Important: bla bla bla.

.SH EXAMPLE
Invoke \\fB$SCRIPT_BASENAME\\fR on file "/home/user/foo.txt" in verbose mode:
.PP
.nf
.RS
$SCRIPT_BASENAME -v "/home/user/foo.txt"
.RE
.fi
.PP
Invoke \\fB$SCRIPT_BASENAME\\fR on file "/home/user/bar.txt" in debug mode:
.PP
.nf
.RS
$SCRIPT_BASENAME -d "/home/user/bar.txt"
.RE
.fi

.SH SEE ALSO
.BR $CONFIG_FILE_BASENAME (5).
EOF


#
# Config file manpage stub
#

printf "Creating manual page for configuration file in %s\n" "$CONFIG_FILE_MANPAGE_NAME"
[ -e "$CONFIG_FILE_MANPAGE_NAME" ] && die "File \"$CONFIG_FILE_MANPAGE_NAME\" already exists"
cat << EOF > "$CONFIG_FILE_MANPAGE_NAME"
.\\" (C) Copyright $(date +%Y) $CRSS_FULLNAME <$CRSS_EMAIL>

.TH $(printf "%s" "$CONFIG_FILE_BASENAME" | tr "[:lower:]" "[:upper:]") 5 "$(date --rfc-3339=date)"

.SH NAME
$CONFIG_FILE_BASENAME - \\fB$SCRIPT_BASENAME\\fR configuration file

.SH SYNOPSIS
.TP
.B /etc/$CONFIG_FILE_BASENAME
system-wide
.TP
.B ~/.$CONFIG_FILE_BASENAME
per-user

.SH DESCRIPTION
The \\fI$CONFIG_FILE_BASENAME\\fR configuration files are sourced by the
\\fB$SCRIPT_BASENAME\\fR shell script to set some default variables.
.PP
The system-wide configuration file overrides the script defaults; the per-user
configuration file overrides the system-wide configuration file; and finally,
the command-line options override the per-user configuration file.

.SH VARIABLES
The following variables are used in the script:
.TP
.B VERBOSE
Set to 1 to enable verbose mode, default is 0.
.TP
.B DEBUG
Set to 1 to enable debug mode, default is 0.

.SH NOTES
Important: bla bla bla.

.SH EXAMPLE
.nf
.RS
# Configuration file for $SCRIPT_BASENAME
# See the $CONFIG_FILE_BASENAME(5) manpage for details

VERBOSE=1
DEBUG=0
.RE
.fi

.SH SEE ALSO
.BR $SCRIPT_BASENAME (1).
EOF
