diff options
author | Dan Nicholson <dbn.lists@gmail.com> | 2008-06-16 07:58:24 -0700 |
---|---|---|
committer | Dan Nicholson <dbn.lists@gmail.com> | 2008-06-16 07:59:02 -0700 |
commit | a11f47b8a13e7d5f626f9d9349a3adac262580bb (patch) | |
tree | 07c07e76e81fe49a35ecef80680199669cc430f8 | |
parent | 339b2f23accfab28dc1a255f75fb69b52657bf18 (diff) |
Create relative symlinks in /lib/initd
It's nice to create relative symlinks from /lib/initd to /sbin so that
the link path is always correct no matter where the filesystems are
mounted. To accomplish this, a script abs2rel.sh has been added which
converts two directory paths to a relative path. This is used to find
the relative path between /lib/initd and /sbin.
-rw-r--r-- | Makefile.am | 2 | ||||
-rwxr-xr-x | abs2rel.sh | 83 | ||||
-rw-r--r-- | src/Makefile.am | 15 |
3 files changed, 94 insertions, 6 deletions
diff --git a/Makefile.am b/Makefile.am index 128d61e..1f08a70 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1 +1,3 @@ SUBDIRS = lib src test + +dist_noinst_SCRIPTS = abs2rel.sh diff --git a/abs2rel.sh b/abs2rel.sh new file mode 100755 index 0000000..224cbb7 --- /dev/null +++ b/abs2rel.sh @@ -0,0 +1,83 @@ +#!/bin/sh + +# Determine the relative path between 2 directories. This is modeled +# on the perl File::Spec abs2rel function. + +if test "x$1" = x || test "x$2" = x; then + echo "error: two paths must be supplied to $0" >&2 + exit 1 +fi + +dir1=$1 +dir2=$2 + +# all shell builtins except for sed; let user override +SED=${SED-sed} + +# if either path is not absolute, prefix the current directory +case "$dir1" in +/*) ;; +*) dir1=$PWD/$dir1 ;; +esac +case "$dir2" in +/*) ;; +*) dir2=$PWD/$dir2 ;; +esac + +# strip multiple / and trailing / +dir1=`echo $dir1 | $SED 's,//*,/,g;s,\(.\)/$,\1,'` +dir2=`echo $dir2 | $SED 's,//*,/,g;s,\(.\)/$,\1,'` + +# special cases +if test "$dir1" = "$dir2"; then + # same directory, no relative path needed + exit 0 +elif test "$dir1" = /; then + # convert all components of $2 to .. + dir2=`echo $dir2 | $SED 's,/[^/]*,../,g'` + echo ${dir2%/} + exit 0 +elif test "$dir2" = /; then + # strip leading / of $1 to convert to relative + echo ${dir1#/} + exit 0 +fi + +# find the common prefix +pre1= +pre2= +while :; do + # find the leading dir component + pre1=${dir1#/} + pre1=/${pre1%%/*} + pre2=${dir2#/} + pre2=/${pre2%%/*} + + # if the leading components match, strip them from the suffixes + if test "$pre1" = "$pre2"; then + dir1=${dir1#$pre1} + dir2=${dir2#$pre2} + + # if either suffix is empty, we're done + if test "x$dir1" = x; then + break + elif test "x$dir2" = x; then + # dir1 is a subdir of dir2; convert to relative + dir1=${dir1#/} + break + fi + else + # we've found the common prefix + break + fi +done + +# convert remaining components of $dir2 to .. +rem= +if test "x$dir2" != x; then + rem=`echo $dir2 | $SED 's,/[^/]*,../,g'` + rem=${rem%/} +fi + +# print converted $dir2 + remainder of $dir1 +echo $rem$dir1 diff --git a/src/Makefile.am b/src/Makefile.am index 3fb14c5..0b38199 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,12 +14,15 @@ man_MANS = install_initd.8 remove_initd.8 libinitddir = $(exec_prefix)/lib/initd install-exec-hook: $(MKDIR_P) $(DESTDIR)$(libinitddir) - rm -f $(DESTDIR)$(libinitddir)/install_initd - $(LN_S) $(sbindir)/install_initd \ - $(DESTDIR)$(libinitddir)/install_initd - rm -f $(DESTDIR)$(libinitddir)/remove_initd - $(LN_S) $(sbindir)/remove_initd \ - $(DESTDIR)$(libinitddir)/remove_initd + @relpath=`$(SHELL) $(top_srcdir)/abs2rel.sh $(sbindir) $(libinitddir)` \ + || relpath=$(sbindir); \ + for prog in install_initd remove_initd; do \ + echo rm -f $(DESTDIR)$(libinitddir)/$$prog && \ + rm -f $(DESTDIR)$(libinitddir)/$$prog && \ + echo $(LN_S) $$relpath/$$prog $(DESTDIR)$(libinitddir)/$$prog && \ + $(LN_S) $$relpath/$$prog $(DESTDIR)$(libinitddir)/$$prog \ + || exit $?; \ + done uninstall-hook: rm -f $(DESTDIR)$(libinitddir)/install_initd |