I wanted to make a relocatable Perl script. My first thought was to rewrite the relocatable-sh module for Perl, but of course that would be some work.
My second thought was to have a relocatable-sh wrapper. For most scripting languages, that would probably be necessary, and would work OK, although depending on the language, getting the variables in in a way that doesn't interfere with command-line arguments might be a problem. In Lua, for example, you can set variables on the command line.
With Perl, you can avoid a separate wrapper, because of its parsing tricks, and I used this to pass the relocated paths.
Maybe this is useful enough to be worth documenting? A collection of such templates for different languages could be quite helpful. I can feel a similar trick coming on for Lua to get it all in one file, but even for languages requiring a separate wrapper, all that's needed is to make bindir one of the relocated variables, and use that to find the "real" script.
#!/bin/sh
# -*- perl -*- this line sets the syntax mode for Emacs
# Perl script with relocatable header
@relocatable_sh@
if test "@RELOCATABLE@" = yes; then
exec_prefix="@exec_prefix@"
bindir="@bindir@"
orig_installdir="$sbindir" # see Makefile.am's *_SCRIPTS variables
func_find_curr_installdir # determine curr_installdir
func_find_prefixes
# Relocate the directory variables that we use.
gettext_dir=`
echo "$gettext_dir/" \
| sed -e "s%^${orig_installprefix}/%${curr_installprefix}/%" \
| sed -e 's,/$,,'`
fi
# The next line tells Perl to start parsing
#!perl
eval 'exec perl -x -wS $0 "$gettext_dir" ${1+"$@"}'
if 0;
my $gettext_dir = shift;
# "real" script starts here
print STDOUT "gettext_dir: $gettext_dir; ARGV[0]: $ARGV[0]\n";