installKernelspec :: InstallUser -- ^ Whether this kernel should be installed with or without @--user@
                  -> FilePath    -- ^ Path to the @jupyter@ executable
                  -> Kernelspec  -- ^ Kernelspec to install
                  -> IO ()
installKernelspec installUser jupyterPath kernelspec = do
  tempDir <- getTemporaryDirectory
  let kernelspecDir = tempDir ++ "/" ++ T.unpack (kernelspecLanguage kernelspec)
  prepareKernelspecDirectory kernelspec kernelspecDir

  let userFlag =
        case installUser of
          InstallLocal  -> ["--user"]
          InstallGlobal -> []
      cmd = "kernelspec" : "install" : kernelspecDir : "--replace" : userFlag
  void $ runJupyterCommand jupyterPath cmd


-- | Parse a Jupyter version string into a list of integers.
--
-- >>> parseVersion "4.1.3\n"
-- Just (JupyterVersion 4 1 3)
--
-- >>> parseVersion "XYZ"
-- Nothing
--
-- If minor or patch versions are unavailable, they are assumed to be zero:
--
-- >>> parseVersion "4.1"
-- Just (JupyterVersion 4 1 0)
--
-- >>> parseVersion "4"
-- Just (JupyterVersion 4 0 0)
installKernelspec :: InstallUser -- ^ Whether this kernel should be installed with or without @--user@
                  -> FilePath    -- ^ Path to the @jupyter@ executable
                  -> Kernelspec  -- ^ Kernelspec to install
                  -> IO ()
installKernelspec installUser jupyterPath kernelspec = do
  tempDir <- getTemporaryDirectory
  let kernelspecDir = tempDir ++ "/" ++ T.unpack (kernelspecLanguage kernelspec)
  prepareKernelspecDirectory kernelspec kernelspecDir

  let userFlag =
        case installUser of
          InstallLocal  -> ["--user"]
          InstallGlobal -> []
      cmd = "kernelspec" : "install" : kernelspecDir : "--replace" : userFlag
  void $ runJupyterCommand jupyterPath cmd


-- | Parse a Jupyter version string into a list of integers.
--
-- >>> parseVersion "4.1.3\n"
-- Just (JupyterVersion 4 1 3)
--
-- >>> parseVersion "XYZ"
-- Nothing
--
-- If minor or patch versions are unavailable, they are assumed to be zero:
--
-- >>> parseVersion "4.1"
-- Just (JupyterVersion 4 1 0)
--
-- >>> parseVersion "4"
-- Just (JupyterVersion 4 0 0)
installKernelspec installUser jupyterPath kernelspec = do
  tempDir <- getTemporaryDirectory
  let kernelspecDir = tempDir ++ "/" ++ T.unpack (kernelspecLanguage kernelspec)
  prepareKernelspecDirectory kernelspec kernelspecDir

  let userFlag =
        case installUser of
          InstallLocal  -> ["--user"]
          InstallGlobal -> []
      cmd = "kernelspec" : "install" : kernelspecDir : "--replace" : userFlag
  void $ runJupyterCommand jupyterPath cmd


-- | Parse a Jupyter version string into a list of integers.
--
-- >>> parseVersion "4.1.3\n"
-- Just (JupyterVersion 4 1 3)
--
-- >>> parseVersion "XYZ"
-- Nothing
--
-- If minor or patch versions are unavailable, they are assumed to be zero:
--
-- >>> parseVersion "4.1"
-- Just (JupyterVersion 4 1 0)
--
-- >>> parseVersion "4"
-- Just (JupyterVersion 4 0 0)