#!/usr/bin/env bash
# cronjob -- wrapper for cron jobs to set common environment

test "$XDG_RUNTIME_DIR"	|| export XDG_RUNTIME_DIR="/run/user/$(id -u)"
test "$SSH_AUTH_SOCK"	|| export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent"

. ~/.dotfiles/environ || exit

chronic='chronic'
useshell=0
krbcc=0
waitnet=0

while getopts 'cknv' OPT; do
	case $OPT in
	c)	useshell=1;;
	k)	krbcc=1;;
	n)	waitnet=1;;
	v)	chronic="";;
	\?)	exit 2;;
	esac
done; shift $((OPTIND-1))

if (( ! $# )); then
	echo "$0: no command given" >&2
	exit 2
fi

if (( useshell )); then
	set -- sh -c "$*"
fi

if (( krbcc )); then
	export KRB5CCNAME="FILE:/tmp/krb5cc_$(id -u)_cron"
	export KRB5_CLIENT_KTNAME="FILE:$HOME/.config/cron.keytab"
fi

if (( waitnet )); then
	nm-online -qx || exit 0
fi

nice $chronic "$@"
