#!/usr/bin/env bash
#===============================================================================
#
# DIRECTORY:
#   ---
#
# FILE:
#   ./configuration_plugin.sh
#
# USAGE:
#     . configuration_plugin.sh
#   OR
#     source configuration_plugin.sh
#
# DESCRIPTION:
#   Configuration plugin (library) to generate content with simple template engine.
#
# BUGS:
#   ---
#
# TESTS:
#   - shellcheck -s bash -e 2034 ./configuration_plugin.sh
#   - shellcheck -s ksh -e 2034 ./configuration_plugin.sh
#   - shellcheck -s dash -e 2034 ./configuration_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_configuration_info
# DESCRIPTION: "Grep" information and store into vars.
# PARAMETERS: none (global vars will be used.)
#===============================================================================
get_configuration_info () {
  # NOT in iTunes Backup!
	# There are two data_ark.plist:
	#   - mobile/Library/mad/data_ark.plist
	#   - root/Library/Lockdown/data_ark.plist
  readonly DATA_ARK_PLIST="$( ${BIN_FIND} "${IOS_BACKUP}" -type f -path "*Lockdown/data_ark.plist" )"
	
  if [ -z "${DATA_ARK_PLIST}" ] ; then
    readonly DATA_ARK_SOURCE="<code>data_ark.plist not found</code>"
    error "data_ark.plist not found"
    readonly DEVICE_NAME="unknown"
    readonly TIME_ZONE="unknown"
    readonly LOCALE="unknown"
	else
    readonly DATA_ARK_SOURCE="<a href='data_ark.plist' target='_blank'><code>${DATA_ARK_PLIST/${IOS_BACKUP}/\/private\/var}</code></a>"
    plist_bin2xml "${DATA_ARK_PLIST}" "${FOLDER}/data_ark.plist"

	  readonly DEVICE_NAME="$( ${BIN_GREP} --fixed-strings --after-context=1 "DeviceName" "${FOLDER}/data_ark.plist" \
		                | ${BIN_TAIL} -n 1 \
		                | ${BIN_SED} "${SED_EXT_REGEXP}" 's|[[:space:]]*</?string>||g' )"

	  readonly TIME_ZONE="$( ${BIN_GREP} --fixed-strings --after-context=1 "TimeZone" "${FOLDER}/data_ark.plist" \
		                | ${BIN_TAIL} -n 1 \
		                | ${BIN_SED} "${SED_EXT_REGEXP}" 's|[[:space:]]*</?string>||g' )"


	  readonly LOCALE="$( ${BIN_GREP} --fixed-strings --after-context=1 "com.apple.international-Locale" "${FOLDER}/data_ark.plist" \
		                | ${BIN_TAIL} -n 1 \
		                | ${BIN_SED} "${SED_EXT_REGEXP}" 's|[[:space:]]*</?string>||g' )"
  fi

  #--- next source ---

  # "7ddb1ea8c09e5baae5e2d2ecac604a4e4e3087de" in iTunes Backup.
  LOCK_BACKGROUND="$( ${BIN_FIND} "${IOS_BACKUP}" -type f -name "LockBackgroundThumbnail.jpg" )" # <- could be modified later!

  if [ -z "${LOCK_BACKGROUND}" ] ; then
    readonly LOCK_BACKGROUND="LockBackgroundThumbnail.jpg not found"
    readonly LOCK_BACKGROUND_SOURCE="<code>${LOCK_BACKGROUND}</code>"
		error "LockBackgroundThumbnail.jpg not found"
	else
    readonly LOCK_BACKGROUND_SOURCE="<a href='LockBackgroundThumbnail.jpg' target='_blank'><code>${LOCK_BACKGROUND/${IOS_BACKUP}/\/private\/var}</code></a>"
    ${BIN_CP}	"${LOCK_BACKGROUND}" "${FOLDER}/"

		if [ "${DEBUG}" = "on" ] ; then
		  ${BIN_PRINTF} "INFO: LockBackgroundThumbnail.jpg copied\\n" 1>&2
		fi
  fi

	if [ "${DEBUG}" = "on" ] ; then
	  ${BIN_PRINTF} "DEVICE_NAME: %s\\n" "${DEVICE_NAME}" 1>&2
	  ${BIN_PRINTF} "TIME_ZONE: %s\\n" "${TIME_ZONE}" 1>&2
	  ${BIN_PRINTF} "LOCALE: %s\\n" "${LOCALE}" 1>&2
	fi
}

#-------------------------------------------------------------------------------
# Partial template for hardware information for the simple template engine.
#-------------------------------------------------------------------------------
# SC2034: all "unused" vars are verified!
CONFIGURATION_INFORMATION=$( ${BIN_CAT} <<'EOF'
          <h2>Configuration</h2>
          <div class='ym-grid'>
            <div class='ym-g75 ym-gl'>
              <div class='ym-gbox'>
                <h4>Sources</h4>
                <ul class="file">
                  <li>${DATA_ARK_SOURCE}</li>
                  <li>${LOCK_BACKGROUND_SOURCE}</li>
		            </ul>
		            <h3>Information</h3>
                <table>
                  <tr>
                    <td>Device Name:</td>
                    <td>${DEVICE_NAME}</td>
                  </tr>
                  <tr>
                    <td>Time Zone:</td>
                    <td>${TIME_ZONE}</td>
                  </tr>
                  <tr>
                    <td>Locale:</td>
                    <td>${LOCALE}</td>
                  </tr>
                 </table>
               </div>
             </div>
             <div class='ym-g25 ym-gr'>
               <div class='ym-gbox'>
                 <h3>Background</h3>
                 <img alt='${LOCK_BACKGROUND}' class='center' src='LockBackgroundThumbnail.jpg' title='Lock Background Image' height='240'>
               </div>
             </div>
           </div>
EOF
)

readonly CONFIGURATION_PLUGIN_LOADED="true"

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