Project

General

Profile

1
# Make
2
SHELL := /bin/bash
3
subMake = $(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
subdirs := schemas/ mappings/
34

    
35
#####
36

    
37
all: _always $(subdirs) inputs
38

    
39
.SUFFIXES:
40

    
41
_always:
42
.PHONY: _always
43

    
44
clean: _always inputs/clean $(addsuffix clean,$(subdirs))
45

    
46
define subdirTargets
47
$(subdir): _always
48
	+$$(subMake)
49

    
50
$(subdir)%: _always
51
	+$$(subMake)
52
endef
53
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
54

    
55
#####
56

    
57
install: _always core mysql inputs/install test
58
	@$(done)
59

    
60
uninstall: _always inputs/uninstall rm_mysql rm_core
61

    
62
reinstall: _always uninstall install
63

    
64
#####
65

    
66
core: _always $(call forOs,python postgres) postgres_user db
67
	@$(done)
68

    
69
rm_core: _always rm_db rm_postgres_user
70

    
71
reinstall_core: _always rm_core core
72

    
73
python-Linux: _always
74
	-sudo apt-get install python python-dev python-dateutil python-pip
75

    
76
python-Darwin: _always
77
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
78
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
79
	@echo 'Extract the archive'
80
	@echo 'Change to the directory of the extracted files'
81
	@echo 'Run python setup.py build'
82
	@echo 'Run sudo python setup.py install'
83
	@echo "Run python -c 'import dateutil'"
84
	@$(wait)
85

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

    
89
apacheConf := /etc/apache2/apache2.conf
90
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
91
editApacheConfForPhppgadmin =\
92
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
93
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
94

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

    
99
postgres-Linux: _always
100
	-sudo apt-get install postgresql libpq-dev phppgadmin
101
	$(editPhppgadminApacheConf)
102
	$(editApacheConfForPhppgadmin)
103
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
104
	-sudo apache2ctl restart
105
	-sudo pip install psycopg2
106
# ignore errors if conf files already edited
107

    
108
postgres-Darwin: _always
109
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
110
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
111
	@echo 'Open the disk image'
112
	@echo 'Run the installer in it'
113
	@$(wait)
114

    
115
postgres-: _always # other OSes
116

    
117
psqlOpts := --set ON_ERROR_STOP=1 --quiet
118
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
119
psqlAsBien := ./bin/psql_vegbien $(psqlOpts)
120
bienPassword := $(shell cat config/bien_password)
121

    
122
postgres_user: _always
123
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
124
# ignore errors if user exists
125

    
126
rm_postgres_user: _always
127
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
128

    
129
#####
130

    
131
db: _always schemas/vegbien.sql
132
	-$($@_create_cmd)
133
	$(psqlAsBien) <schemas/vegbien.sql
134
# ignore errors if database exists
135
db_create_cmd = echo "CREATE DATABASE vegbien OWNER bien ENCODING 'UTF8' \
136
TEMPLATE template1;"|$(psqlAsAdmin)
137

    
138
rm_db: _always
139
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
140

    
141
reinstall_db: _always rm_db db
142

    
143
empty_db: _always schemas/vegbien_empty.sql
144
	$(psqlAsBien) <schemas/vegbien_empty.sql
145

    
146
#####
147

    
148
mysql: _always $(call forOs,mysql) mysql_user
149
	@$(done)
150

    
151
rm_mysql: _always rm_mysql_user
152

    
153
mysql-Linux: _always
154
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
155
	@$(wait)
156
	-sudo apt-get install mysql-server mysql-client python-mysqldb
157

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

    
175
mysql-: _always # other OSes
176

    
177
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
178

    
179
mysql_user: _always
180
	-$($@_cmd)
181
# ignore errors if user exists
182
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
183
'$(bienPassword)';"|$(mysqlAsRoot)
184

    
185
rm_mysql_user: _always
186
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
187
# ignore errors if user exists
188

    
189
#####
190

    
191
inputs: _always inputs/all
192

    
193
import: _always inputs/import
194

    
195
verify: _always inputs/verify
196

    
197
test2: _always inputs/test
198
	@$(done)
199

    
200
#####
201

    
202
test: _always test-map
203
	@$(done)
204

    
205
test-%: _always
206
	./test/$(*F)
(1-1/3)