module Development.Shake.FilePath(
module System.FilePath.Posix,
module System.FilePath,
dropDirectory1, takeDirectory1, normalise,
(-<.>),
toNative, (</>), combine,
exe
) where
import System.FilePath.Posix hiding
(normalise, (</>), combine
,searchPathSeparator, isSearchPathSeparator, splitSearchPath, getSearchPath)
import System.FilePath(searchPathSeparator, isSearchPathSeparator, splitSearchPath, getSearchPath)
import General.Base
import qualified System.FilePath as Native
infixr 5 </>
infixr 7 -<.>
dropDirectory1 :: FilePath -> FilePath
dropDirectory1 = drop 1 . dropWhile (not . Native.isPathSeparator)
takeDirectory1 :: FilePath -> FilePath
takeDirectory1 = takeWhile (not . Native.isPathSeparator)
normalise :: FilePath -> FilePath
normalise xs | a:b:xs <- xs, isWindows && sep a && sep b = '/' : f ('/':xs)
| otherwise = f xs
where
sep = Native.isPathSeparator
f o = deslash o $ (++"/") $ concatMap ('/':) $ reverse $ g 0 $ reverse $ split o
deslash o x
| x == "/" = case (pre,pos) of
(True,True) -> "/"
(True,False) -> "/."
(False,True) -> "./"
(False,False) -> "."
| otherwise = (if pre then id else tail) $ (if pos then id else init) x
where pre = sep $ head $ o ++ " "
pos = sep $ last $ " " ++ o
g i [] = replicate i ".."
g i ("..":xs) = g (i+1) xs
g i (".":xs) = g i xs
g 0 (x:xs) = x : g 0 xs
g i (x:xs) = g (i1) xs
split xs = if null ys then [] else a : split b
where (a,b) = break sep $ ys
ys = dropWhile sep xs
toNative :: FilePath -> FilePath
toNative = map (\x -> if Native.isPathSeparator x then Native.pathSeparator else x)
(</>) :: FilePath -> FilePath -> FilePath
(</>) = combine
(-<.>) :: FilePath -> String -> FilePath
(-<.>) = replaceExtension
combine :: FilePath -> FilePath -> FilePath
combine "." y = y
combine x ('.':'.':'/':y) = combine (takeDirectory x) y
combine x ('.':'/':y) = combine x y
combine x y = normalise $ Native.combine (toNative x) (toNative y)
exe :: String
#ifdef mingw32_HOST_OS
exe = "exe"
#else
exe = ""
#endif