{- user info
 -
 - Copyright 2012 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-tabs #-}

module Utility.UserInfo (
	myHomeDir,
	myUserName,
	myUserGecos,
) where

import Utility.Env.Basic
import Utility.Exception
#ifndef mingw32_HOST_OS
import Utility.Data
import Control.Applicative
#endif

import System.Posix.User
#if MIN_VERSION_unix(2,8,0)
import System.Posix.User.ByteString (UserEntry)
#endif
import Prelude

{- Current user's home directory.
 -
 - getpwent will fail on LDAP or NIS, so use HOME if set. -}
myHomeDir :: IO FilePath
myHomeDir :: IO FilePath
myHomeDir = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. FilePath -> a
giveup forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [FilePath]
-> (UserEntry -> FilePath) -> IO (Either FilePath FilePath)
myVal [FilePath]
env UserEntry -> FilePath
homeDirectory
  where
#ifndef mingw32_HOST_OS
	env :: [FilePath]
env = [FilePath
"HOME"]
#else
	env = ["USERPROFILE", "HOME"] -- HOME is used in Cygwin
#endif

{- Current user's user name. -}
myUserName :: IO (Either String String)
myUserName :: IO (Either FilePath FilePath)
myUserName = [FilePath]
-> (UserEntry -> FilePath) -> IO (Either FilePath FilePath)
myVal [FilePath]
env UserEntry -> FilePath
userName
  where
#ifndef mingw32_HOST_OS
	env :: [FilePath]
env = [FilePath
"USER", FilePath
"LOGNAME"]
#else
	env = ["USERNAME", "USER", "LOGNAME"]
#endif

myUserGecos :: IO (Maybe String)
-- userGecos is not available on Windows.
#if defined(mingw32_HOST_OS)
myUserGecos = return Nothing
#else
myUserGecos :: IO (Maybe FilePath)
myUserGecos = forall a b. Either a b -> Maybe b
eitherToMaybe forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [FilePath]
-> (UserEntry -> FilePath) -> IO (Either FilePath FilePath)
myVal [] UserEntry -> FilePath
userGecos
#endif

myVal :: [String] -> (UserEntry -> String) -> IO (Either String String)
myVal :: [FilePath]
-> (UserEntry -> FilePath) -> IO (Either FilePath FilePath)
myVal [FilePath]
envvars UserEntry -> FilePath
extract = [FilePath] -> IO (Either FilePath FilePath)
go [FilePath]
envvars
  where
	go :: [FilePath] -> IO (Either FilePath FilePath)
go [] = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall {b}. Either FilePath b
envnotset) (forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. UserEntry -> FilePath
extract) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO (Either SomeException UserEntry)
get
	go (FilePath
v:[FilePath]
vs) = forall b a. b -> (a -> b) -> Maybe a -> b
maybe ([FilePath] -> IO (Either FilePath FilePath)
go [FilePath]
vs) (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< FilePath -> IO (Maybe FilePath)
getEnv FilePath
v
#ifndef mingw32_HOST_OS
	-- This may throw an exception if the system doesn't have a
	-- passwd file etc; don't let it crash.
	get :: IO (Either SomeException UserEntry)
get = forall (m :: * -> *) a.
MonadCatch m =>
m a -> m (Either SomeException a)
tryNonAsync forall a b. (a -> b) -> a -> b
$ UserID -> IO UserEntry
getUserEntryForID forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO UserID
getEffectiveUserID
#else
	get = return envnotset
#endif
	envnotset :: Either FilePath b
envnotset = forall a b. a -> Either a b
Left (FilePath
"environment not set: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> FilePath
show [FilePath]
envvars)