tagDependency :: Bool -> [(Text, [Text])] -> FilePath -> StackTag (Either (PkgName, String) FilePath)
tagDependency nocache stkpaths dep = do

    let snapRoot
            | Just (sr : _) <- lookup "snapshot-install-root" stkpaths = sr
            | otherwise = error ("[tag:error] error tagging "
                               ++ dep
                               ++ ". "
                               ++ "No 'snapshot-install-root' found, aborting.")
        dir          = T.unpack snapRoot ++ "/packages" ++ "/" ++ dep
        tagFile      = dir ++ "/TAGS"

    -- HACK as of Aug 5 2015, `stack unpack` can only download sources
    -- into the current directory. Therefore, we move the source to
    -- the correct snapshot location. This could/should be fixed in
    -- the stack source (especially since --haddock has similar
    -- behavior). A quick solution to avoid this might be to run the
    -- entire function in the target directory
    _ <- io $ readProcess "rm" ["--preserve-root", "-rf", dep] []

    -- error (show dep)

    exists <- io $ doesDirectoryExist dir
    tagged <- io $ doesFileExist tagFile

    unless exists $ void $ do
        io $ createDirectoryIfMissing True dir
        p $ "[tag:download] " ++ dep
        (ec,stout,_) <- runStk ["unpack", dep]
        case ec of
          ExitFailure _ -> void $ do
              p $ "[tag:download] failed to download " ++ dep ++ " - " ++ stout
          ExitSuccess   -> void $ do
              p $ "[tag:download] cp " ++ dep ++ " to snapshot source cache in " ++ dir
              io $ readProcess "mv" [dep,dir] []

    if tagged && nocache
       then do p $ "[tag:nocache] " ++ dep
               return (Right tagFile)
       else do p $ "[tag:cache] " ++ dep
               runTagger (TagCmd Hasktags ETags tagFile dir dep)
tagDependency :: Bool -> [(Text, [Text])] -> FilePath -> StackTag (Either (PkgName, String) FilePath)
tagDependency nocache stkpaths dep = do

    let snapRoot
            | Just (sr : _) <- lookup "snapshot-install-root" stkpaths = sr
            | otherwise = error ("[tag:error] error tagging "
                               ++ dep
                               ++ ". "
                               ++ "No 'snapshot-install-root' found, aborting.")
        dir          = T.unpack snapRoot ++ "/packages" ++ "/" ++ dep
        tagFile      = dir ++ "/TAGS"

    -- HACK as of Aug 5 2015, `stack unpack` can only download sources
    -- into the current directory. Therefore, we move the source to
    -- the correct snapshot location. This could/should be fixed in
    -- the stack source (especially since --haddock has similar
    -- behavior). A quick solution to avoid this might be to run the
    -- entire function in the target directory
    _ <- io $ readProcess "rm" ["--preserve-root", "-rf", dep] []

    -- error (show dep)

    exists <- io $ doesDirectoryExist dir
    tagged <- io $ doesFileExist tagFile

    unless exists $ void $ do
        io $ createDirectoryIfMissing True dir
        p $ "[tag:download] " ++ dep
        (ec,stout,_) <- runStk ["unpack", dep]
        case ec of
          ExitFailure _ -> void $ do
              p $ "[tag:download] failed to download " ++ dep ++ " - " ++ stout
          ExitSuccess   -> void $ do
              p $ "[tag:download] cp " ++ dep ++ " to snapshot source cache in " ++ dir
              io $ readProcess "mv" [dep,dir] []

    if tagged && nocache
       then do p $ "[tag:nocache] " ++ dep
               return (Right tagFile)
       else do p $ "[tag:cache] " ++ dep
               runTagger (TagCmd Hasktags ETags tagFile dir dep)
tagDependency nocache stkpaths dep = do

    let snapRoot
            | Just (sr : _) <- lookup "snapshot-install-root" stkpaths = sr
            | otherwise = error ("[tag:error] error tagging "
                               ++ dep
                               ++ ". "
                               ++ "No 'snapshot-install-root' found, aborting.")
        dir          = T.unpack snapRoot ++ "/packages" ++ "/" ++ dep
        tagFile      = dir ++ "/TAGS"

    -- HACK as of Aug 5 2015, `stack unpack` can only download sources
    -- into the current directory. Therefore, we move the source to
    -- the correct snapshot location. This could/should be fixed in
    -- the stack source (especially since --haddock has similar
    -- behavior). A quick solution to avoid this might be to run the
    -- entire function in the target directory
    _ <- io $ readProcess "rm" ["--preserve-root", "-rf", dep] []

    -- error (show dep)

    exists <- io $ doesDirectoryExist dir
    tagged <- io $ doesFileExist tagFile

    unless exists $ void $ do
        io $ createDirectoryIfMissing True dir
        p $ "[tag:download] " ++ dep
        (ec,stout,_) <- runStk ["unpack", dep]
        case ec of
          ExitFailure _ -> void $ do
              p $ "[tag:download] failed to download " ++ dep ++ " - " ++ stout
          ExitSuccess   -> void $ do
              p $ "[tag:download] cp " ++ dep ++ " to snapshot source cache in " ++ dir
              io $ readProcess "mv" [dep,dir] []

    if tagged && nocache
       then do p $ "[tag:nocache] " ++ dep
               return (Right tagFile)
       else do p $ "[tag:cache] " ++ dep
               runTagger (TagCmd Hasktags ETags tagFile dir dep)