aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/RemovePath.hs
blob: 50c4ca24f241f7d8c326b3aa84b874427e9d5beb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{-
 - Copyright 2015 Egor Tensin <Egor.Tensin@gmail.com>
 - This file is licensed under the terms of the MIT License.
 - See LICENSE.txt for details.
-}

module Main ( main ) where

import Control.Monad ( when )
import System.Console.GetOpt
import System.Environment ( getArgs, getProgName )
import System.Exit ( exitFailure, exitSuccess )
import System.IO ( hPutStr, stderr )

import qualified EnvUtils

main :: IO ()
main = do
  rawArgs <- getArgs
  case getOpt Permute optionDescription rawArgs of
    (actions, args, []) -> do
      options <- foldl (>>=) (return defaultOptions) actions      
      case args of
        [path] -> removePath path options
        _ -> invalidNumberOfArguments
    (_, _, errorMessages) -> exitWithUsageErrors errorMessages

removePath :: String -> Options -> IO ()
removePath path options = do
  let varName = name options
  userVal <- EnvUtils.queryFromRegistry EnvUtils.CurrentUserEnvironment varName
  let userValParts = EnvUtils.splitPaths userVal
  if path `elem` userValParts
    then do
      let newUserValParts = filter (/= path) userValParts
      EnvUtils.saveToRegistryWithPrompt EnvUtils.CurrentUserEnvironment varName $ EnvUtils.joinPaths newUserValParts
    else do
      when (global options) $ do
        globalVal <- EnvUtils.queryFromRegistry EnvUtils.AllUsersEnvironment varName
        let globalValParts = EnvUtils.splitPaths globalVal
        when (path `elem` globalValParts) $ do
          let newGlobalValParts = filter (/= path) globalValParts
          EnvUtils.saveToRegistryWithPrompt EnvUtils.AllUsersEnvironment varName $ EnvUtils.joinPaths newGlobalValParts

data Options = Options { name :: String
                       , global :: Bool }
                       deriving (Eq, Show)

defaultOptions :: Options
defaultOptions = Options { name = "PATH"
                         , global = False }

buildHelpMessage :: IO String
buildHelpMessage = do
  header <- buildHeader
  return $ usageInfo header optionDescription
    where
      buildHeader :: IO String
      buildHeader = do
        progName <- getProgName
        return $ "Usage: " ++ progName ++ " [OPTIONS...] PATH\nOptions:"

exitWithHelpMessage :: a -> IO b
exitWithHelpMessage _ = do
  helpMessage <- buildHelpMessage
  putStr helpMessage
  exitSuccess

exitWithUsageErrors :: [String] -> IO a
exitWithUsageErrors errorMessages = do
  hPutStr stderr $ concatMap ("Usage error: " ++) errorMessages
  helpMessage <- buildHelpMessage
  hPutStr stderr helpMessage
  exitFailure

invalidNumberOfArguments :: IO a
invalidNumberOfArguments = exitWithUsageErrors ["invalid number of arguments\n"]

optionDescription :: [OptDescr (Options -> IO Options)]
optionDescription = [
    Option "n" ["name"] (ReqArg (\s opts -> return opts { name = s }) "NAME") "set the variable name ('PATH' by default)",
    Option "g" ["global"] (NoArg $ \opts -> return opts { global = True }) "remove the path for all users",
    Option "h" ["help"] (NoArg exitWithHelpMessage) "show this message and exit"
  ]