module Main ( main ) where

import Control.Monad.Error
import System.Environment ( getArgs )
import System.FilePath

import Pwstore.Commands.Add
import Pwstore.Commands.Show
import Pwstore.Crypt
import Pwstore.Data
import Pwstore.Opts.Show
import Pwstore.PWS
import Pwstore.Util


main :: IO ()
main = do
   -- Just for development
   let dbPath = "util" </> "resources" </> "testEncrypt" <.> "pws"

   args <- getArgs

   parsedResult <- runPWS $ case args of
      ("debuginit":_) -> do
         pw <- liftIO $ getPasswdV ("password: ", "repeat password: ")
         encryptToFile dbPath pw sampleData
         return "debuginit: saved db successfully"

      ("show":opts) -> do
         parsedOpts <- liftIO $ parseShowOpts opts
         pw <- liftIO $ getPasswd "password: "
         accounts <- decryptFromFile dbPath pw
         liftIO $ printAccounts accounts
         throwErrorE $ "Not yet implemented. Your input: "
            ++ show parsedOpts

      ("add":_) -> do
         newAccount <- getAccount
         throwErrorE $ "Not yet implemented. Your input: "
            ++ show newAccount

      _ -> throwErrorE $ "Unrecognized input: " ++ show args

   -- We're getting a string status message back, whether it's bad 
   -- or good. Just display it.
   putStrLn $ either id id parsedResult


