#!/bin/bash
# ================================================================================
#
# LOCALDUMP :: SQLITE
# create gzipped plain text backups from each sqlite database in
# backup-dbfiles.job
#
# --------------------------------------------------------------------------------
# ah - Axel Hahn <axel.hahn@iml.unibe.ch>
# ds - Daniel Schueler <daniel.schueler@iml.unibe.ch>
#
# 2018-02-02  ah,ds  v1.0  first lines
# 2018-02-09  ah,ds  v1.1  write a .meta file after successful backup
# 2022-03-17         v1.2  WIP: add lines with prefix __DB__
# ================================================================================

if [ -z "$BACKUP_TARGETDIR" ]; then
  echo "ERROR: you cannot start $(basename $0) directly"
  rc=$rc+1
  exit 1
fi
# --------------------------------------------------------------------------------
# CONFIG
# --------------------------------------------------------------------------------

FILEDEFS=${DIR_JOBS}/backup-dbfiles.job

# --------------------------------------------------------------------------------
# FUNCTIONS
# --------------------------------------------------------------------------------

# make sqlite3 backups of all sqlite = ... in backup-dbfiles.job
function doSqliteBackup(){
  if ! _j_getvar ${FILEDEFS} "sqlite" | grep . ; then
    echo "__DB__$SERVICENAME SKIP: no entries found for sqlite."
    return 0
  fi

  create_targetdir

  for DATABASE_FILE in $(_j_getvar ${FILEDEFS} "sqlite")
  do
    echo -n "__DB__${SERVICENAME} backup $DATABASE_FILE "
    if [ ! -f "$DATABASE_FILE" ]; then
      color error
      echo "ERROR: given database file does not exist"
      color reset
      rc=$rc+1
    else
      file "$DATABASE_FILE" | cut -f 2 -d ":" | grep -i "sqlite" >/dev/null
      if [ $? -ne 0 ]; then
        color error
        echo "ERROR: given database file is not a sqlite database"
        color reset
        rc=$rc+1
      else
        TARGET=$(get_outfile ${DATABASE_FILE})
        TARGET=${BACKUP_TARGETDIR}/$(echo ${TARGET} | sed "s#/#_#g").sql
        META=${TARGET}.gz.meta
        # echo -n " to $TARGET "
        sqlite3 "$DATABASE_FILE" .dump >"${TARGET}"
        fetchrc >/dev/null
        db._compressDumpfile "${TARGET}" && echo "$DATABASE_FILE" >"${META}"

        ls -l ${TARGET}*

      fi
    fi
  done
}

# restore database dump file into database
# param  string  database dump file (gzipped)
# param  string  optional: database to import; default: database is parsed from file
function restoreByFile(){
  sMyfile=$1
  sMyDb=$2

  if [ -f "${sMyDb}" ]; then
    color error
    echo ERROR: target file already exists. Remove or rename it first.
    ls -l "${sMyDb}"
    color reset
    rc=$rc+1
  else
    color cmd
    zcat "${sMyfile}" | sqlite3 "${sMyDb}"
    fetchrc
    if [ $myrc -eq 0 ]; then
      color ok
      echo OK, restore was successful
      color reset
      ls -l "${sMyDb}"
    else
      color error
      echo ERROR while restoring backup.
      color reset
    fi
  fi

}

# --------------------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------------------


# ----- check requirements
j_requireBinary "sqlite3"   1

if [ ! -f "$FILEDEFS" ]; then
  echo "INFO: file definitions $FILEDEFS do not exist."
  rc=$rc+1
fi


if [ $rc -ne 0 ]; then
  rc=0
  echo "SKIP: sqlite seems not to be here"
else
  if [ "$1" = "restore" ]; then
    echo
    shift 1
    restoreByFile $*

  else
    doSqliteBackup
  fi
fi

echo "__DB__$SERVICENAME INFO: $0 $* [$SERVICENAME] final returncode rc=$rc"

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