#!/usr/bin/env bash
#===============================================================================
#
# DIRECTORY:
#   ---
#
# FILE:
#   ./kik_plugin.sh
#
# USAGE:
#     . kik_plugin.sh
#   OR
#     source kik_plugin.sh
#
# DESCRIPTION:
#   Kik messenger plugin (library) to generate content with simple template engine.
#
# BUGS:
#   ---
#
# TESTS:
#   - shellcheck -s bash -e SC2034 ./kik_plugin.sh
#   - shellcheck -s ksh -e SC2034 ./kik_plugin.sh
#   - shellcheck -s dash -e SC2034 ./kik_plugin.sh
#
# AUTHOR:
#   Patrick Neumann, patrick@neumannsland.de
#
# COAUTHOR(S):
#   Odin Heitmann, odin.heitmann@gmail.com
#
# COMPANY:
#   (privately)
#
# VERSION:
#   1.0
#
# LINK TO THE MOST CURRENT VERSION:
#   (Sorry, we bet, I'm not allowed to publish it over GitHub!)
#
# CREATED:
#   2018-06-01
#
# COPYRIGHT (C):
#   2018 - Patrick Neumann & Odin Heitmann
#
# LICENSE:
#   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.
#
# WARRANTY:
#   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/>.
#
# NOTES:
#   ---
#
# TODO:
#   ---
#
# HISTORY:
#   1.0 - P. N. & O. H. - Initial (for the trainers eyes only) release
#
#===============================================================================

#-------------------------------------------------------------------------------
# Check if functions library is loaded first.
#-------------------------------------------------------------------------------
if [ "${FUNCTIONS_LOADED}" != "true" ] ; then
  printf "\\n\\033[01;31;40mERROR: functions library not loaded... EXIT!!!\\033[00m\\n\\n"
  exit 200
fi

#-------------------------------------------------------------------------------
# Check if temblate engine library is loaded first.
#-------------------------------------------------------------------------------
if [ "${TEMPLATE_ENGINE_LOADED}" != "true" ] ; then
  error_exit "template_engine.sh not loaded" 200
fi

#=== CONFIGURATION (static) ====================================================
#assign_binary "xyz"                                                # (un)common

#=== FUNCTION ==================================================================
# NAME: get_kik_list
# DESCRIPTION: "Grep" information and store into vars.
# PARAMETERS: none (global vars will be used.)
#===============================================================================
get_kik_list () {
  # "8e281be6657d4523710d96341b6f86ba89b56df7" in iTunes Backup.
  readonly KIK_SQLITE="$( ${BIN_FIND} "${IOS_BACKUP}" -type f -name "kik.sqlite" )"
	
  if ! [ -f "${KIK_SQLITE}" ] ; then
    readonly KIK_SOURCE="<code>kik.sqlite not found</code>"
		error "kik.sqlite not found"
    readonly KIK_CONTACTS="<tr><td class='highlight' colspan='4'>ERROR: database not found!</td></tr>"
    readonly KIK_MESSAGES="<tr><td class='highlight' colspan='5'>ERROR: database not found!</td></tr>"
	else
    KIK_SOURCE="<a href='kik.sqlite' target='_blank'><code>${KIK_SQLITE/${IOS_BACKUP}/\/private\/var}</code></a>"
    # Don't forget to copy write ahead log!
    ${BIN_CP}	"${KIK_SQLITE}"* "${FOLDER}/"

	  readonly KIK_CONTACTS="$( ${BIN_SQLITE3} -html "${FOLDER}/kik.sqlite" \
	                           "SELECT
	                              Z_PK,
	                              ZDISPLAYNAME,
	                              ZFIRSTNAME,
	                              ZLASTNAME,
	                              ZUSERNAME
	                            FROM
	                              ZKIKUSER;" )"

	  readonly KIK_MESSAGES="$( ${BIN_SQLITE3} -html "${FOLDER}/kik.sqlite" \
	                            "SELECT
	                               m.Z_PK,
	                               CASE WHEN m.ZTYPE=1 THEN 'received' WHEN m.ZTYPE=2 THEN 'sent' ELSE '(system)' END,
	                               u.ZUSERNAME,
	                               DATETIME(m.ZTIMESTAMP+978307200, 'unixepoch'),
	                               m.ZBODY
	                             FROM
	                               ZKIKMESSAGE AS m,
	                               ZKIKUSER AS u
	                             WHERE
	                               m.ZUSER=u.Z_PK;" )"
  fi
	
  if [ "${DEBUG}" = "on" ] ; then
    ${BIN_PRINTF} "INFO: kik_plugin processed.\\n" 1>&2
	fi
}

#-------------------------------------------------------------------------------
# Partial template for hardware information for the simple template engine.
#-------------------------------------------------------------------------------
# SC2034: all "unused" vars are verified!
KIK_INFORMATION=$( ${BIN_CAT} <<'EOF'
          <h2>Kik Messenger</h2>
          <h4>Sources</h4>
          <ul class="file">
						<li>${KIK_SOURCE}</li>
          </ul>
          <h3>Contacts</h3>
         <table>
            <tr>
              <th>ID</th>
              <th>Displayname</th>
              <th>Firstname</th>
              <th>Lastname</th>
              <th>Username</th>
            </tr>
            ${KIK_CONTACTS}
					</table>
          <h3>Messages</h3>
            <table>
              <tr>
                <th>ID</th>
                <th>Direction</th>
                <th>Username</th>
                <th>Date</th>
                <th>Message</th>
              </tr>
              ${KIK_MESSAGES}
            </table>
EOF
)

readonly KIK_PLUGIN_LOADED="true"

if [ "${DEBUG}" = "on" ] ; then
  ${BIN_PRINTF} "INFO: kik_plugin.sh loaded.\\n" 1>&2
fi
# Do not use "exit" at the end of a sourced library!