main :: IO ()
main = do
    getProgName >>= \case
        "yx" -> yxMain
        other -> getArgs >>= yxExec other
  where
    yxExec alias args = do
        lookupEnv "YX_VERSION" >>= \case
            Nothing -> error "Not in an YX environment."
            Just _ -> pure ()

        envName <- lookupEnv "YX_ENVIRONMENT" >>= \case
            Nothing -> error "YX environment variables are corrupted."
            Just x -> pure x

        root <- lookupEnv "YX_PROJECT_ROOT" >>= \case
            Nothing -> error "YX environment variables are corrupted."
            Just x -> pure x

        projCfg <- readCachedYxConfig
            $ yxStuffFile root CachedStuff "config.bin"

        projEnv <- case ProjectConfig.getEnvironment projCfg $ fromString envName of
            Nothing -> error "YX configuration corrupted."
            Just e -> pure e

        case HashMap.lookup (fromString alias) (Environment._bin projEnv) of
            Nothing -> errInvalidAlias alias
            Just Executable{..}
              | _type /= Command -> errInvalidAlias alias
              | otherwise        -> case words (Text.unpack _command) of
                [] -> error $ alias <> ": Invalid configuration of this alias."
                cmd : cmdArgs -> executeFile cmd True (cmdArgs <> args) Nothing

    errInvalidAlias cmd = error $ cmd <> ": YX called with invalid alias."
main :: IO ()
main = do
    getProgName >>= \case
        "yx" -> yxMain
        other -> getArgs >>= yxExec other
  where
    yxExec alias args = do
        lookupEnv "YX_VERSION" >>= \case
            Nothing -> error "Not in an YX environment."
            Just _ -> pure ()

        envName <- lookupEnv "YX_ENVIRONMENT" >>= \case
            Nothing -> error "YX environment variables are corrupted."
            Just x -> pure x

        root <- lookupEnv "YX_PROJECT_ROOT" >>= \case
            Nothing -> error "YX environment variables are corrupted."
            Just x -> pure x

        projCfg <- readCachedYxConfig
            $ yxStuffFile root CachedStuff "config.bin"

        projEnv <- case ProjectConfig.getEnvironment projCfg $ fromString envName of
            Nothing -> error "YX configuration corrupted."
            Just e -> pure e

        case HashMap.lookup (fromString alias) (Environment._bin projEnv) of
            Nothing -> errInvalidAlias alias
            Just Executable{..}
              | _type /= Command -> errInvalidAlias alias
              | otherwise        -> case words (Text.unpack _command) of
                [] -> error $ alias <> ": Invalid configuration of this alias."
                cmd : cmdArgs -> executeFile cmd True (cmdArgs <> args) Nothing

    errInvalidAlias cmd = error $ cmd <> ": YX called with invalid alias."
main = do
    getProgName >>= \case
        "yx" -> yxMain
        other -> getArgs >>= yxExec other
  where
    yxExec alias args = do
        lookupEnv "YX_VERSION" >>= \case
            Nothing -> error "Not in an YX environment."
            Just _ -> pure ()

        envName <- lookupEnv "YX_ENVIRONMENT" >>= \case
            Nothing -> error "YX environment variables are corrupted."
            Just x -> pure x

        root <- lookupEnv "YX_PROJECT_ROOT" >>= \case
            Nothing -> error "YX environment variables are corrupted."
            Just x -> pure x

        projCfg <- readCachedYxConfig
            $ yxStuffFile root CachedStuff "config.bin"

        projEnv <- case ProjectConfig.getEnvironment projCfg $ fromString envName of
            Nothing -> error "YX configuration corrupted."
            Just e -> pure e

        case HashMap.lookup (fromString alias) (Environment._bin projEnv) of
            Nothing -> errInvalidAlias alias
            Just Executable{..}
              | _type /= Command -> errInvalidAlias alias
              | otherwise        -> case words (Text.unpack _command) of
                [] -> error $ alias <> ": Invalid configuration of this alias."
                cmd : cmdArgs -> executeFile cmd True (cmdArgs <> args) Nothing

    errInvalidAlias cmd = error $ cmd <> ": YX called with invalid alias."