#!/bin/sh

#
# abs2rel
#
# Script to determine the relative path between two directories.
#
# Copyright (c) D. J. Hawkey Jr. 2002
#
# Free for anybody's use in any application, in whole or in part,
# as long as the above copyright is retained. By using this stuff,
# you agree that I'm not responsible for anything that might go
# wrong, nor for any damages because something did.
#

# Inputs: $1 = source path, $2 = destination path
#         Inputs must exist if they aren't absolute
# Outputs: $BASEPATH = base of $1 and $2, $DESTPATH = transformed $2
abs2rel ()
{
	local CWD SRCPATH
                
	if [ "$1" != "/" -a "${1##*[^/]}" = "/" ]; then
		SRCPATH=${1%?}
	else
		SRCPATH=$1
	fi
	if [ "$2" != "/" -a "${2##*[^/]}" = "/" ]; then
		DESTPATH=${2%?}
	else
		DESTPATH=$2
	fi

	CWD=$PWD
	[ "${1%%[^/]*}" != "/" ] && cd $1 && SRCPATH=$PWD
	[ "${2%%[^/]*}" != "/" ] && cd $2 && DESTPATH=$PWD
	[ "$CWD" != "$PWD" ] && cd $CWD

	BASEPATH=$SRCPATH

	[ "$SRCPATH" = "$DESTPATH" ] && DESTPATH="." && return
	[ "$SRCPATH" = "/" ] && DESTPATH=${DESTPATH#?} && return

	while [ "$BASEPATH" != "${DESTPATH%${DESTPATH#$BASEPATH}}" ]; do
		BASEPATH=${BASEPATH%/*}
	done

	SRCPATH=${SRCPATH#$BASEPATH}
	DESTPATH=${DESTPATH#$BASEPATH}
	DESTPATH=${DESTPATH#?}
	while [ -n "$SRCPATH" ]; do
		SRCPATH=${SRCPATH%/*}
		DESTPATH="../$DESTPATH"
	done

	[ -z "$BASEPATH" ] && BASEPATH="/"
	[ "${DESTPATH##*[^/]}" = "/" ] && DESTPATH=${DESTPATH%?}
}

# for CLI - leave commented to source
abs2rel $1 $2
echo $BASEPATH
echo $DESTPATH
