{-# LANGUAGE ScopedTypeVariables, EmptyDataDecls, MultiParamTypeClasses,
             DeriveDataTypeable, FlexibleInstances #-}
import Prelude hiding ( readFile )
import System.FilePath
import System.Directory
import Control.Applicative hiding ( (<|>) )

import Data.Time.Clock
import Data.Time.Format
import System.Locale

import Storage.Hashed.Plain
import Storage.Hashed.Darcs
import Storage.Hashed.Hash
import Storage.Hashed.Tree
import Storage.Hashed.AnchoredPath
import qualified Data.ByteString.Char8 as BSC

import CmdLib
import CmdLine
import System.Environment( getArgs )
import Restore
import Take
import Core

data List deriving Typeable
instance Command List Flag [Flag] where
  options _ = [Group "Basic options" (accept Repo <|> accept Root)]
  run _ f opts | (HashF r:_) <- [ r | Root r <- f ] = listdir repo (hdecode $ BSC.pack r)
               | otherwise = listrepo =<< getrepo repo
    where (Path repo:_) = [ r | Repo r <- f ]

commands = CmdGroup "Basic commands" (
              (cmd :: Restore) :&:
              (cmd :: Take) :&:
              (cmd :: List) :&:
              EndGroup
           ) :&&: EndCommands

main = getArgs >>= \opts -> dispatch commands opts []

{- main = do cfg <- cmdArgs help CmdLine.options
          print cfg
          case cfg of
            CmdLine.Take from to -> make from to
            CmdLine.List r "" -> listrepo =<< getrepo r
            CmdLine.List r h -> listdir r (hdecode $ BSC.pack h)
            CmdLine.Restore r t "" -> do x <- last `fmap` repo r
                                         putStrLn $ "restoring " ++ path x ++ ", at "
                                           ++ (BSC.unpack $ hencode $ root x)
                                         restore r t (root x)
            CmdLine.Restore r t h -> restore r t (hdecode $ BSC.pack h)
-}

