module System.FriendlyPath where
import System.FilePath
import System.Directory
import Data.List
canonicalizePathFix :: FilePath -> IO FilePath
canonicalizePathFix f = do
de <- doesDirectoryExist (takeDirectory f)
if de then canonicalizePath f else makeAbsolute f
makeAbsolute :: FilePath -> IO FilePath
makeAbsolute f
| isAbsolute' f = return f
| otherwise = fmap (</> f) getCurrentDirectory
userToCanonPath :: FilePath -> IO String
userToCanonPath f = canonicalizePathFix =<< expandTilda f
recoverTilda :: FilePath -> IO String
recoverTilda path = do
home <- getHomeDirectory
return $ if home `isPrefixOf` path
then "~" ++ drop (length home) path
else path
canonicalizePath' :: FilePath -> IO String
canonicalizePath' f = recoverTilda =<< canonicalizePathFix f
expandTilda :: String -> IO FilePath
expandTilda "~" = getHomeDirectory
expandTilda s0 = do
home <- getHomeDirectory
return $ if (['~',pathSeparator] `isPrefixOf` s0) then home </> drop 2 s0 else s0
isAbsolute' :: String -> Bool
isAbsolute' ('~':_) = True
isAbsolute' p = isAbsolute p