staticFilesMergeMap :: FilePath -> M.Map FilePath FilePath -> Q [Dec]
staticFilesMergeMap fp m = do
  fs <- qRunIO $ getFileListPieces fp
  let filesList = map FP.joinPath fs
      mergedMapList = M.toList $ foldl' (checkedInsert invertedMap) m filesList
  mkStaticFilesList' fp (map splitBoth mergedMapList) True
  where
    splitBoth (k, v) = (split k, split v)
    swap (x, y) = (y, x)
    mapList = M.toList m
    invertedMap = M.fromList $ map swap mapList
    split :: FilePath -> [String]
    split [] = []
    split x =
        let (a, b) = break (== '/') x
         in a : split (drop 1 b)
    -- We want to keep mappings for all files that are pre-fingerprinted,
    -- so this function checks against all of the existing fingerprinted files and
    -- only inserts a new mapping if it's not a fingerprinted file.
    checkedInsert
      :: M.Map FilePath FilePath -- inverted dictionary
      -> M.Map FilePath FilePath -- accumulating state
      -> FilePath
      -> M.Map FilePath FilePath
    checkedInsert iDict st p = if M.member p iDict
      then st
      else M.insert p p st
staticFilesMergeMap :: FilePath -> M.Map FilePath FilePath -> Q [Dec]
staticFilesMergeMap fp m = do
  fs <- qRunIO $ getFileListPieces fp
  let filesList = map FP.joinPath fs
      mergedMapList = M.toList $ foldl' (checkedInsert invertedMap) m filesList
  mkStaticFilesList' fp (map splitBoth mergedMapList) True
  where
    splitBoth (k, v) = (split k, split v)
    swap (x, y) = (y, x)
    mapList = M.toList m
    invertedMap = M.fromList $ map swap mapList
    split :: FilePath -> [String]
    split [] = []
    split x =
        let (a, b) = break (== '/') x
         in a : split (drop 1 b)
    -- We want to keep mappings for all files that are pre-fingerprinted,
    -- so this function checks against all of the existing fingerprinted files and
    -- only inserts a new mapping if it's not a fingerprinted file.
    checkedInsert
      :: M.Map FilePath FilePath -- inverted dictionary
      -> M.Map FilePath FilePath -- accumulating state
      -> FilePath
      -> M.Map FilePath FilePath
    checkedInsert iDict st p = if M.member p iDict
      then st
      else M.insert p p st
staticFilesMergeMap fp m = do
  fs <- qRunIO $ getFileListPieces fp
  let filesList = map FP.joinPath fs
      mergedMapList = M.toList $ foldl' (checkedInsert invertedMap) m filesList
  mkStaticFilesList' fp (map splitBoth mergedMapList) True
  where
    splitBoth (k, v) = (split k, split v)
    swap (x, y) = (y, x)
    mapList = M.toList m
    invertedMap = M.fromList $ map swap mapList
    split :: FilePath -> [String]
    split [] = []
    split x =
        let (a, b) = break (== '/') x
         in a : split (drop 1 b)
    -- We want to keep mappings for all files that are pre-fingerprinted,
    -- so this function checks against all of the existing fingerprinted files and
    -- only inserts a new mapping if it's not a fingerprinted file.
    checkedInsert
      :: M.Map FilePath FilePath -- inverted dictionary
      -> M.Map FilePath FilePath -- accumulating state
      -> FilePath
      -> M.Map FilePath FilePath
    checkedInsert iDict st p = if M.member p iDict
      then st
      else M.insert p p st