install
    :: Verbosity -- ^verbosity
    -> LocalBuildInfo
    -> FilePath  -- ^Library install location
    -> FilePath  -- ^Program install location
    -> FilePath  -- ^Executable install location
    -> FilePath  -- ^Program location on target system
    -> FilePath  -- ^Build location
    -> (FilePath,FilePath)  -- ^Executable (prefix,suffix)
    -> PackageDescription
    -> IO ()
--FIXME: this script should be generated at build time, just installed at this stage
install verbosity lbi libDir installProgDir binDir targetProgDir buildPref (progprefix,progsuffix) pkg_descr = do
    removeDirectoryRecursive libDir `catchIO` \_ -> return ()
    withLib pkg_descr $ \ lib ->
      findModuleFiles [buildPref] hugsInstallSuffixes (libModules lib)
        >>= installOrdinaryFiles verbosity libDir
    let buildProgDir = buildPref </> "programs"
    when (any (buildable . buildInfo) (executables pkg_descr)) $
        createDirectoryIfMissingVerbose verbosity True binDir
    withExe pkg_descr $ \ exe -> do
        let bi = buildInfo exe
        let theBuildDir = buildProgDir </> exeName exe
        let installDir = installProgDir </> exeName exe
        let targetDir = targetProgDir </> exeName exe
        removeDirectoryRecursive installDir `catchIO` \_ -> return ()
        findModuleFiles [theBuildDir] hugsInstallSuffixes
                        (ModuleName.main : autogenModuleName pkg_descr
                                         : otherModules (buildInfo exe))
          >>= installOrdinaryFiles verbosity installDir
        let targetName = "\"" ++ (targetDir </> hugsMainFilename exe) ++ "\""
        let hugsOptions = hcOptions Hugs (buildInfo exe)
                       ++ languageToFlags (compiler lbi) (defaultLanguage bi)
                       ++ extensionsToFlags (compiler lbi) (allExtensions bi)
            --TODO: also need to consider options, extensions etc of deps
            --      see ticket #43
        let baseExeFile = progprefix ++ (exeName exe) ++ progsuffix
        let exeFile = case buildOS of
                          Windows -> binDir </> baseExeFile <.> ".bat"
                          _       -> binDir </> baseExeFile
        let script = case buildOS of
                         Windows ->
                             let args = hugsOptions ++ [targetName, "%*"]
                             in unlines ["@echo off",
                                         unwords ("runhugs" : args)]
                         _ ->
                             let args = hugsOptions ++ [targetName, "\"$@\""]
                             in unlines ["#! /bin/sh",
                                         unwords ("runhugs" : args)]
        writeFileAtomic exeFile script
        setFileExecutable exeFile
install
    :: Verbosity -- ^verbosity
    -> LocalBuildInfo
    -> FilePath  -- ^Library install location
    -> FilePath  -- ^Program install location
    -> FilePath  -- ^Executable install location
    -> FilePath  -- ^Program location on target system
    -> FilePath  -- ^Build location
    -> (FilePath,FilePath)  -- ^Executable (prefix,suffix)
    -> PackageDescription
    -> IO ()
install verbosity lbi libDir installProgDir binDir targetProgDir buildPref (progprefix,progsuffix) pkg_descr = do
    removeDirectoryRecursive libDir `catchIO` \_ -> return ()
    withLib pkg_descr $ \ lib ->
      findModuleFiles [buildPref] hugsInstallSuffixes (libModules lib)
        >>= installOrdinaryFiles verbosity libDir
    let buildProgDir = buildPref </> "programs"
    when (any (buildable . buildInfo) (executables pkg_descr)) $
        createDirectoryIfMissingVerbose verbosity True binDir
    withExe pkg_descr $ \ exe -> do
        let bi = buildInfo exe
        let theBuildDir = buildProgDir </> exeName exe
        let installDir = installProgDir </> exeName exe
        let targetDir = targetProgDir </> exeName exe
        removeDirectoryRecursive installDir `catchIO` \_ -> return ()
        findModuleFiles [theBuildDir] hugsInstallSuffixes
                        (ModuleName.main : autogenModuleName pkg_descr
                                         : otherModules (buildInfo exe))
          >>= installOrdinaryFiles verbosity installDir
        let targetName = "\"" ++ (targetDir </> hugsMainFilename exe) ++ "\""
        let hugsOptions = hcOptions Hugs (buildInfo exe)
                       ++ languageToFlags (compiler lbi) (defaultLanguage bi)
                       ++ extensionsToFlags (compiler lbi) (allExtensions bi)
            --TODO: also need to consider options, extensions etc of deps
            --      see ticket #43
        let baseExeFile = progprefix ++ (exeName exe) ++ progsuffix
        let exeFile = case buildOS of
                          Windows -> binDir </> baseExeFile <.> ".bat"
                          _       -> binDir </> baseExeFile
        let script = case buildOS of
                         Windows ->
                             let args = hugsOptions ++ [targetName, "%*"]
                             in unlines ["@echo off",
                                         unwords ("runhugs" : args)]
                         _ ->
                             let args = hugsOptions ++ [targetName, "\"$@\""]
                             in unlines ["#! /bin/sh",
                                         unwords ("runhugs" : args)]
        writeFileAtomic exeFile script
        setFileExecutable exeFile
install verbosity lbi libDir installProgDir binDir targetProgDir buildPref (progprefix,progsuffix) pkg_descr = do
    removeDirectoryRecursive libDir `catchIO` \_ -> return ()
    withLib pkg_descr $ \ lib ->
      findModuleFiles [buildPref] hugsInstallSuffixes (libModules lib)
        >>= installOrdinaryFiles verbosity libDir
    let buildProgDir = buildPref </> "programs"
    when (any (buildable . buildInfo) (executables pkg_descr)) $
        createDirectoryIfMissingVerbose verbosity True binDir
    withExe pkg_descr $ \ exe -> do
        let bi = buildInfo exe
        let theBuildDir = buildProgDir </> exeName exe
        let installDir = installProgDir </> exeName exe
        let targetDir = targetProgDir </> exeName exe
        removeDirectoryRecursive installDir `catchIO` \_ -> return ()
        findModuleFiles [theBuildDir] hugsInstallSuffixes
                        (ModuleName.main : autogenModuleName pkg_descr
                                         : otherModules (buildInfo exe))
          >>= installOrdinaryFiles verbosity installDir
        let targetName = "\"" ++ (targetDir </> hugsMainFilename exe) ++ "\""
        let hugsOptions = hcOptions Hugs (buildInfo exe)
                       ++ languageToFlags (compiler lbi) (defaultLanguage bi)
                       ++ extensionsToFlags (compiler lbi) (allExtensions bi)
            --TODO: also need to consider options, extensions etc of deps
            --      see ticket #43
        let baseExeFile = progprefix ++ (exeName exe) ++ progsuffix
        let exeFile = case buildOS of
                          Windows -> binDir </> baseExeFile <.> ".bat"
                          _       -> binDir </> baseExeFile
        let script = case buildOS of
                         Windows ->
                             let args = hugsOptions ++ [targetName, "%*"]
                             in unlines ["@echo off",
                                         unwords ("runhugs" : args)]
                         _ ->
                             let args = hugsOptions ++ [targetName, "\"$@\""]
                             in unlines ["#! /bin/sh",
                                         unwords ("runhugs" : args)]
        writeFileAtomic exeFile script
        setFileExecutable exeFile