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
# Env
28
export PGOPTIONS = --client-min-messages=WARNING
29

    
30
#####
31

    
32
all: _not_file install
33

    
34
.SUFFIXES:
35

    
36
_not_file:
37
.PHONY: _not_file
38

    
39
#####
40

    
41
install: _not_file core mysql inputs test
42
	@$(done)
43

    
44
uninstall: _not_file rm_inputs rm_mysql rm_core
45

    
46
reinstall: _not_file uninstall install
47

    
48
#####
49

    
50
core: _not_file $(call forOs,python postgres) postgres_user db
51
	@$(done)
52

    
53
rm_core: _not_file rm_db rm_postgres_user
54

    
55
reinstall_core: _not_file rm_core core
56

    
57
python-Linux: _not_file
58
	-sudo apt-get install python-dev python-dateutil python-pip
59

    
60
python-Darwin: _not_file
61
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
62
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
63
	@echo 'Extract the archive'
64
	@echo 'Change to the directory of the extracted files'
65
	@echo 'Run python setup.py build'
66
	@echo 'Run sudo python setup.py install'
67
	@echo "Run python -c 'import dateutil'"
68
	@$(wait)
69

    
70
postgres-Linux: _not_file
71
	-sudo apt-get install postgresql libpq-dev phppgadmin
72
	-sudo pip install psycopg2
73

    
74
postgres-Darwin: _not_file
75
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
76
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
77
	@echo 'Open the disk image'
78
	@echo 'Run the installer in it'
79
	@$(wait)
80

    
81
postgres-: _not_file # other OSes
82

    
83
psqlOpts := --set ON_ERROR_STOP=1 --quiet
84
psqlAsAdmin := sudo -u postgres psql $(psqlOpts)
85
psqlAsBien := ./bin/psql_vegbien $(psqlOpts)
86
bienPassword := $(shell cat config/bien_password)
87

    
88
postgres_user: _not_file
89
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
90
# ignore errors about user existing
91

    
92
rm_postgres_user: _not_file
93
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
94

    
95
#####
96

    
97
db: _not_file
98
	$(psqlAsAdmin) <mappings/schemas/vegbien.sql
99

    
100
rm_db: _not_file
101
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
102

    
103
reinstall_db: _not_file rm_db db
104

    
105
empty_db: _not_file
106
	$(psqlAsBien) <mappings/schemas/vegbien_empty.sql
107

    
108
#####
109

    
110
mysql: _not_file $(call forOs,mysql) mysql_user
111
	@$(done)
112

    
113
rm_mysql: _not_file rm_mysql_user
114

    
115
mysql-Linux: _not_file
116
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
117
	@$(wait)
118
	-sudo apt-get install mysql-server mysql-client python-mysqldb
119

    
120
mysql-Darwin: _not_file
121
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
122
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
123
	@echo 'Extract the archive'
124
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
125
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
126
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
127
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
128
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
129
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
130
	@echo 'Change to the directory of the extracted files'
131
	@echo 'Run python setup.py clean'
132
	@echo 'Run python setup.py build'
133
	@echo 'Run sudo python setup.py install'
134
	@echo "Run python -c 'import MySQLdb'"
135
	@$(wait)
136

    
137
mysql-: _not_file # other OSes
138

    
139
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
140

    
141
mysql_user: _not_file
142
	-$($@_cmd)
143
# ignore errors about user existing
144
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
145
'$(bienPassword)';"|$(mysqlAsRoot)
146

    
147
rm_mysql_user: _not_file
148
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
149
# ignore errors about user not existing
150

    
151
#####
152

    
153
inputs := $(wildcard inputs/*/)
154

    
155
inputs: _not_file $(inputs:%=%install)
156

    
157
rm_inputs: _not_file $(inputs:%=%uninstall)
158

    
159
inputs/%: _not_file
160
	-$(subMake)
161
# ignore errors in sub-makes
162

    
163
#####
164

    
165
test: _not_file test-map
166
	@$(done)
167

    
168
test-%: _not_file
169
	./test/$(*F)
(1-1/4)