module Config.FreeBSD.PortTools (
isPortmasterPresent,
isPkgToolsPresent,
isPkgNgPresent,
isPkgPresent,
upgradeWithPortmaster,
upgradeWithPortupgrade,
upgradeWithMake
) where
import System.Process
import System.Exit
import System.Directory
import Control.Exception
checkExitCode :: String -> [String] -> IO Bool
checkExitCode nm args = readProcessWithExitCode nm args "" >>= \(status, _, _) -> return (status == ExitSuccess)
checkPortmaster :: IO Bool
checkPortmaster = checkExitCode "portmaster" ["--help"]
checkPortupgrade :: IO Bool
checkPortupgrade = checkExitCode "portupgrade" ["--help"]
checkPkg :: IO Bool
checkPkg = checkExitCode "pkg" ["help"]
checkOldPkg :: IO Bool
checkOldPkg = checkExitCode "pkg_version" ["-h"]
handler :: SomeException -> IO Bool
handler _ = return False
isPortmasterPresent :: IO Bool
isPortmasterPresent = catch checkPortmaster handler
isPkgToolsPresent :: IO Bool
isPkgToolsPresent = catch checkPortupgrade handler
isPkgNgPresent :: IO Bool
isPkgNgPresent = catch checkPkg handler
isPkgPresent :: IO Bool
isPkgPresent = catch checkOldPkg handler
upgradeWithPortmaster :: String -> IO ()
upgradeWithPortmaster name = callProcess "portmaster" [name]
upgradeWithPortupgrade :: String -> IO ()
upgradeWithPortupgrade name = callProcess "portupgrade" [name]
upgradeWithMake :: String -> IO ()
upgradeWithMake name = readProcess "find" ["/usr/ports", name] ""
>>= setCurrentDirectory
>> callProcess "make" ["config-recursive"]
>> callProcess "make" ["install", "clean"]