                                                                                                                                                                        
                                                                                                                                                                                      
                                                                                                                                                                                      
data AppOperation = NoOperation | QueryOperation | SyncOperation                                                                                                                      
                                                                                                                                                                                      
data AppState = AppState{                                                                                                                                                             
      dbpath  :: String,                                                                                                                                                              
      operation :: AppOperation                                                                                                                                                       
    }                                                                                                                                                                                 
                                                                                                                                                                            
                                                                                                                                                                                      
                                                                                                                                                                                      
                                                                                                                                                                                      
argsselect' (state,pkgs) ('S':s) =  (state {operation=SyncOperation} , pkgs  )                                                                                                        
argsselect' (state,pkgs) ('Q':s) =  (state {operation=QueryOperation} , pkgs  )                                                                                                       
                                                                                                                                                                                      
                                                                                                                                                                                      
argsselect (state,pkgs) ('-':s)  =  argsselect' (state,pkgs ) s                                                                                                                       
argsselect (state,pkgs) s        =  (state,pkgs++[s] )                                                                                                                                
                                                                                                                                                                                      
main :: IO ()                                                                                                                                                                         
main =  do                                                                                                                                                                            
  args <- getArgs                                                                                                                                                                     
                                                                                                                                                                                      
  let (state,pkgs) = foldl argsselect (AppState {dbpath="/var/lib/pacman/",operation=NoOperation},[]) args                                                                            
                                                                                                                                                                                      
  case  (operation state) of                                                                                                                                                          
    SyncOperation ->  getDirectoryContents'  ((dbpath state)++"sync/")  >>=  mapM_ (\x -> listPkg state ("sync/"++x) >>= mapM_ printPkg)                                              
                                                                                                                                                                                      
    QueryOperation ->  listPkg state "local/" >>= mapM_ printPkg                                                                                                                      
    _ -> putStrLn $ "Usage: m <operation> [...] \n\t-S sync  [packages]\n\t-Q query [packages]"                                                                                       
                                                                                                                                                                                      
                                                                                                                                                                                      
                  
