module System.FriendlyPath
( userToCanonPath
, expandTilda
, isAbsolute'
) where
import Control.Applicative
import System.FilePath
import System.PosixCompat.User (getUserEntryForName, homeDirectory)
import System.CanonicalizePath
import System.Directory hiding (canonicalizePath)
userToCanonPath :: FilePath -> IO String
userToCanonPath f = canonicalizePath =<< expandTilda f
expandTilda :: String -> IO FilePath
expandTilda ('~':path)
| (null path) || (head path == pathSeparator) = (++ path) <$> getHomeDirectory
| otherwise = let username = takeWhile (/= pathSeparator) path
dirname = drop (length username) path
in (normalise . (++ dirname) . homeDirectory) <$> getUserEntryForName username
expandTilda path = return path
isAbsolute' :: String -> Bool
isAbsolute' ('~':_) = True
isAbsolute' p = isAbsolute p