Skip to content

Commit

Permalink
do not require a torrent file as argument when WatchDir is set.
Browse files Browse the repository at this point in the history
  • Loading branch information
Saizan committed May 3, 2010
1 parent 8519325 commit 794fb78
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/Combinatorrent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Control.Concurrent.STM
import Control.Monad
import Control.Monad.State

import Data.Maybe
import Data.List

import System.Environment
Expand Down Expand Up @@ -51,6 +52,17 @@ options =
, Option ['S'] ["statfile"] (ReqArg StatFile "FILE") "Choose a file to gather stats into"
]

(~=) :: Flag -> Flag -> Bool
Version ~= Version = True
Debug ~= Debug = True
LogFile _ ~= LogFile _ = True
WatchDir _ ~= WatchDir _ = True
StatFile _ ~= StatFile _ = True
_ ~= _ = False

flag :: Flag -> [Flag] -> Maybe Flag
flag x = find (x ~=)

progOpts :: [String] -> IO ([Flag], [String])
progOpts args = do
case getOpt Permute options args of
Expand All @@ -63,7 +75,7 @@ run (flags, files) = do
if Version `elem` flags
then progHeader
else case files of
[] -> putStrLn "No torrentfile input"
[] | isNothing $ flag (WatchDir "") flags -> putStrLn "No torrentfile input"
names -> progHeader >> download flags names

progHeader :: IO ()
Expand All @@ -72,20 +84,17 @@ progHeader = putStrLn $ "This is Combinatorrent \x2620 version " ++ version ++ "

setupLogging :: [Flag] -> IO ()
setupLogging flags = do
fLog <- case logFlag flags of
fLog <- case flag (LogFile "") flags of
Nothing -> streamHandler SIO.stdout DEBUG
Just (LogFile fp) -> fileHandler fp DEBUG
Just _ -> error "Impossible match"
when (Debug `elem` flags)
(updateGlobalLogger rootLoggerName
(setHandlers [fLog] . (setLevel DEBUG)))
where logFlag = find (\e -> case e of
LogFile _ -> True
_ -> False)

setupDirWatching :: [Flag] -> TorrentManager.TorrentMgrChan -> IO [Child]
setupDirWatching flags watchC = do
case dirWatchFlag flags of
case flag (WatchDir "") flags of
Nothing -> return []
Just (WatchDir dir) -> do
ex <- doesDirectoryExist dir
Expand All @@ -94,19 +103,13 @@ setupDirWatching flags watchC = do
else do putStrLn $ "Directory does not exist, not watching"
return []
Just _ -> error "Impossible match"
where dirWatchFlag = find (\e -> case e of
WatchDir _ -> True
_ -> False)

setupStatus :: [Flag] -> Status.StatusChannel -> TVar [Status.PStat] -> Child
setupStatus flags statusC stv =
case statFileFlag flags of
case flag (StatFile "") flags of
Nothing -> Worker $ Status.start Nothing statusC stv
Just (StatFile fn) -> Worker $ Status.start (Just fn) statusC stv
Just _ -> error "Impossible match"
where statFileFlag = find (\e -> case e of
StatFile _ -> True
_ -> False)

generatePeerId :: IO PeerId
generatePeerId = do
Expand Down

0 comments on commit 794fb78

Please sign in to comment.