checkKernelspecFiles :: Kernelspec -> IO Kernelspec
checkKernelspecFiles spec = do
  let jsFile = kernelspecJsFile spec
      logoFile = kernelspecLogoFile spec
  kernelspecJsFile' <- checkFile jsFile
  kernelspecLogoFile' <- checkFile logoFile
  return spec { kernelspecJsFile = kernelspecJsFile', kernelspecLogoFile = kernelspecLogoFile' }

  where
    checkFile :: Maybe FilePath -> IO (Maybe FilePath)
    checkFile Nothing = return Nothing
    checkFile (Just file) = do
      exists <- doesFileExist file
      return $ if exists
                 then Just file
                 else Nothing

-- | A list of kernelspecs, obtained by running @jupyter kernelspec list --json@.
--
-- The list contains the name of the kernelspec mapped to the kernelspec itself.
checkKernelspecFiles :: Kernelspec -> IO Kernelspec
checkKernelspecFiles spec = do
  let jsFile = kernelspecJsFile spec
      logoFile = kernelspecLogoFile spec
  kernelspecJsFile' <- checkFile jsFile
  kernelspecLogoFile' <- checkFile logoFile
  return spec { kernelspecJsFile = kernelspecJsFile', kernelspecLogoFile = kernelspecLogoFile' }

  where
    checkFile :: Maybe FilePath -> IO (Maybe FilePath)
    checkFile Nothing = return Nothing
    checkFile (Just file) = do
      exists <- doesFileExist file
      return $ if exists
                 then Just file
                 else Nothing

-- | A list of kernelspecs, obtained by running @jupyter kernelspec list --json@.
--
-- The list contains the name of the kernelspec mapped to the kernelspec itself.
checkKernelspecFiles spec = do
  let jsFile = kernelspecJsFile spec
      logoFile = kernelspecLogoFile spec
  kernelspecJsFile' <- checkFile jsFile
  kernelspecLogoFile' <- checkFile logoFile
  return spec { kernelspecJsFile = kernelspecJsFile', kernelspecLogoFile = kernelspecLogoFile' }

  where
    checkFile :: Maybe FilePath -> IO (Maybe FilePath)
    checkFile Nothing = return Nothing
    checkFile (Just file) = do
      exists <- doesFileExist file
      return $ if exists
                 then Just file
                 else Nothing

-- | A list of kernelspecs, obtained by running @jupyter kernelspec list --json@.
--
-- The list contains the name of the kernelspec mapped to the kernelspec itself.