#
# this is the general include file with variables
# and function for all other scripts
#
# this file is necessary for every other script!
#
# Usage:
#   . jwm_env
#
# If the script does not find a valid JWM / Linux
# environment it will end the script with an RC not
# zero.
# If a valid JWM / Linux environment is found the
# current working directory will be set to this dir
# and the variable CURRENT_JWM_DIR contains the
# name of the directory.
#
# History
#    30.10.2013/bs
#      initial release
#
#    09.06.2014/bs
#       minor changes for JWM for Linux 2.0.0
#


# check for the JWM / Linux directory
#
INCLUDE_FILE="jwm_env"

THISSCRIPT="$0"

[ "${SCRIPTNAME}"x = ""x ] && SCRIPTNAME="${0##*/}"
[ "${SCRIPTDIR}"x = ""x ] && SCRIPTDIR="${0%/*}"

[ "${SCRIPTDIR}"x =  "$0"x ] && SCRIPTDIR="${PWD}"

if [ "${JWM_DIR}"x = ""x ] ; then
  cd "${SCRIPTDIR}" && JWM_DIR="${PWD}" && cd - >/dev/null
fi

JWM_DIR="${JWM_DIR:=/var/tmp/jwm}"

echo "JWM_DIR is \"${JWM_DIR}\" "

if [ -r "${INCLUDE_FILE}" ] ; then
  :
elif [ -r "${SCRIPTDIR}/${INCLUDE_FILE}" ] ; then
  cd "${SCRIPTDIR}"
elif [ -r "jwm/${INCLUDE_FILE}" ] ; then
  cd jwm
elif [ -r "../${INCLUDE_FILE}" ] ; then
  cd ..
elif [ -r "${JWM_DIR}/${INCLUDE_FILE}" ] ; then
  cd "${JWM_DIR}/"
else
  echo "JWM / Linux directory NOT found!"
  exit 5
fi

THIS_DIR="${PWD}"
export THIS_DIR

CURRENT_JWM_DIR="${THIS_DIR}"

ARC="$( uname -m )"
RELEASE="$( uname -r )"

if  which lsb_release 2>/dev/null  >/dev/null ; then
  DIST_NAME="$( lsb_release -s -d  2>/dev/null | tr -d '"' )"
elif [ -r /etc/os-release ] ; then
  DIST_NAME="$( grep PRETTY_NAME /etc/os-release 2>/dev/null | cut -f2- -d "=" )"
elif [ -r /etc/system-release ] ; then
  DIST_NAME=$( cat /etc/system-release )
fi

if [ "${DIST_NAME}"x = ""x ] ; then
  DIST_PRINT_NAME="unknown_distribution"
else
  DIST_PRINT_NAME="$( echo "${DIST_NAME}" |  tr  " " "_" | tr -d '"' | cut -f2  )"
fi
CUR_DATE="$( date +%Y-%m-%d )"

ARCHIVE_VERSION="$( cat VERSION | tr -d "." )"
if [ "${ARCHIVE_VERSION}"x = ""x ] ; then
  echo "ERROR: File VERSION not found."
  exit 10
fi

# name for a new archive created by create_archive.sh
#
ARCHIVE_NAME="$( echo "jwml_v${ARCHIVE_VERSION}_${DIST_PRINT_NAME}_${RELEASE}_${ARC}_${CUR_DATE}.tar.gz" | tr "/" "_" )"


# description for a new archive created by create_archive.sh
#
CURRENT_DESC="JMW / Linux v${ARCHIVE_VERSION} created on ${DIST_PRINT_NAME} ${RELEASE} ${ARC} at ${CUR_DATE}"


# -----------------------------------------------------------------------------
# general sub routines

logMsg() {
  typeset THISMSG

  if [ "$1"x = "-"x ] ; then
    shift
    THISMSG="$*"
  else
    THISMSG="[${SCRIPTNAME}] $*"
  fi

  echo "${THISMSG}"
  [ "${LOGFILE}"x != ""x ] && "${THISMSG}" >>"${LOGFILE}"
  return 0
}

logInfo() {
  [ "${VERBOSE}"X != ""X ] && logMsg "INFO: $*"
}

logWarning() {
  logMsg "WARNING: $*" >&2
}

logError() {
  logMsg "ERROR: $*" >&2
}

die () {
  typeset THISRC=$1
  if [ $# -gt 1 ] ; then
    shift
    typeset THISMSG="$*"
    if [ "${THISRC}"x = "0"x ] ; then
      logMsg "${THISMSG}"
    else
      logError "${THISMSG}"
    fi
  fi
  exit ${THISRC}
}

# -----------------------------------------------------------------------------
#
create_archive_desc_file() {
  echo "${CURRENT_DESC}" >"${JWM_DIR}/JWM_DESC"
}

read_archive_desc_file() {
  cat "${JWM_DIR}/JWM_DESC" 2>/dev/null || echo "Initial archive"
}

# -----------------------------------------------------------------------------
# init some other variables
#
  ARCHIVE_DESC="$( read_archive_desc_file )"

# -----------------------------------------------------------------------------
