#!/usr/bin/env bash
#===============================================================================
#
# DIRECTORY:
#   ---
#
# FILE:
#   ./os_plugin.sh
#
# USAGE:
#     . os_plugin.sh
#   OR
#     source os_plugin.sh
#
# DESCRIPTION:
#   Operating system plugin (library) to generate content with simple template engine.
#
# BUGS:
#   ---
#
# TESTS:
#   - shellcheck -s bash -e SC2034,SC2016 ./os_plugin.sh
#   - shellcheck -s ksh -e SC2034,SC2016 ./os_plugin.sh
#   - shellcheck -s dash -e SC2034,SC2016 ./os_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) ====================================================
# See template engine library!

#=== FUNCTION ==================================================================
# NAME: get_os_info
# DESCRIPTION: "Grep" information and store into vars.
# PARAMETERS: none (global vars will be used.)
#===============================================================================
get_os_info () {
  # NOT in iTunes Backup!
  readonly LOCKDOWND_LOG="$( ${BIN_FIND} "${IOS_BACKUP}" -type f -name "lockdownd.log" )"

  if [ -z "${LOCKDOWND_LOG}" ] ; then
    readonly LOCKDOWND_SOURCE="<code>lockdownd.log not found</code>"
    error "lockdownd.log not found"
    readonly BUILD_NUMBER="unknown"
    readonly OS_VERSION="unknown"
	else
    readonly LOCKDOWND_SOURCE="<a href='lockdownd.log' target='_blank'><code>${LOCKDOWND_LOG/${IOS_BACKUP}/\/private\/var}</code></a>"
    ${BIN_CP}	"${LOCKDOWND_LOG}" "${FOLDER}/"

	  # SC2016: shellcheck fails if awk/sed is used!
	  readonly BUILD_NUMBER="$( ${BIN_AWK} '/Build=/ { print $NF; }' "${FOLDER}/lockdownd.log" | ${BIN_HEAD} -n 1 )"

	  [ -f "$( ${BIN_DIRNAME} "${0}" )/lib/iphone_os_map.tsv" ] ||  error_exit "iphone_os_map.tsv not found" 201

	  OS_VERSION="$( ${BIN_GREP} --fixed-strings --word-regexp "${BUILD_NUMBER}" $( ${BIN_DIRNAME} "${0}" )/lib/iphone_os_map.tsv \
	                 | ${BIN_CUT} -f 1 )"
		[ -z "${OS_VERSION}" ] && readonly OS_VERSION="<span title='Not found in our map - please ask for an update!'>unknown</span>"  
  fi

  #--- next source ---

  # EVENTUALLY in iTunes Backup!?
	# There are three com.saurik.Cydia named folders:
	#   - mobile/Library/Caches/Snapshots/com.saurik.Cydia
	#   - mobile/Library/Caches/Snapshots/com.saurik.Cydia/com.saurik.Cydia
	#   - mobile/Library/Caches/com.saurik.Cydia
  CACHE_CYDIA="$( ${BIN_FIND} "${IOS_BACKUP}" -type d -path "*Caches/com.saurik.Cydia" )"

  JAILBREAK="no"
  if [ -n "${CACHE_CYDIA}" ] ; then
		if [ -n "$( ${BIN_LS} "${CACHE_CYDIA}" )" ] ; then
      readonly JAILBREAK="yes"
    fi
  fi
  [ "${JAILBREAK}" = "no" ] && readonly CACHE_CYDIA="com.saurik.Cydia not found"

	if [ "${DEBUG}" = "on" ] ; then
	  ${BIN_PRINTF} "OS_VERSION: %s\\n" "${OS_VERSION}" 1>&2
	  ${BIN_PRINTF} "BUILD_NUMBER: %s\\n" "${BUILD_NUMBER}" 1>&2
	  ${BIN_PRINTF} "JAILBREAK: %s\\n" "${JAILBREAK}" 1>&2
	fi
}

#-------------------------------------------------------------------------------
# Partial template for hardware information for the simple template engine.
#-------------------------------------------------------------------------------
# SC2034: all "unused" vars are verified!
OS_INFORMATION=$( ${BIN_CAT} <<'EOF'
          <h2>Operating System</h2>
          <h4>Sources</h4>
          <ul class="file">
						<li>${LOCKDOWND_SOURCE}</li>
						<li><code>${CACHE_CYDIA/${IOS_BACKUP}/\/private\/var}</code></li>
          </ul>
          <h3>Information</h3>
          <table>
            <tr>
              <td>iOS Version:</td>
              <td>${OS_VERSION}</td>
            </tr>
            <tr>
              <td>Build Number:</td>
              <td>${BUILD_NUMBER}</td>
            </tr>
            <tr>
              <td>Jailbreak detected:</td>
              <td>${JAILBREAK}</td>
            </tr>
          </table>
EOF
)

readonly OS_PLUGIN_LOADED="true"

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