#!/bin/bash -

# This script exports data from canto - to be set to run automatically on Wednesdays noon.

# Define canto-space root
CANTOSPACE="/data/export/canto-space"

# Define logs folder inside canto-space
LOGS="logs"

# Define log file
LOGFILE="${CANTOSPACE}/${LOGS}/canto_weekly_export.log"

# Redirect all output to logfile
exec &>> "${LOGFILE}"

# Create archive directory if required
EXPORT_DIR="${CANTOSPACE}/import_export"

EXPORT_FILENAME=canto_server_export_latest.json

# Function to log messages with date stamp
# e.g. log "Canto script completed successfully" generates something like 2020-05-20 10:24:37: Canto script completed successfully

function log () {
    local DATESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
    /bin/echo "${DATESTAMP}: ${1}"
}

# Create archive directory if required
EXPORT_DIR="${CANTOSPACE}/import_export"

EXPORT_FILENAME=canto_server_export_latest.json

log "Starting"

if ./canto/script/canto_docker  --non-interactive ./script/canto_export.pl canto-json --export-curator-names --export-approved > "${EXPORT_DIR}/${EXPORT_FILENAME}"; then
	
	log "Approved sessions exported into ${EXPORT_DIR} successful"
	
	if [[ -e "${EXPORT_DIR}/${EXPORT_FILENAME}" ]]; then

		# Create archive directory if required
		ARCHIVE="${CANTOSPACE}/archive"

		if [[ ! -d "${ARCHIVE}" ]]; then
   			log "Creating ${ARCHIVE}"
   			mkdir "${ARCHIVE}"
		fi

		EXPORT_FILENAME_ARCHIVE="canto_server_export_$(date +"%Y-%m-%d-%T").json"

		# Copying canto_server_export_latest.json to archive
		log "Copying ${EXPORT_FILENAME} to ${ARCHIVE}/${EXPORT_FILENAME_ARCHIVE}"

		cp "${EXPORT_DIR}/${EXPORT_FILENAME}" "${ARCHIVE}/${EXPORT_FILENAME_ARCHIVE}"
	
		log "Finished"

		exit
					
	else 
			
		log "But ${EXPORT_DIR}/${EXPORT_FILENAME} cannot be found - cannot continue"
			
		log "Finished"		
		exit
	fi

else
	
	log "Export failed."
		
	log "Finished"
	
	exit

fi
	
exit
