1#!/bin/bash
2
3WIN32=false
4SEPARATOR=""
5ABSOLUTE=""
6UNIX=false
7done=false
8while [ $done = false ]; do
9    case "$1" in
10	-w)
11	    WIN32=true;
12	    SEPARATOR=backslash;
13	    shift;;
14	-d)
15	    WIN32=true;
16	    SEPARATOR=double;
17	    shift;;
18	-m)
19	    WIN32=true;
20	    SEPARATOR=slash;
21	    shift;;
22	-u)
23	    UNIX=true;
24	    shift;;
25	-a)
26	    ABSOLUTE="-a";
27	    shift;;
28
29	*)
30	    done=true;;
31    esac
32done
33
34if [ $WIN32 = false -a $UNIX = false ]; then
35    echo "Usage: $0 -m|-w|-d|-u [-a] <path>" >&2
36    exit 1;
37fi
38
39if [ -z "$1" ]; then
40    echo "Usage: $0 -m|-w|-u [-a] <path>" >&2
41    exit 1;
42fi
43
44case "$1" in
45    /*)
46        rel_input=false
47        ;;
48    *)
49        rel_input=true
50        ;;
51esac
52
53if [ $UNIX = true ]; then
54    # cl.exe loses //// in the beginning which make dependencies fail
55    # and sometimes lowercases the path
56    case $1 in
57        \\*wsl$\\*)
58            y=`echo $1 | sed 's,\\\\\+,/,g'`;
59            z=`echo $y | sed 's,^/wsl$/[^/]*\(.*\),\1,g' | sed 's, ,\\ ,g'`;
60            echo "$z";
61            ;;
62        *)
63            echo `wslpath -u $ABSOLUTE "$1" | sed 's, ,\\ ,g'`
64            ;;
65    esac
66else
67    #  wslpath have changed to always return absolute paths and
68    #  ensure the file/dir exists before translation
69
70    if [ $rel_input = true -a "$ABSOLUTE" = "" ]; then
71        case "$SEPARATOR" in
72	    slash)
73                echo $1
74                ;;
75            backslash)
76                echo "$1" | sed 's,/,\\,g'
77                ;;
78            double)
79                echo "$1" | sed 's,/,\\\\,g'
80                ;;
81        esac
82        exit 0
83    fi
84
85    # absolute input and/or absolute output
86
87    if [ -d "$1" ]; then
88        dir=$1;
89        case "$SEPARATOR" in
90	    slash)
91	        echo `wslpath -m $ABSOLUTE "$dir"`;
92	        ;;
93	    backslash)
94	        echo `wslpath -w $ABSOLUTE "$dir"`;
95	        ;;
96	    double)
97	        DOUBLE=`wslpath -w $ABSOLUTE "$dir" | sed 's,\\\\,\\\\\\\\,g'`;
98	        echo $DOUBLE
99	        ;;
100        esac
101        exit 0
102    else
103        dir=`dirname "$1"`
104        file=`basename "$1"`
105
106        case "$SEPARATOR" in
107	    slash)
108	        echo `wslpath -m $ABSOLUTE "$dir"`/$file;
109	        ;;
110	    backslash)
111	        echo `wslpath -w $ABSOLUTE "$dir"`\\$file;
112	        ;;
113	    double)
114	        DOUBLE=`wslpath -w $ABSOLUTE "$dir" | sed 's,\\\\,\\\\\\\\,g'`;
115	        echo $DOUBLE\\\\$file
116	        ;;
117        esac
118    fi
119fi
120