#!/bin/sh -

# if set, archived files queried via Globus are appended to this file
list="${RESIDENT_LIST:-/var/cache/resident/recall-${USER:-$LOGNAME}}"

ressize=4096

# resident - output the archival/migration status of a file
# Steve Kinzler, steve@kinzler.com, Aug 26
# https://kinzler.com/me/home.html#unix

# Based on tape-recall by Milan Kupcevic <milan@nese.mghpcc.org> 2021-2026
# Also suitable for use as an "app" for the Globus Staging Connector
# See https://docs.globus.org/premium-storage-connectors/v5.4/posix-staging

verbose=; case "$1" in -v) verbose=$1; shift;; esac
file="${GLOBUS_STAGE_PATH-$1}"
globus=; [ -n "${GLOBUS_STAGE_PATH+defined}" ] && globus=true

case "$file,$1" in
,*|*,-h|*,--help)	cat <<EOF
usage: $0 [ -v ] [ file [ user ] ]
The file tested is GLOBUS_STAGE_PATH else the argument.
A user argument tests the file as that user (sudo permissions required).
EOF
			exit 1;;
esac

case "$globus,$2" in
,*[!-a-zA-Z0-9_]*)  echo "$0: archive: '$2' not a valid user" 1>&2; exit 2;;
,?*)	as="sudo -u $2";;
*)	as=;;
esac
# the first stat syncs the cache, then the others use it for efficiency
ftype="`$as stat --cached=never  --format=%F "$file"`" || exit 3
fsize="`$as stat --cached=always --format=%s "$file"`" || exit 3
bsize="`$as stat --cached=always --format=%B "$file"`" || exit 3
fblks="`$as stat --cached=always --format=%b "$file"`" || exit 3

case "$verbose" in ?*)	echo "ftype	$ftype"
			echo "fsize	$fsize"
			echo "bsize	$bsize"
			echo "fblks	$fblks";; esac

case "$ftype" in regular*file) ;; *)
    echo "$0: archive: '$file' not a regular file ($ftype)" 1>&2; exit 4;; esac
case "$fsize" in ''|*[!0-9]*)
    echo "$0: archive: invalid fsize for '$file' ($fsize)"  1>&2; exit 4;; esac
case "$bsize" in ''|*[!0-9]*|0)
    echo "$0: archive: invalid bsize for '$file' ($bsize)"  1>&2; exit 4;; esac
case "$fblks" in ''|*[!0-9]*)
    echo "$0: archive: invalid fblks for '$file' ($fblks)"  1>&2; exit 4;; esac

# if a Globus app, try to touch the access time to postpone
# any automated archival since Globus is about to read it
case "$globus" in ?*)
	touch -a "$file";; esac

if   [ $fsize -le "$ressize" ]; then	# assume tiny files are resident
	echo resident			# maybe because only in inode?
elif [ $fblks -ge "`echo $fsize/$bsize | bc`" ]; then
	echo resident
else
	echo archived
	case "$globus,$list" in true,?*)
		echo "$file" >> "$list" || exit 5;; esac
fi
exit 0

# 2026-08-25 TEST CASES:
# globus-xfer1$ resident -v /nfs/dataden/med-bfxcore-dataden/Vol_20170823_projects/Bernard_jbernard_MSU_edu_RS1_HI-1631/reads/Sample_65886_R1.fastq.gz trsaari
# ftype	regular file
# fsize	1516129740
# bsize	512
# fblks	4096
# archived
# globus-xfer1$ resident -v /nfs/dataden/lsa-bis3/DNA_V4/variants/data_before_10152023/Strassmann_V4DNA_GATKhardFilterPASS_mappability.tab.tmp.bed weishwu
# ftype	regular file
# fsize	97342078
# bsize	512
# fblks	380288
# resident
# globus-xfer1$ resident -v /nfs/dataden/coe-fanyue/yuchuw/vit_from_eng_hasdonwloaded.tar yuchuw
# ftype	regular file
# fsize	33204613120
# bsize	512
# fblks	129705600
# resident
