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
#####
34

    
35
all:
36

    
37
.SUFFIXES:
38

    
39
_always:
40
.PHONY: _always
41

    
42
define subdirTargets
43
$(subdir): _always
44
	+$$(subMake)
45

    
46
$(subdir)%: _always
47
	+$$(subMake)
48
endef
49
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
50

    
51
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
52

    
53
#####
54

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

    
58
uninstall: _always inputs/uninstall rm_mysql rm_core
59

    
60
reinstall: _always uninstall install
61

    
62
#####
63

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

    
67
rm_core: _always rm_db rm_postgres_user
68

    
69
reinstall_core: _always rm_core core
70

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

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

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

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

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

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

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

    
113
postgres-: _always # other OSes
114

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

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

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

    
127
#####
128

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

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

    
139
reinstall_db: _always rm_db db
140

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

    
144
#####
145

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

    
149
rm_mysql: _always rm_mysql_user
150

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

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

    
173
mysql-: _always # other OSes
174

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

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

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

    
187
#####
188

    
189
inputs: _always inputs/all
190

    
191
import: _always inputs/import
192

    
193
verify: _always inputs/verify
194

    
195
test2: _always inputs/test
196
	@$(done)
197

    
198
#####
199

    
200
test: _always test-map
201
	@$(done)
202

    
203
test-%: _always
204
	./test/$(*F)
(1-1/3)