#!/bin/bash

# Line below locks the script so cannot be run simultaneously (i.e. to prevent conflicts) -- however, does not work on home directory filesystem, only works on project space filesystem...

#[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :


if [ ! -n "$SINGULARITY_DIR" ]
then
	echo "SINGULARITY_DIR not set, reverting to default: /project/6007967/akhanf/singularity" >&2
	export SINGULARITY_DIR=/project/6007967/akhanf/singularity
fi

if [ ! "$#" = 1 ]
then
	echo "Usage: $0 {shub/docker}://org/repo:tag" >&2
	exit 1
fi

#input is shub URL

url=$1

suff=${url##*://}
tag=${suff#*:}
org=${suff%%/*}
name=${suff#*/}
name=${name%:*}

bra=master

#check version of singularity (2 vs 3)
version=`singularity --version`
version=${version##*\ }
version=${version:0:1}


hub=${url%%://*}
if [ "$hub" = "docker" ]
then
	if [ "$version" = "2" ]
	then
	cached=$SINGULARITY_DIR/bids-apps/${org}_${name}_${tag}.img
	dlpath=$SINGULARITY_DIR/${name}-${tag}.simg
	elif [ "$version" = "3" ]
	then
	cached=$SINGULARITY_DIR/bids-apps/${org}_${name}_${tag}.sif
	dlpath=$cached
	fi
elif [ "$hub" = "shub" ]
then
	if [ "$version" = "2" ]
	then
	cached=$SINGULARITY_DIR/${org}-${name}-${bra}-${tag}.simg
	dlpath=$cached
	elif [ "$version" = "3" ]
	then
	cached=$SINGULARITY_DIR/${org}-${name}-${bra}-${tag}.sif
	dlpath=$cached
	fi

else
	echo "Must use docker:// or shub://"  >&2
	exit 1
fi


if [ ! -e $cached ]
then
	export SINGULARITY_PULLFOLDER=$SINGULARITY_DIR
	export SINGULARITY_CACHEDIR=$SINGULARITY_DIR
	if [ "$version" = "2" ]
	then
	rm -f $dlpath
	singularity pull $url >&2 
	if [ "$?" = 0 ]
	then
		mv -v $dlpath $cached >&2
	fi
	elif [ "$version" = "3" ]
	then
	#singularity v3 can specify pull path directly
	singularity pull $cached $url >&2 
	fi

	if [ ! -e $cached ]
	then
		echo ERROR: could not pull image >&2
		exit 1
	fi
		
else

	#cached version exists, want to back it up and update it
	ext=${cached##*.}
	#get date of old container
	datestring=`ls -l --time-style='+%Y%m%d' $cached`
	datestring=${datestring%%\ $cached}
	datestring=${datestring##*\ }

	echo ext $ext >&2
	echo cached $cached >&2
	echo datestring $datestring >&2
	cached_bak=${cached%.*}.$datestring.$ext
	mv -n $cached $cached_bak
	cached=$(shub-cache $url)


fi


echo `realpath $cached`
exit 0
