Skip to content

Instantly share code, notes, and snippets.

@phadej
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phadej/b4e63bc0a5b9b0593134 to your computer and use it in GitHub Desktop.
Save phadej/b4e63bc0a5b9b0593134 to your computer and use it in GitHub Desktop.
# if we have haskell installed
command -v runhaskell > /dev/null 2>&1
if [ $? ]; then
# Embedded haskell
TMP=`mktemp /tmp/orderpath.XXXXXX`
cat > $TMP <<'EOF'
module Main where
import System.Environment (getEnvironment)
import Data.Function (on)
import Data.List.Split (splitOn)
import Data.List (intercalate, sortBy, isPrefixOf)
sortPath :: String -> String
sortPath = intercalate ":" . map snd . sortBy (compare `on` metric) . zip [0, 1 ..] . splitOn ":"
where metric (i, p) = i + if "/usr/local" `isPrefixOf` p then 0 else 1000
defaultPath :: String
defaultPath = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
main :: IO ()
main = do
env <- getEnvironment
putStrLn $ maybe defaultPath sortPath $ lookup "PATH" env
EOF
# Run, and if succeed assign new PATH
SORTEDPATH=`runhaskell $TMP`
if [ $? ]; then
export PATH=$SORTEDPATH
fi
# cleanup
rm $TMP
fi
module Main where
import System.Environment (getEnvironment)
import Data.Function (on)
import Data.List.Split (splitOn)
import Data.List (intercalate, sortBy, isPrefixOf)
sortPath :: String -> String
sortPath = intercalate ":" . map snd . sortBy (compare `on` metric) . zip [0, 1 ..] . splitOn ":"
where metric (i, p) = i + if "/usr/local" `isPrefixOf` p then 0 else 1000
defaultPath :: String
defaultPath = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
main :: IO ()
main = do
env <- getEnvironment
putStrLn $ maybe defaultPath sortPath $ lookup "PATH" env
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment