#!/usr/bin/env bash

SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`

# toolset:---------------------------------------------------------------------

command 2> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)

function lineprint {
    printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
}

function message {
    lineprint
    printf "$1\n"
    lineprint
}

function error_message {
    lineprint
    printf "$1\n" >&2
    lineprint
}

current_action="IDLE"

function confirm_action {
    message "successfully finished action: $current_action"
}

function set_action {
    current_action="$1"
    message "$1"
}

function perform {
    "$@"
    local status=$?
    if [ $status -ne 0 ]
    then
        error_message "$current_action failed!"
    fi
    return $status
}

function perform_and_exit {
    perform "$@" || exit 1
}

# -----------------------------------------------------------------------------
# reading input

INPUT=$1
OUTPUT="$INPUT.csv"

if [ $# -eq 0 ]
then
    error_message "Error: no input file given. Usage: $0 <Whatsapp textbackup file>"
    exit 1
fi

set_action "processing File: ${INPUT}"

if [ ! -e "$INPUT" ]
then
    error_message "Error: file '$INPUT' not found"
    exit 1
fi

perform_and_exit echo -e "date\ttime\tperson\tmessage" > "$OUTPUT"

# doing the following things in the pipeline below:
#
# 1. merging multiline messages by replacing newlines with spaces if they are not starting with a number followed by '/' (not stating with a date)
# 2. delete all lines containing the keywords: "added, creates, end-to-end, Media ommited" (which are most probably system messages)
# 3. replace separators of whatsapp's txt-format with '\t' as separator for csv
# 4. delete all double-quotes. Because people are too stupid to type them in pairs and that breaks some csv-interpreters 🤦‍♂

perform_and_exit sed ':a;N;/\n[0-9]\{1,\}\+\//!s/\n/ /;ta;P;D' "$INPUT" | grep -v -E 'left|added|created|end-to-end|Media omitted|import java|<VirtualHost' | sed 's/, /\t/1; s/- /\t/1; s/: /\t/1;' | tr -d '"' >> "$OUTPUT"

confirm_action

message "Wrote output to $OUTPUT"