#!/usr/bin/env bash
#
# The "fix-sage-local-bin.sh" replaces "hardcoded" paths in some relevant
# "scripts" in the "${SAGE_LOCAL}/bin" subdirectory with SAGE_* "relative"
# paths (git-cvsserver *-config *-makefile gap gphelp maxima [rx]maxima R).
#
# Before executing "fix-sage-local-bin.sh" ... cd "${SAGE_LOCAL}/bin"
#
# I general, it is a good idea to build Sage in its "final" location.
# If you build Sage somewhere and then copy or move the entire build
# tree to another location, then you must run "./sage" once so that
# various "hardcoded" locations get updated (you will see a message
# saying "Changing/Updating various hardcoded paths...").
# However, at least up to "sage-6.4.1", this will NOT "fix" paths in
# "scripts" which reside in the "${SAGE_LOCAL}/bin" subdirectory.
# That is why you need to execute "fix-sage-local-bin.sh", too.
#
# Note: the same problem appears if you install Sage from a pre-built
# binary tarball in a location (i.e. a subdirectory) which does not
# exactly follow the one which was used to build this binary distribution.
#
for file in git-cvsserver *-config *-makefile gap gphelp maxima [rx]maxima R ;
do
# check that the "file" exists and is a "script"
if [[ -e "${file}" && -n "`sed -n -e '{ 1 { /^#![ \t]*\/.*/ p } ; q }' "${file}"`" ]] ; then
# replace "hardcoded" paths with SAGE_* "relative" ones
# e.g. "/var/lib/buildbot/...", "/scratch/buildbot/...",
# "/home/buildbot/...", "/home/wstein/...", "/opt/SAGE/..."
sed --follow-symlinks -i -e '{
s#\([=ILR,'\''" \t]\)\(/var/[^[:space:]]*/local\)\([/'\''" \t]\|$\)#\1\$SAGE_LOCAL\3#g
s#\([=ILR,'\''" \t]\)\(/var/[^[:space:]]*/spkg/build\)\([/'\''" \t]\|$\)#\1\$SAGE_PACKAGES/build\3#g
s#\([=ILR,'\''" \t]\)\(/scratch/[^[:space:]]*/local\)\([/'\''" \t]\|$\)#\1\$SAGE_LOCAL\3#g
s#\([=ILR,'\''" \t]\)\(/scratch/[^[:space:]]*/spkg/build\)\([/'\''" \t]\|$\)#\1\$SAGE_PACKAGES/build\3#g
s#\([=ILR,'\''" \t]\)\(/home/[^[:space:]]*/local\)\([/'\''" \t]\|$\)#\1\$SAGE_LOCAL\3#g
s#\([=ILR,'\''" \t]\)\(/home/[^[:space:]]*/spkg/build\)\([/'\''" \t]\|$\)#\1\$SAGE_PACKAGES/build\3#g
s#\([=ILR,'\''" \t]\)\(/opt/[^[:space:]]*/local\)\([/'\''" \t]\|$\)#\1\$SAGE_LOCAL\3#g
s#\([=ILR,'\''" \t]\)\(/opt/[^[:space:]]*/spkg/build\)\([/'\''" \t]\|$\)#\1\$SAGE_PACKAGES/build\3#g
}' "${file}"
fi
done
# end of file fix-sage-local-bin.sh by Silesius Anonymus