Project

General

Profile

1
# Make
2
SHELL := /bin/bash
3
subMake = $(if $(wildcard $(@D)/Makefile*),$(MAKE) $(@F) --directory=$(@D),)
4

    
5
# OS
6
os := $(shell uname)
7
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
8

    
9
# Terminal
10
esc := '['
11
reset := $(esc)'0m'
12
emph := $(esc)'7m '
13
endEmph := ' '$(reset)
14

    
15
# User interaction
16

    
17
confirm = $(shell read -p $(emph)"$(1) (y/n)"$(endEmph) REPLY; \
18
test "$REPLY" = y && echo t)
19

    
20
getPassword = $(shell read -s -p $(emph)"Enter your $(1) password:"$(endEmph) \
21
REPLY; echo >&2; echo -n "$REPLY")
22

    
23
wait := read -p $(emph)'Press ENTER to continue:'$(endEmph) REPLY
24

    
25
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
26

    
27
# File editing
28
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
29

    
30
# Env
31
export PGOPTIONS = --client-min-messages=WARNING
32

    
33
#####
34

    
35
all: _always install
36

    
37
.SUFFIXES:
38

    
39
_always:
40
.PHONY: _always
41

    
42
#####
43

    
44
install: _always core mysql inputs test
45
	@$(done)
46

    
47
uninstall: _always rm_inputs rm_mysql rm_core
48

    
49
reinstall: _always uninstall install
50

    
51
#####
52

    
53
core: _always $(call forOs,python postgres) postgres_user db
54
	@$(done)
55

    
56
rm_core: _always rm_db rm_postgres_user
57

    
58
reinstall_core: _always rm_core core
59

    
60
python-Linux: _always
61
	-sudo apt-get install python python-dev python-dateutil python-pip
62

    
63
python-Darwin: _always
64
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
65
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
66
	@echo 'Extract the archive'
67
	@echo 'Change to the directory of the extracted files'
68
	@echo 'Run python setup.py build'
69
	@echo 'Run sudo python setup.py install'
70
	@echo "Run python -c 'import dateutil'"
71
	@$(wait)
72

    
73
editPhppgadminApacheConf = echo $$'1\n,s/\# \(allow from all\)/\1/\nwq'|\
74
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
75

    
76
apacheConf := /etc/apache2/apache2.conf
77
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
78
editApacheConfForPhppgadmin =\
79
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
80
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
81

    
82
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
83
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
84
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
85

    
86
postgres-Linux: _always
87
	-sudo apt-get install postgresql libpq-dev phppgadmin
88
	$(editPhppgadminApacheConf)
89
	$(editApacheConfForPhppgadmin)
90
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
91
	-sudo apache2ctl restart
92
	-sudo pip install psycopg2
93
# ignore errors if conf files already edited
94

    
95
postgres-Darwin: _always
96
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
97
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
98
	@echo 'Open the disk image'
99
	@echo 'Run the installer in it'
100
	@$(wait)
101

    
102
postgres-: _always # other OSes
103

    
104
psqlOpts := --set ON_ERROR_STOP=1 --quiet
105
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
106
psqlAsBien := ./bin/psql_vegbien $(psqlOpts)
107
bienPassword := $(shell cat config/bien_password)
108

    
109
postgres_user: _always
110
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
111
# ignore errors if user exists
112

    
113
rm_postgres_user: _always
114
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
115

    
116
#####
117

    
118
db: _always
119
	-$($@_create_cmd)
120
	$(psqlAsBien) <mappings/schemas/vegbien.sql
121
# ignore errors if database exists
122
db_create_cmd = echo "CREATE DATABASE vegbien OWNER bien ENCODING 'UTF8' \
123
TEMPLATE template1;"|$(psqlAsAdmin)
124

    
125
rm_db: _always
126
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
127

    
128
reinstall_db: _always rm_db db
129

    
130
empty_db: _always
131
	$(psqlAsBien) <mappings/schemas/vegbien_empty.sql
132

    
133
#####
134

    
135
mysql: _always $(call forOs,mysql) mysql_user
136
	@$(done)
137

    
138
rm_mysql: _always rm_mysql_user
139

    
140
mysql-Linux: _always
141
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
142
	@$(wait)
143
	-sudo apt-get install mysql-server mysql-client python-mysqldb
144

    
145
mysql-Darwin: _always
146
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
147
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
148
	@echo 'Extract the archive'
149
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
150
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
151
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
152
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
153
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
154
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
155
	@echo 'Change to the directory of the extracted files'
156
	@echo 'Run python setup.py clean'
157
	@echo 'Run python setup.py build'
158
	@echo 'Run sudo python setup.py install'
159
	@echo "Run python -c 'import MySQLdb'"
160
	@$(wait)
161

    
162
mysql-: _always # other OSes
163

    
164
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
165

    
166
mysql_user: _always
167
	-$($@_cmd)
168
# ignore errors if user exists
169
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
170
'$(bienPassword)';"|$(mysqlAsRoot)
171

    
172
rm_mysql_user: _always
173
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
174
# ignore errors if user exists
175

    
176
#####
177

    
178
inputs := $(wildcard inputs/*/)
179

    
180
inputs: _always $(addsuffix install,$(inputs))
181

    
182
rm_inputs: _always $(addsuffix uninstall,$(inputs))
183

    
184
verify: _always $(addsuffix verify,$(inputs))
185

    
186
test2: _always $(addsuffix test,$(inputs))
187

    
188
inputs/%: _always
189
	+$(subMake)
190
# ignore errors in sub-makes
191

    
192
#####
193

    
194
test: _always test-map
195
	@$(done)
196

    
197
test-%: _always
198
	./test/$(*F)
(1-1/4)