#!/bin/sh

if [ "$1" = "" -o "$1" = "--help" ]; then
  echo "description: convert any TOS/DOS/unified path to lowercase *nix path"
  echo "             (uses: sed)"
  echo
  echo "usage: $0 [--2env] _string_"
  echo
  echo " --2env   : print output from \"path2env _result_\""
  echo " _string_ : any valid path format"
  echo
  echo "examples: $0 u:\\d\\path"
  echo "            \"u:\\d\\path\" => \"/d/path\""
  echo "          $0 \"\$SLBPATH\""
  echo "            \"C:\\MINT\\SLB\" => \"/c/mint/slb\""
  echo "          $0 --2env \"\$PATH\""
  echo "            \"C:\\MINT;U:\bin\;D:\usr\bin\" => \"/c/mint /bin /d/usr/bin\""
  exit 0
fi

PASS_2_PATH2ENV="no"
if [ "$1" = "--2env" ]; then
  shift
  PASS_2_PATH2ENV="yes"
fi

NIX_PATH="$1"
#IS_TOS_PATH=`echo -E "$1" | sed '/[\;\,]/d'`
IS_ENV_PATH=`echo -E "$1" | sed '/\:\//d'`

if [ "$IS_ENV_PATH" = "" ]; then
  NIX_PATH=`echo -E "$NIX_PATH" | sed 's/\\:\\//;\\//g;s/\\\\/\\//g;s/\\(.*\\)/\\L\\1/;s/u://g;s/\\([a-z]\\)\\(:\\)/\\/\\1/g'`
else
  NIX_PATH=`echo -E "$NIX_PATH" | sed 's/\\\\/\\//g;s/\\(.*\\)/\\L\\1/;s/u://g;s/\\([a-z]\\)\\(:\\)/\\/\\1/g'`
fi

if [ "$PASS_2_PATH2ENV" = "yes" ]; then
  NIX_PATH=`echo -E "$NIX_PATH" | sed 's/\\;/ /g;s/,/ /g;s/  / /g;s/  / /g'`
fi
if [ "$PASS_2_PATH2ENV" = "no" -a "$IS_ENV_PATH" = "" ]; then
  NIX_PATH=`echo -E "$NIX_PATH" | sed 's/\\;\\//\\:\\//g'`
fi

echo -E "$NIX_PATH"

exit 0
