diff options
-rw-r--r-- | Setup.hs | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/Setup.hs b/Setup.hs new file mode 100644 index 00000000..d07decd4 --- /dev/null +++ b/Setup.hs | |||
@@ -0,0 +1,96 @@ | |||
1 | import Control.Monad | ||
2 | import Data.List | ||
3 | import Distribution.Simple | ||
4 | import Distribution.Simple.LocalBuildInfo | ||
5 | import Distribution.Simple.Setup | ||
6 | import Distribution.Simple.Utils | ||
7 | import System.Directory | ||
8 | |||
9 | main = defaultMainWithHooks hooks | ||
10 | |||
11 | hooks :: UserHooks | ||
12 | hooks = simpleUserHooks | ||
13 | { buildHook = \pkgDesc localBuildInfo hooks flags -> do | ||
14 | let paths = (absoluteInstallDirs pkgDesc localBuildInfo dest) | ||
15 | {- libsubdir = "<undef>" | ||
16 | , datasubdir = "<undef>" | ||
17 | -} | ||
18 | dest = maybe NoCopyDest CopyTo $ | ||
19 | flagToMaybe $ buildDistPref flags | ||
20 | buildSystemDConfig localBuildInfo paths | ||
21 | buildHook simpleUserHooks pkgDesc localBuildInfo hooks flags | ||
22 | , copyHook = \pkgDesc localBuildInfo hooks flags -> do | ||
23 | let paths = (absoluteInstallDirs pkgDesc localBuildInfo (fromFlag $ copyDest flags)) | ||
24 | {- libsubdir = "<undef>" | ||
25 | , datasubdir = "<undef>" | ||
26 | -} | ||
27 | dest = maybe NoCopyDest CopyTo $ | ||
28 | flagToMaybe $ copyDistPref flags | ||
29 | copySystemDConfig localBuildInfo paths flags | ||
30 | copyHook simpleUserHooks pkgDesc localBuildInfo hooks flags | ||
31 | } | ||
32 | |||
33 | -- Drop a /-terminated prefix from a Filepath. The / is not dropped. | ||
34 | dropPrefix :: String -> FilePath -> FilePath | ||
35 | dropPrefix prefix s | prefix `isPrefixOf` s = drop (length prefix - 1) s | ||
36 | | otherwise = s | ||
37 | |||
38 | exestart :: FilePath -> InstallDirs FilePath -> String -> String | ||
39 | exestart buildprefix paths cmd | ||
40 | | "ExecStart=" `isPrefixOf` cmd = "ExecStart="++(dropPrefix buildprefix $ bindir paths) ++ "/presence" | ||
41 | | otherwise = cmd | ||
42 | |||
43 | buildSystemDConfig :: LocalBuildInfo -> InstallDirs FilePath -> IO () | ||
44 | buildSystemDConfig buildInfo paths = do | ||
45 | template <- lines <$> readFile "presence.service" | ||
46 | let buildprefix = takeWhile (/='/') (buildDir buildInfo) ++ "/" | ||
47 | service = map (exestart buildprefix paths) template | ||
48 | createDirectoryIfMissing True (buildDir buildInfo) | ||
49 | writeFile (buildDir buildInfo ++ "/presence.service") $ unlines service | ||
50 | |||
51 | preferredSystemDPath prefix = | ||
52 | case removeSlashes prefix of | ||
53 | "" -> "/lib/systemd/system" | ||
54 | "usr" -> "/lib/systemd/system" | ||
55 | _ -> "/etc/systemd/system" | ||
56 | |||
57 | removeSlashes prefix = reverse $ dropWhile (=='/') $ reverse $ dropWhile (=='/') prefix | ||
58 | |||
59 | -- InstallDirs {prefix = "dest/usr/local" | ||
60 | -- , bindir = "dest/usr/local/bin" | ||
61 | -- , libdir = "dest/usr/local/lib/x86_64-linux-ghc-8.0.2/presence-0.0.1-1fqjx0Frxyf1ESoA9cNUiZ" | ||
62 | -- , libsubdir = "<undef>" | ||
63 | -- , dynlibdir = "dest/usr/local/lib/x86_64-linux-ghc-8.0.2" | ||
64 | -- , libexecdir = "dest/usr/local/libexec" | ||
65 | -- , includedir = "dest/usr/local/lib/x86_64-linux-ghc-8.0.2/presence-0.0.1-1fqjx0Frxyf1ESoA9cNUiZ/include" | ||
66 | -- , datadir = "dest/usr/local/share/x86_64-linux-ghc-8.0.2/presence-0.0.1" | ||
67 | -- , datasubdir = "<undef>" | ||
68 | -- , docdir = "dest/usr/local/share/doc/x86_64-linux-ghc-8.0.2/presence-0.0.1" | ||
69 | -- , mandir = "dest/usr/local/share/man" | ||
70 | -- , htmldir = "dest/usr/local/share/doc/x86_64-linux-ghc-8.0.2/presence-0.0.1/html" | ||
71 | -- , haddockdir = "dest/usr/local/share/doc/x86_64-linux-ghc-8.0.2/presence-0.0.1/html" | ||
72 | -- , sysconfdir = "dest/usr/local/etc"} | ||
73 | -- CopyFlags | ||
74 | -- { copyDest = Flag (CopyTo "dest/") | ||
75 | -- , copyDistPref = Flag "dist" | ||
76 | -- , copyVerbosity = Flag Verbose} | ||
77 | -- realprefix = dest/usr/local | ||
78 | |||
79 | copySystemDConfig :: LocalBuildInfo -> InstallDirs FilePath -> CopyFlags -> IO () | ||
80 | copySystemDConfig buildInfo paths flags = do | ||
81 | -- print paths | ||
82 | -- print flags | ||
83 | let verbosity = fromFlag (copyVerbosity flags) | ||
84 | built = buildDir buildInfo ++ "/presence.service" | ||
85 | realprefix = dropPrefix dest (prefix paths) | ||
86 | dest = case fromFlag (copyDest flags) of | ||
87 | NoCopyDest -> "" | ||
88 | CopyTo p -> p | ||
89 | sysddir = dest ++ drop 1 (preferredSystemDPath realprefix) | ||
90 | sysdpath = sysddir ++ "/presence.service" | ||
91 | -- putStrLn $ "realprefix = " ++ realprefix | ||
92 | -- putStrLn $ "no slashes = " ++ removeSlashes realprefix | ||
93 | -- putStrLn $ "sysdpath = " ++ sysdpath | ||
94 | createDirectoryIfMissing True sysddir | ||
95 | installOrdinaryFile verbosity built sysdpath | ||
96 | |||