summaryrefslogtreecommitdiff
path: root/dht/Setup.hs
blob: bd7c6d3efef26f89e3e2378ebd71e7f079f51dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import Control.Applicative
import Control.Monad
import Data.List
import Distribution.Simple
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Setup
import Distribution.Simple.Utils
import System.Directory

main = defaultMainWithHooks hooks

hooks :: UserHooks
hooks = simpleUserHooks
    { buildHook = \pkgDesc localBuildInfo hooks flags -> do
        let paths = (absoluteInstallDirs pkgDesc localBuildInfo dest)
                {- libsubdir = "<undef>"
                , datasubdir = "<undef>"
                -}
            dest = maybe NoCopyDest CopyTo $
                   flagToMaybe $ buildDistPref flags
        buildSystemDConfig localBuildInfo paths
        buildHook simpleUserHooks pkgDesc localBuildInfo hooks flags
    , copyHook = \pkgDesc localBuildInfo hooks flags -> do
        let paths = (absoluteInstallDirs pkgDesc localBuildInfo (fromFlag $ copyDest flags))
                {- libsubdir = "<undef>"
                , datasubdir = "<undef>"
                -}
            dest = maybe NoCopyDest CopyTo $
                   flagToMaybe $ copyDistPref flags
        copySystemDConfig localBuildInfo paths flags
        copyHook simpleUserHooks pkgDesc localBuildInfo hooks flags
    }

-- Drop a /-terminated prefix from a Filepath.  The / is not dropped.
dropPrefix :: String -> FilePath -> FilePath
dropPrefix prefix s | prefix `isPrefixOf` s = drop (length prefix - 1) s
                    | otherwise             = s

exestart :: FilePath -> InstallDirs FilePath -> String -> String
exestart buildprefix paths cmd
    | "ExecStart=" `isPrefixOf` cmd  = "ExecStart="++(dropPrefix buildprefix $ bindir paths) ++ "/presence"
    | otherwise                      = cmd

buildSystemDConfig :: LocalBuildInfo -> InstallDirs FilePath -> IO ()
buildSystemDConfig buildInfo paths = do
    template <- lines <$> readFile "presence.service"
    let buildprefix = takeWhile (/='/') (buildDir buildInfo) ++ "/"
        service = map (exestart buildprefix paths) template
    createDirectoryIfMissing True (buildDir buildInfo)
    writeFile (buildDir buildInfo ++ "/presence.service") $ unlines service

preferredSystemDPath prefix =
    case removeSlashes prefix of
        ""    -> "/lib/systemd/system"
        "usr" -> "/lib/systemd/system"
        _     -> "/etc/systemd/system"

removeSlashes prefix = reverse $ dropWhile (=='/') $ reverse $ dropWhile (=='/') prefix

-- InstallDirs {prefix = "dest/usr/local"
-- , bindir = "dest/usr/local/bin"
-- , libdir = "dest/usr/local/lib/x86_64-linux-ghc-8.0.2/presence-0.0.1-1fqjx0Frxyf1ESoA9cNUiZ"
-- , libsubdir = "<undef>"
-- , dynlibdir = "dest/usr/local/lib/x86_64-linux-ghc-8.0.2"
-- , libexecdir = "dest/usr/local/libexec"
-- , includedir = "dest/usr/local/lib/x86_64-linux-ghc-8.0.2/presence-0.0.1-1fqjx0Frxyf1ESoA9cNUiZ/include"
-- , datadir = "dest/usr/local/share/x86_64-linux-ghc-8.0.2/presence-0.0.1"
-- , datasubdir = "<undef>"
-- , docdir = "dest/usr/local/share/doc/x86_64-linux-ghc-8.0.2/presence-0.0.1"
-- , mandir = "dest/usr/local/share/man"
-- , htmldir = "dest/usr/local/share/doc/x86_64-linux-ghc-8.0.2/presence-0.0.1/html"
-- , haddockdir = "dest/usr/local/share/doc/x86_64-linux-ghc-8.0.2/presence-0.0.1/html"
-- , sysconfdir = "dest/usr/local/etc"}
-- CopyFlags
-- { copyDest = Flag (CopyTo "dest/")
-- , copyDistPref = Flag "dist"
-- , copyVerbosity = Flag Verbose}
-- realprefix = dest/usr/local

copySystemDConfig :: LocalBuildInfo -> InstallDirs FilePath -> CopyFlags -> IO ()
copySystemDConfig buildInfo paths flags = do
    -- print paths
    -- print flags
    let verbosity  = fromFlag (copyVerbosity flags)
        built      = buildDir buildInfo ++ "/presence.service"
        realprefix = dropPrefix dest (prefix paths)
        dest       = case fromFlag (copyDest flags) of
                        NoCopyDest -> ""
                        CopyTo p   -> p
        sysddir    = dest ++ drop 1 (preferredSystemDPath realprefix)
        sysdpath   = sysddir ++ "/presence.service"
    -- putStrLn $ "realprefix = " ++ realprefix
    -- putStrLn $ "no slashes = " ++ removeSlashes realprefix
    -- putStrLn $ "sysdpath   = " ++ sysdpath
    createDirectoryIfMissing True sysddir
    installOrdinaryFile verbosity built sysdpath