From 09b3231f573b31bde81dd7484b00d581263f2b99 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Mon, 16 Feb 2015 10:57:52 +0530
Subject: [PATCH] more refactoring

---
 src/Bencode.hs  |  2 +-
 src/Main.hs     | 14 +++++++++++++-
 src/Metainfo.hs |  3 ---
 3 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/Bencode.hs b/src/Bencode.hs
index 16e4a2b..e07799e 100644
--- a/src/Bencode.hs
+++ b/src/Bencode.hs
@@ -133,6 +133,6 @@ encode (Bstr bs) = let s = BC.unpack bs
 encode (Bint i) = "i" ++ show i ++ "e"
 encode (Blist xs) = "l" ++ encodeList xs ++ "e"
   where encodeList [] = ""
-        encodeList (x:xs) = encode x ++ encodeList xs
+        encodeList (x:xs') = encode x ++ encodeList xs'
 encode (Bdict d) = "d" ++ encodeDict d ++ "e"
   where encodeDict m = concat [encode k ++ encode (m M.! k) | k <- M.keys m]
diff --git a/src/Main.hs b/src/Main.hs
index f5a0a50..10aa287 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -1,6 +1,7 @@
 module Main where
 
 import System.Environment (getArgs)
+import System.Exit
 import qualified Data.ByteString.Char8 as BC
 import qualified Bencode as Benc
 import qualified Metainfo as MInfo
@@ -14,10 +15,21 @@ printError e = putStrLn $ "parse error: " ++ show e
 genPeerId :: String
 genPeerId = "-HS0001-20150215"
 
+exit :: IO BC.ByteString
+exit = exitWith ExitSuccess
+
+usage :: IO ()
+usage = putStrLn "usage: deluge torrent-file"
+
+parse :: [String] -> IO (BC.ByteString)
+parse [] = usage >> exit
+parse [a] = BC.readFile a
+parse _ = exit
+
 main :: IO ()
 main = do
   args <- getArgs
-  torrentStr <- BC.readFile (head args)
+  torrentStr <- parse args
   case (Benc.decode torrentStr) of
    Right d -> case (MInfo.mkMetaInfo d) of
                Nothing -> putStrLn "parse error"
diff --git a/src/Metainfo.hs b/src/Metainfo.hs
index 47a134c..4c614d6 100644
--- a/src/Metainfo.hs
+++ b/src/Metainfo.hs
@@ -3,9 +3,6 @@ module Metainfo where
 import qualified Bencode as Benc
 import qualified Data.ByteString.Char8 as BC
 import qualified Data.Map as M
-import qualified Crypto.Hash as H
-import qualified Crypto.Hash.SHA1 as SHA1
-import Data.Time.Clock
 
 -- only single file mode supported for the time being.
 data Info = Info { pieceLength :: !Integer
-- 
2.45.2