| 1 | {-# LANGUAGE ScopedTypeVariables #-} |
| 2 | |
| 3 | module Util where |
| 4 | |
| 5 | import Control.Exception as E |
| 6 | import Data.List |
| 7 | import Param |
| 8 | import System.EasyFile |
| 9 | import System.Environment |
| 10 | import System.IO |
| 11 | |
| 12 | -- symbolic link to real link |
| 13 | |
| 14 | normalizePath :: FilePath -> IO FilePath |
| 15 | normalizePath path = dropTrailingPathSeparator <$> (expandHome path >>= realPath) |
| 16 | where |
| 17 | realPath sym = canonicalizePath sym `E.catch` \(_::SomeException) -> return sym |
| 18 | expandHome "" = expandHome defaultMailDir |
| 19 | expandHome ('~':cs) = do |
| 20 | mhome <- getHomeDirectory2 |
| 21 | case mhome of |
| 22 | Just home -> return $ home ++ cs |
| 23 | Nothing -> (++ cs) <$> getHomeDirectory |
| 24 | expandHome dir |
| 25 | | isRelative dir = expandHome (defaultMailDir </> dir) |
| 26 | | otherwise = return dir |
| 27 | |
| 28 | help :: String -> IO () |
| 29 | help message = getProgName >>= hPutStrLn stderr . (\prog -> "Usage: " ++ prog ++ " " ++ message) |
| 30 | |
| 31 | splitArgOpt :: [String] -> ([String],[String]) |
| 32 | splitArgOpt as = (filter isArg as, filter isOpt as) |
| 33 | where |
| 34 | isArg = not . isOpt |
| 35 | isOpt = ("-" `isPrefixOf`) |