#!/bin/sh
# simplestPathTo - deterim simplest path between two directories
# Copyright (C) 2014 - 2018 FARGOS Development, LLC
# $Id$
originDir=`dirname $0`
fromDir=`pwd -P`
while test $# -gt 0
do
	case $1 in
	-h | -help | --help)
		printf "usage: %s [-from dir] toDir [...]\n" $0 >&2
		exit 1
		;;
	-f | -from | --from)
		fromDir=`readlink -m ${2} 2>/dev/null`
		if test -z "${fromDir}"
		then # failed, probably due to busybox implementation
			# fallback to -f
			fromDir=`readlink -f ${2}`
			if test -z "${fromDir}"
			then
				fromDir="${2}"
			fi
		fi
		shift
		;;
	-*)
		printf "%s: unrecognized option: %s\n" "${0}" "${1}" >&2
		exit 1
		;;
	*)
		dirList="${dirList}${dirList:+ }${1}"
		;;
	esac
	shift
done

for arg in ${dirList}
do
	d=`readlink -m ${arg} 2>/dev/null`
	if test -z "${d}"
	then # failed, probably due to busybox implementation
		# fallback to using -f
		d=`readlink -f ${arg}`
		if test -z "${d}"
		then
			d="${arg}"
		fi
		
	fi
	newPath=`echo ${d} | awk -v SRC="${fromDir}" -f "${originDir}/simplestPathTo.awk"`
	echo ${newPath}
done
