#!/bin/bash

#rndsig, Written by Storm Dragon
#Released under the terms of the WTFPL: http://wtfpl.net

#exit if root
if [ $(whoami) == "root" ] ; then
    echo "$(basename $0) should not be ran as root..."
    exit 0
fi

# Get numeric argument and remove from arg list. If not numeric, fall through to help.
if [[ "$1" =~ ^[0-9]+$ ]] ; then
        arg="$1"
        shift
else
        arg=""
fi

#Determine XDG path
if [ -z "$XDG_CONFIG_HOME" ] ; then
    XDG_CONFIG_HOME="$HOME/.config"
fi

#Make path for rndsig if it isn't there already
if ! [ -d "$XDG_CONFIG_HOME/rndsig$arg" ] ; then
    mkdir -p "$XDG_CONFIG_HOME/rndsig$arg"
fi

#program help
show_usage()
{
cat << EOF
Usage:
With no arguments or a single numeric argument rndsig displays your signature.
-h or --help, or any other arguments for that matter, shows this message.

Signature files go in your XDG_CONFIG_HOME/rndsig/ directory (usually ~/.config/rndsig/).
If you provide a numeric argument, the files will go in your XDG_CONFIG_HOME/rndsig<arg> directory.
Files are called X.sig where .ext determines how the signature will be displayed.
All files are optional.

2DigitDay-2DigitMonth.sig: This file is displayed on the date specified by 2 digit month - 2 digit day.
If you wanted to wish everyone a happy new year, add the text happy new year to a file called 01-01.sig
Holiday signatures are shown before any other signature info.

all.sig: the text from this file is displayed in all signatures. The whole file is placed inside the signature.
It is displayed first in the signature, unless there is a holiday signature.

random.sig: A line is chosen at random from this file and added to the signature after
the holiday message and after signature.all if they exist. Tabs and the characters \n are replaced with new lines.

list.sig: A single line is added from this file to your signature. The Lines are added in the order they appear in the
file. After the final line has been added, the lines start over from the beginning. Tabs and the characters \n are replaced with new lines. This file
appears last in your signature, below holidays, signature.all, and signature.random if they exist but before scripts if they exist.

Special case: file.sh:
If there are any files ending in .sh in your rndsig directory, they will be ran through bash and added to the very end of your signature.
EOF
exit 0
}

#Deal with arguments:
if [ $# -gt 0 ] ; then
    show_usage
fi

#get Index for signature.list
if [ -f "$XDG_CONFIG_HOME/rndsig$arg/count" ] ; then
    listIndex=$(cat "$XDG_CONFIG_HOME/rndsig$arg/count")
    #Make sure our list number is in range.
    if [ -f "$XDG_CONFIG_HOME/rndsig$arg/list.sig" ] ; then
     if [ "$listIndex" -gt "$(wc -l "$XDG_CONFIG_HOME/rndsig$arg/list.sig" | tr -Cd "[:digit:]")" ] ; then
            listIndex=1
        fi
    fi
else
    listIndex=0
fi

#increment listIndex for the next time.
echo "$(($listIndex + 1))" > "$XDG_CONFIG_HOME/rndsig$arg/count"

#Construct signature from files
#Holiday files are a special Case. Any holiday signature file will be placed at the top of the signature.
#For example, to have a signature on Halloween, name the file 10-31.sig
dateExtension="$(date +'%m-%d')"
if [ -f "$XDG_CONFIG_HOME/rndsig$arg/${dateExtension}.sig" ] ; then
    cat "$XDG_CONFIG_HOME/rndsig$arg/${dateExtension}.sig"
fi

#The all.sig file holds items for every signature and is the first thing in the signature. Itemse are displayed exactly as written.
if [ -f "$XDG_CONFIG_HOME/rndsig$arg/all.sig" ] ; then
    cat "$XDG_CONFIG_HOME/rndsig$arg/all.sig"
fi

#The random.sig file holds items that should be added randomly, 1 per signature and is displayed second in the signature. Tabs are replaced with  new lines.
if [ -f "$XDG_CONFIG_HOME/rndsig$arg/random.sig" ] ; then
    shuf -n 1 "$XDG_CONFIG_HOME/rndsig$arg/random.sig" | sed 's/\t/\n/g' | sed 's/\\n/\n/g'
fi

#The list.sig file holds items that should be cycled through in order 1 at a time. Tabs are replaced with newlines in this file. It is shown last.
if [ -f "$XDG_CONFIG_HOME/rndsig$arg/list.sig" ] ; then
    head -n $listIndex "$XDG_CONFIG_HOME/rndsig$arg/list.sig" | tail -n 1 | sed 's/\t/\n/g' | sed 's/\\n/\n/g'
fi
# Run any scripts in the rndsig directory.
find "$XDG_CONFIG_HOME/rndsig$arg" -maxdepth 1 -type f -name '*.sh' -exec  bash -c "{}" +
exit 0
