#!/bin/bash

# Command line tutorial: Moving a collection from Greenstone to DSpace
# https://stackoverflow.com/questions/6482377/check-existence-of-input-argument-in-a-bash-shell-script
if [ -z "$1" ]; then
    echo "Usage: $0 http://<host:port>/greenstone3/"
    echo "   e.g. $0 http://localhost:8383/greenstone3/"
    exit -1
fi

pushd $GSDL3SRCHOME
source ./gs3-setup.sh

# expects $1 to be something like http://localhost:8383/greenstone3/
gs_base_url=$1

# Need to store cache_folder in a variable to get tilde expansion to work
# https://unix.stackexchange.com/questions/709882/how-does-the-tilde-expansion-work-within-a-shell-variable
cache_folder=~/.gli/cache/

#sed -i 's/#check_certificate = off/check_certificate = off/' gs2build/bin/linux/wgetrc

perl -S downloadfrom.pl -download_mode OAI -url ${gs_base_url}oaiserver -set backdrop -max_records 15 -no_check_certificate -get_doc -get_doc_exts "jpg,pdf" -cache_dir $cache_folder

result=$?
if [[ "$result" -eq 0 ]]; then
    echo ""
    echo ""
    echo "@@@ Download complete. Check the cache folder at $cache_folder for downloads."
    echo ""
fi
popd
exit $result
