#!/bin/bash
#!/bin/sh
# Use /bin/bash for exec -a

# perl(1) wrapper
# Copyright 2015-2023 Rivoreo
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


REAL_PERL=/usr/lib/perl5/5.8.0/perl
argv0="${0##*/}"
set +H > /dev/null 2>&1
print_escaped() {
	local q
	case "$1" in
		"")
			printf '""'
			return
			;;
		*[\~\#\&\*\?\(\)\{\}\<\>\|\;\ \	\'\[\]]*)
			q=1
			printf \"
			;;
		*)
			q=
			;;
	esac
	printf %s "$1" | sed -E "s/([\\\`\$\"])/\\\\\\1/g"
	[ -n "$q" ] && printf \"
}
{
	printf '[%s] UID=%s PWD=' "`date +%F.%T.%Z`" "$UID"
	print_escaped "$PWD"
	printf " %s" "$0"
	for a in "$@"; do
		printf \ 
		print_escaped "$a"
	done
	printf ' (#=%s)\n' $#
} >> /var/log/perl.log
case "$argv0" in
	perl*)
		;;
	*)
		exec -a "$argv0" $REAL_PERL "$@"
		;;
esac
case "$1" in
	--)
		shift
		;;
	-*)
		exec -a "$argv0" $REAL_PERL "$@"
		;;
esac
[ $# -lt 1 ] || [ "$1" = - ] && exec -a "$argv0" $REAL_PERL
if [ ! -e "$1" ]; then
	printf "Can't open perl script \"%s\": No such file or directory\\n" "$1" 1>&2
	exit 2
fi
if [ "`dd bs=1 count=3 if=\"$1\" 2> /dev/null`" = "`printf '\\xef\\xbb\\xbf'`" ]; then
	script="$1"
	shift
	{ printf \\000; cat "$script"; } | $REAL_PERL -- - "$@"
	exit
fi
if sed 1!d "$1" | grep -Eq ^\#!.+; then
	script="$1"
	shift
	sed 1c\# "$script" | $REAL_PERL -- - "$@"
	exit
fi
exec -a "$argv0" $REAL_PERL -- "$@"
