{-# OPTIONS -Wall #-}
module Shelly.Directory where

import System.IO.Error (modifyIOError, ioeSetLocation, ioeGetLocation)
import qualified System.Directory as FP

#ifdef mingw32_HOST_OS
import qualified System.Win32 as Win32
#else
import qualified System.Posix as Posix
#endif

createFileLink :: String -> String -> IO ()
createFileLink :: String -> String -> IO ()
createFileLink target :: String
target link :: String
link =
  (IOError -> String -> IOError
`ioeAddLocation` "createFileLink") (IOError -> IOError) -> IO () -> IO ()
forall a. (IOError -> IOError) -> IO a -> IO a
`modifyIOError` do
#ifdef mingw32_HOST_OS
    Win32.createSymbolicLink False target link
#else
    String -> String -> IO ()
Posix.createSymbolicLink String
target String
link
#endif

getSymbolicLinkTarget :: String -> IO String
getSymbolicLinkTarget :: String -> IO String
getSymbolicLinkTarget path :: String
path =
  (IOError -> String -> IOError
`ioeAddLocation` "getSymbolicLinkTarget") (IOError -> IOError) -> IO String -> IO String
forall a. (IOError -> IOError) -> IO a -> IO a
`modifyIOError` do
#ifdef mingw32_HOST_OS
    Win32.readSymbolicLink path
#else
    String -> IO String
Posix.readSymbolicLink String
path
#endif

ioeAddLocation :: IOError -> String -> IOError
ioeAddLocation :: IOError -> String -> IOError
ioeAddLocation e :: IOError
e loc :: String
loc = do
  IOError -> String -> IOError
ioeSetLocation IOError
e String
newLoc
  where
    newLoc :: String
newLoc = String
loc String -> String -> String
forall a. [a] -> [a] -> [a]
++ if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
Prelude.null String
oldLoc then "" else ":" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
oldLoc
    oldLoc :: String
oldLoc = IOError -> String
ioeGetLocation IOError
e