Project

General

Profile

1
# Make
2
SHELL := /bin/bash
3
pathParts = $(shell path="$(1)"; echo "$${path%%/*}" "$${path\#*/}")
4
topDir = $(word 1,$(pathParts))
5
subPath = $(word 2,$(pathParts))
6
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
7

    
8
# OS
9
os := $(shell uname)
10
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
11

    
12
# Terminal
13
esc := '['
14
reset := $(esc)'0m'
15
emph := $(esc)'7m '
16
endEmph := ' '$(reset)
17

    
18
# User interaction
19

    
20
confirm = $(shell read -p $(emph)"$(1) (y/n)"$(endEmph) REPLY; \
21
test "$REPLY" = y && echo t)
22

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

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

    
28
done = echo; echo $(emph)"Finished $@"$(endEmph); echo
29

    
30
# File editing
31
sudoAppend = echo $$'$(2)'|sudo tee -a $(1) >/dev/null
32

    
33
# Env
34
export PGOPTIONS = --client-min-messages=WARNING
35

    
36
#####
37

    
38
all:
39

    
40
.SUFFIXES:
41

    
42
_always:
43
.PHONY: _always
44

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

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

    
54
Makefile: ;
55

    
56
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
57

    
58
#####
59

    
60
install: _always core mysql inputs/install test
61
	@$(done)
62

    
63
uninstall: _always inputs/uninstall rm_mysql rm_core ;
64

    
65
reinstall: _always uninstall install ;
66

    
67
#####
68

    
69
core: _always $(call forOs,python postgres) postgres_user db
70
	@$(done)
71

    
72
rm_core: _always rm_db rm_postgres_user ;
73

    
74
reinstall_core: _always rm_core core ;
75

    
76
python-Linux: _always
77
	-sudo apt-get install python python-dev python-dateutil python-pip
78

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

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

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

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

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

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

    
118
postgres-: _always ; # other OSes
119

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

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

    
129
rm_postgres_user: _always
130
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
131

    
132
#####
133

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

    
141
rm_db: _always
142
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
143

    
144
reinstall_db: _always rm_db db ;
145

    
146
empty_db: _always schemas/vegbien_empty.sql
147
	$(psqlAsBien) <schemas/vegbien_empty.sql
148

    
149
#####
150

    
151
mysql: _always $(call forOs,mysql) mysql_user
152
	@$(done)
153

    
154
rm_mysql: _always rm_mysql_user ;
155

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

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

    
178
mysql-: _always # other OSes
179

    
180
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
181

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

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

    
192
#####
193

    
194
inputs: _always inputs/all ;
195

    
196
import: _always inputs/import ;
197

    
198
verify: _always inputs/verify ;
199

    
200
test2: _always inputs/test ;
201
	@$(done)
202

    
203
#####
204

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

    
208
test-%: _always
209
	./test/$(*F)
(1-1/3)