Permalink
Browse files

hs-port.hs: Add support for being called from command line

  • Loading branch information...
pgj committed Jun 10, 2012
1 parent 89a1185 commit 9a522f7c61f988d713e34653de2f60d00e1e5e69
Showing with 22 additions and 0 deletions.
  1. +22 −0 hs-ports.hs
View
@@ -1,10 +1,14 @@
import Prelude as P
import Data.List
+import Data.Maybe
+import System.Directory
+import System.Environment
import System.FilePath.Posix
import qualified Data.Text as T
import qualified Data.Text.IO as T
+portsDir = "/usr/ports"
hackageMk = "lang/ghc/bsd.hackage.mk"
hsPorts portsdir = do
@@ -17,3 +21,21 @@ hsPorts portsdir = do
printHsPorts portsdir =
hsPorts portsdir >>= putStrLn . unlines . sort
+
+getPortsDir = do
+ env <- getEnvironment
+ return $ maybe portsDir id (lookup "PORTSDIR" env)
+
+bailOut s = putStrLn $ "ERROR: " ++ s
+
+-- A main function for running from a shell.
+main = do
+ args <- getArgs
+ case args of
+ ["ports"] -> do
+ pdir <- getPortsDir
+ mkThere <- doesFileExist (pdir </> hackageMk)
+ if mkThere
+ then printHsPorts pdir
+ else bailOut "No bsd.hackage.mk found."
+ _ -> bailOut "Nissing or invalid query."

0 comments on commit 9a522f7

Please sign in to comment.