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
subdirs := schemas/ mappings/
34

    
35
#####
36

    
37
all: _always $(subdirs)
38

    
39
.SUFFIXES:
40

    
41
_always:
42
.PHONY: _always
43

    
44
clean: $(addsuffix clean,$(subdirs))
45

    
46
*/%: _always
47
	$(subMake)
48
.PRECIOUS: */%
49

    
50
%/: _always
51
	$(subMake)
52
.PRECIOUS: %/
53

    
54
#####
55

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

    
59
uninstall: _always rm_inputs rm_mysql rm_core
60

    
61
reinstall: _always uninstall install
62

    
63
#####
64

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

    
68
rm_core: _always rm_db rm_postgres_user
69

    
70
reinstall_core: _always rm_core core
71

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

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

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

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

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

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

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

    
114
postgres-: _always # other OSes
115

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

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

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

    
128
#####
129

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

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

    
140
reinstall_db: _always rm_db db
141

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

    
145
#####
146

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

    
150
rm_mysql: _always rm_mysql_user
151

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

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

    
174
mysql-: _always # other OSes
175

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

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

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

    
188
#####
189

    
190
inputs := $(wildcard inputs/*/)
191

    
192
inputs: _always $(addsuffix install,$(inputs))
193

    
194
rm_inputs: _always $(addsuffix uninstall,$(inputs))
195

    
196
import: _always $(addsuffix import,$(inputs))
197

    
198
verify: _always $(addsuffix verify,$(inputs))
199

    
200
test2: _always $(addsuffix test,$(inputs))
201

    
202
#####
203

    
204
test: _always test-map
205
	@$(done)
206

    
207
test-%: _always
208
	./test/$(*F)
(1-1/4)