Add link to posts because IFTTT really hates feeds without links.
[SquirrelJME.git] / utils-dev / absolute.sh
blob68ad3841dc70f48aec4c0d4dfe34771c80ec20a1
1 #!/bin/sh
2 # ---------------------------------------------------------------------------
3 # Multi-Phasic Applications: SquirrelJME
4 # Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 # Copyright (C) Multi-Phasic Applications <multiphasicapps.net>
6 # ---------------------------------------------------------------------------
7 # SquirrelJME is under the GNU General Public License v3, or later.
8 # For more information see license.txt.
9 # ---------------------------------------------------------------------------
10 # DESCRIPTION: Calculates the absolute path of a file
12 # File to check
13 __check="$1"
15 # path is absolute, just needs . and .. removed
16 if echo "$__check" | grep '^\/' > /dev/null
17 then
18 __yuck="$__check"
20 # Get relative path from PWD
21 else
22 __yuck="$(pwd)/$__check"
25 # Split path by slashes and handle each line
26 # Need file due to argument stuff.
27 rm -f /tmp/$$.abs
28 touch /tmp/$$.abs
29 echo "$__yuck" | tr '/' '\n' | while read __seg
31 # If this segment is ".", ignore it
32 # Also ignore blank segments too
33 if [ "$__seg" = "." ] || [ -z "$__seg" ]
34 then
35 continue
37 # If this is .., remove the topmost path element
38 elif [ "$__seg" = ".." ]
39 then
40 sed 's/\/[^/]*$//g' < /tmp/$$.abs > /tmp/$$.abs2
41 mv /tmp/$$.abs2 /tmp/$$.abs
43 # Otherwise append to path
44 else
45 __part="$(cat "/tmp/$$.abs")"
46 printf '%s/%s' "$__part" "$__seg" > /tmp/$$.abs
48 done
50 # Blank result ends in /
51 __result="$(cat /tmp/$$.abs)"
52 rm -f /tmp/$$.abs
53 if [ -z "$__result" ]
54 then
55 echo "/"
56 else
57 echo "$__result"