summaryrefslogtreecommitdiff
path: root/configure.hs
blob: 1abdc8914dfc521ebe9f7c5f450b54cd12c3c2a6 (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
{- Checks system configuration and generates SysConfig.hs. -}

import System.IO
import System.Cmd
import System.Exit
import System.Directory

type Test = IO Bool
data TestCase = TestCase String String Test
data Config = Config String Bool

instance Show Config where
        show (Config key value) = unlines [
			  key ++ " :: Bool"
			, key ++ " = " ++ show value
		]

tests :: [TestCase]
tests = [
	  TestCase "cp -a" "cp_a" $ testCp "-a"
	, TestCase "cp -p" "cp_p" $ testCp "-p"
	, TestCase "cp --reflink=auto" "cp_reflink_auto" $ testCp "--reflink=auto"
	, TestCase "uuid" "uuid" $ requireCmd "uuid" "uuid"
	, TestCase "xargs -0" "xargs_0" $ requireCmd "xargs -0" "xargs -0 </dev/null"
	, TestCase "rsync" "rsync" $ requireCmd "rsync" "rsync --version >/dev/null"
	]

tmpDir :: String
tmpDir = "tmp"

testFile :: String
testFile = tmpDir ++ "/testfile"

quiet :: String -> String
quiet s = s ++ " >/dev/null 2>&1"

requireCmd :: String -> String -> Test
requireCmd c cmdline = do
	ret <- testCmd $ quiet cmdline
	if ret
		then return True
		else do
			testEnd False
			error $ "** the " ++ c ++ " command is required to use git-annex"

testCp :: String -> Test
testCp option = testCmd $ quiet $ "cp " ++ option ++ " " ++ testFile ++ 
			" " ++ testFile ++ ".new"

testCmd :: String -> Test
testCmd c = do
	ret <- system c
	return $ ret == ExitSuccess

testStart :: String -> IO ()
testStart s = do
	putStr $ "  checking " ++ s ++ "..."
	hFlush stdout

testEnd :: Bool -> IO ()
testEnd r = putStrLn $ " " ++ show r

writeSysConfig :: [Config] -> IO ()
writeSysConfig config = writeFile "SysConfig.hs" body
	where
		body = unlines $ header ++ map show config ++ footer
		header = [
			  "{- Automatically generated by configure. -}"
			, "module SysConfig where"
			, ""
			]
		footer = []

runTests :: [TestCase] -> IO [Config]
runTests [] = return []
runTests ((TestCase tname key t):ts) = do
	testStart tname
	val <- t
	testEnd val
	rest <- runTests ts
	return $ (Config key val):rest

setup :: IO ()
setup = do
	createDirectoryIfMissing True tmpDir
	writeFile testFile "test file contents"

cleanup :: IO ()
cleanup = do
	removeDirectoryRecursive tmpDir

main :: IO ()
main = do
	setup
	config <- runTests tests
	writeSysConfig config
	cleanup