#!/bin/bash
# Purple Logs Cleaner
# Copyright (C) 2009  Marco Leogrande
# 
# Purple Logs Cleaner 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.
# 
# Purple Logs Cleaner 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/>.

## helpers ##
usage(){
    echo
    echo "Usage: $0 action [language options]"
    echo
    echo "Available actions (only one can be selected):"
    echo " speed   : perform a fast clean. May fail if there too many log files"
    echo " verbose : perform a slightly slower cleaning, but much more robust against failures"
    echo
    echo "Language options (if multiple options are specified, the latter prevails):"
    echo "(default) Clean using only English patterns"
    echo " -i       Clean all kinds of recognized patterns"
    echo "          (currently, English and Italian strings)"
    echo " -I       Clean only strings formatted in Italian"
    echo "Different patterns may result in different cleaning speeds"

    exit 0
}

count_lines(){
    wc -l "$1" | cut -f 1 -d " "
}

get_dirsize(){
    du -hs "$1" | cut -f 1
}

clean_log_file(){
    if [[ x"$2" == x ]]
    then
        echo "Internal error"
        return 1
    fi

    if [[ x"$1" == x ]]
    then
        echo "Invalid file to clean"
        return 1
    fi

    if [[ ! -f "$1" ]]
    then
        echo "$1 is not a file"
        return 1
    fi

    if [[ ! -r "$1" ]]
    then
        echo "$1 is unreadable"
        return 1
    fi  

    if [[ ! -w "$1" ]]
    then
        echo "$1 is not writable"
        return 1
    fi

    lines_in=$(count_lines "$1")
    echo -n "Cleaning $(basename "$1") ... "
    sed -i -e "$2" "$1"
    echo "DONE, ${lines_in} lines in, $(count_lines "$1") lines out"
    return 0
}

clean_log_dir(){
    if [[ x"$2" == x ]]
    then
        echo "Internal error"
        return 1
    fi

    if [[ x"$1" == x ]]
    then
        echo "Invalid directory to clean"
        return 1
    fi

    if [[ ! -d "$1" ]]
    then
        echo "$1 is not a directory"
        return 1
    fi

    if [[ ! -r "$1" ]]
    then
        echo "$1 is unreadable"
        return 1
    fi  

    if [[ ! -w "$1" ]]
    then
        echo "$1 is not writable"
        return 1
    fi

    old_size=$(get_dirsize "$1")
    echo -n "Cleaning $(dirname "$1")/.system ... "
    sed -i -e "$2" "$1"/*.txt
    echo "DONE, old size ${old_size}, new size $(get_dirsize "$1")"
    return 0
}


## "main" ##
if [[ $# -eq 0 ]]
then
    echo "Give me something to do!"
    usage
fi

case $1 in
    speed)
        verbose=0
        ;;
    verbose)
        verbose=1
        ;;
    *)
        echo "Unrecognized action: $1"
        usage
        ;;
esac

shift

regex_eng='/.*changed status from \(.*\) to \1 @.*/d'
regex_ita='/.*ha cambiato stato da \(.*\) a \1 @.*/d'
regex_all='/.*\(ha cambiato stato da \(.*\) a \2\|changed status from \(.*\) to \3\) @.*/d'
regex_chosen="$regex_eng"
while [[ $# -gt 0 ]]
do
    case $1 in
        -i)
            echo "Using internationalized cleaning patterns"
            regex_chosen="$regex_all"
            ;;
        -I)
            echo "Using Italian cleaning patterns"
            regex_chosen="$regex_ita"
            ;;
        *)
            echo "Unrecognized option: $1"
            usage
            ;;
    esac

    shift
done

if [[ x$(pidof pidgin) != "x" || x$(pidof finch) != "x" ]]
then
    echo "Either Pidgin or Finch is running, I WON'T touch logs"
    exit 1
fi

if [[ $verbose -eq 1 ]]
then
    time find "${HOME}/.purple" -type f -regex ".*\.system/[^/]*.txt" | ( while read filename
        do
            clean_log_file "$filename" "$regex_chosen"
        done
    )
else
    time find "${HOME}/.purple" -type d -regex ".*\.system$" | ( while read dirname
        do
            clean_log_dir "${dirname}" "$regex_chosen"
        done
    )
fi

