#!/bin/bash
#
# moderator-contributions.sh
#
#    Copyright (C) 2013  Matti Lammi
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# This script will produce information about my (lammi87) 
# contributions to h-node as a moderator in human readable form. It 
# will use tools commonly found in GNU/Linux operating system 
# distributions and a source file to do this. Results will be saved 
# to a file and also displayed immediately.
# 
# The source file is a text file consisting on several one line long 
# entries of data. The data is copied from h-node's list of actions 
# carried out by moderators (available only to moderators and 
# h-node's main developer) where a new entry is made every time a 
# user who has moderator's rights carries out an action which only 
# moderators can do. 
#
# This list is rather long, so I have narrowed it down by filtering 
# out every entry about other moderator's actions using a search 
# filter found in the list's page, thus keeping my source file short.
#
# I have used many line breaks in this script so that I can keep the 
# layout clean when adding this script to my final thesis as an 
# appendix.
#
# Please note that the output of this script may not be 100% accurate.
# I think, however, that the results are accurate enough.
#
# SOURCE FILE
# File name:            source-moderator
# Date of first entry:   2 October 2012
# Date of last entry:   22 March   2013
#


### Print instructions. 
###
# Echo program's -e option allows special characters like 
# \n to be implemented.
# \n = new line
# \t = tab
echo -e "H-node script: Actions as moderator\n"
echo -e "First argument:\t\t\tSource file"
echo -e "Second argument: \t\tOutput file\n"


### Quit if source file is not found.
###
# Check if first argument is null.
# !  = not
# -n = not null
if [ ! -n "$1" ]
then
echo "ERROR: No source given. Read source code for more info."
exit 1
fi

# Check if source does not exist.
# !  = not
# -f = file exists
if [ ! -f "$1" ]
then
echo "ERROR: Source not found. Read source code for more info."
exit 1
fi


### Quit if output file is not given.
###
# Check if second argument is null.
# !  = not
# -n = not null
if [ ! -n "$2" ]
then
echo "ERROR: No output file given. Read source code for more info."
exit 1
fi


### Create output file.
###
# \n = new line
touch $2
echo -e "Output file created.\n\n"


### Define table format and lines
###
# I will print the data processed from the source files into easy 
# to read tables. I use "printf" instead of "echo" because it has 
# more options to format the output. I'll define a format for the 
# tables so I can easily use it to print several tables and keep 
# their formatting unanimous. The format is a 35 character long space 
# followed by eight character long space and a new line character. 
# I added the new line character because "printf" does not include 
# it to its output by default. The first space will align characters 
# printed into it to the left. The other space will align its content 
# to right.
# %s = string
# NN = reserved character space in %NNs
#  - = alignment to left
# \n = new line 
TABLE_FORMAT="%-35s%8s\n"

# I'll be using two kinds of "lines" of characters to make the 
# output look more readable. I'll define them here.
THICK_LINE="============================================"
THIN_LINE="--------------------------------------------"

### Find out my actions as moderator:
###
# %s = string
# \n = new line 
echo "Actions as moderator:" > $2
printf "%s\n\n" $THICK_LINE >> $2

# Find out how many issues I have hidden:
#
# Inspect all lines of the source file, get only those which 
# have my username followed by one or more spaces and the 
# word "hide", count them and print the result.
# NUM=$() = Run code defined in brackets and save result to NUM.
#      \s = any white space character (regex)
#       * = preceding character zero or more times (regex)
NUM=$(cat $1 | grep "lammi87\s*hide" | wc -l)
printf $TABLE_FORMAT "Issues hidden by me:" $NUM >> $2

# Find out how many issues I have closed:
#
# Inspect all lines of the source file, get only those which 
# have my username followed by one or more spaces and the word 
# "close", count them and print the result.
# NUM=$() = Run code defined in brackets and save result to NUM.
#      \s = any white space character (regex)
#       * = preceding character zero or more times (regex)
NUM=$(cat $1 | grep "lammi87\s*close" | wc -l)
printf $TABLE_FORMAT "Issues closed by me:" $NUM >> $2

# Find out how many actions I have made in total:
#
# Inspect all lines of the source file, count them and print 
# the result.
#      %s = string
#      \n = new line
# NUM=$() = Run code defined in brackets and save result to NUM.
printf "%s\n" $THIN_LINE >> $2
NUM=$(cat $1 | wc -l)
printf $TABLE_FORMAT "Total:" $NUM >> $2


### Find out how many actions I did per month.
###
# For each year, write an entry to output file. For each 
# month, write an entry to output file including how many 
# actions I have made in that particular month. The "let" 
# command enables arithmetic operations on variables.
#      \n = new line 
#      %s = string
#       \ = line break
# NUM=$() = Run code defined in brackets and save result to NUM.
echo -e "\n\nMy actions per month:" >> $2
printf "%s\n" $THICK_LINE >> $2

for YEAR in 2012 2013 
do

   printf "\n$TABLE_FORMAT" "Year:" $YEAR >> $2
   printf "%s\n" $THIN_LINE >> $2

   for MONTH in January February March April May June July August \
   September October November December
   do

      NUM=$(cat $1 | grep "$MONTH $YEAR" | wc -l)
      printf $TABLE_FORMAT $MONTH $NUM >> $2
      let "TOTAL=$TOTAL+$NUM"

   done

   printf "%s\n" $THIN_LINE >> $2
   printf $TABLE_FORMAT "Total:" $TOTAL >> $2
   TOTAL=0

done


### Show results.
###
cat $2


### Quit
###
# \n = new line
echo -e "\nAll done.\n"
exit 0
