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: _always $(addsuffix clean,$(subdirs))
45

    
46
$(foreach subdir,$(wildcard */),$(eval $(subdir)%: _always; +$$(subMake)))
47

    
48
*/: _always
49
	+$(subMake)
50

    
51
#####
52

    
53
install: _always core mysql inputs test
54
	@$(done)
55

    
56
uninstall: _always rm_inputs rm_mysql rm_core
57

    
58
reinstall: _always uninstall install
59

    
60
#####
61

    
62
core: _always $(call forOs,python postgres) postgres_user db
63
	@$(done)
64

    
65
rm_core: _always rm_db rm_postgres_user
66

    
67
reinstall_core: _always rm_core core
68

    
69
python-Linux: _always
70
	-sudo apt-get install python python-dev python-dateutil python-pip
71

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

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

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

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

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

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

    
111
postgres-: _always # other OSes
112

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

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

    
122
rm_postgres_user: _always
123
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
124

    
125
#####
126

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

    
134
rm_db: _always
135
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
136

    
137
reinstall_db: _always rm_db db
138

    
139
empty_db: _always schemas/vegbien_empty.sql
140
	$(psqlAsBien) <schemas/vegbien_empty.sql
141

    
142
#####
143

    
144
mysql: _always $(call forOs,mysql) mysql_user
145
	@$(done)
146

    
147
rm_mysql: _always rm_mysql_user
148

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

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

    
171
mysql-: _always # other OSes
172

    
173
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
174

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

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

    
185
#####
186

    
187
inputs-%: $(addsuffix %,$(shell echo inputs/*/)) _always ;
188

    
189
inputs: _always inputs-install
190

    
191
rm_inputs: _always inputs-uninstall
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/4)