#!/bin/bash
#####################################################
#                                                   #
#   © 2000-2001 Fritz Mesedilla.                    #
#   Systems Administrator                           #
#   Summit Interactive, Inc. All rights reserved.   #
#   Last Updated: 04 September 2001                 #
#                                                   #
#   This will backup a specified folder.            #
#   This script is taken from the Data.fs script    #
#        that I also made.                          #
#                                                   #
#   Tested Versions:                                #
#   Zope 2.3.2 and ZEO 1.0b4                        #
#                                                   #
##################################################### 
#Declarations
#####################################################
ZOPE_LIB_PATH="/usr/lib/zope"
ZOPE_VAR_PATH="/var/lib/zope/instance/default"
JOBS_PATH="${ZOPE_PATH}/sysad"
STAMP=`date +%Y_%m_%d`
ZOPE_COMMAND="python ${ZOPE_LIB_PATH}/lib/python/ZPublisher/Client.py"


case $DEBUG in
  1) set -x
     ;;
  *)
     ;;
esac

error() { # juste pour usage
		echo $1 1>&2
}

usage() {
	error "usage : zope_export_object.sh [ -h for help ] -a 'auth.conf file' -u zope_address -i object_id -p export_path [-l logfile]"
	error "example : zope_export_object.sh -a /root/admin -u http://localhost:9673/fredz/ -i dossier -p /var/backup"
	error "auth.conf file should contains 'ZOPE_AUTH=user:password'"
}


while true
do
  case $1 in
    -a) auth_conf=$2
        shift 2
		;;
    -h) usage
		exit 0
		;;
	-l) logfile=$2
		shift 2
		;;
	-u) zope_address=$2
		shift 2
		;;
	-i) object_id=$2
		shift 2
		;;
	-p) export_path=$2
		shift 2
		;;
     *)
		break
		;;
  esac
done

if [ -z "$auth_conf" -o -z "$zope_address" -o -z "$object_id" -o -z "$export_path" -o -z "$logfile" ] 
then
	usage
	exit 1
fi

date=`date '+%d-%m-%Y'`
error() {
	echo "ERROR:zope_export_object.sh:$date: $1" 1>&2
	echo "ERROR:zope_export_object.sh:$date: $1" >> $logfile
}



. $auth_conf 

${ZOPE_COMMAND} -u ${ZOPE_AUTH} ${zope_address}/manage_exportObject id=${object_id} download:int=0 | grep "suc\+essfully exported"
if [ $? -ne 0 ]
then
	error "problem exporting "${object_id}
	exit 1
fi

#Creating Backup File
#########################################################
if [ -f "${ZOPE_VAR_PATH}/var/${object_id}.zexp" ]
then
{
	echo "Backing up file..."
	cp ${ZOPE_VAR_PATH}/var/${object_id}.zexp ${export_path}/${object_id}_${STAMP}.zexp
	echo "File: ${object_id}_${STAMP}.zexp created."
	echo ""
}
fi #Notify finished execution


exit 0

