Merge pull request #4896 from totten/master-movedep
[civicrm-core.git] / bin / setup.sh
CommitLineData
6a488035 1#!/usr/bin/env bash
5e43f8f4 2set -e
6a488035
TO
3
4CALLEDPATH=`dirname $0`
5
6# Convert to an absolute path if necessary
7case "$CALLEDPATH" in
8 .*)
9 CALLEDPATH="$PWD/$CALLEDPATH"
10 ;;
11esac
12
ea16a823
TO
13if [ ! -f "$CALLEDPATH/setup.conf" ]; then
14 echo
15 echo "Missing configuration file. Please copy $CALLEDPATH/setup.conf.txt to $CALLEDPATH/setup.conf and edit it."
16 exit 1
17fi
18
6a488035 19source "$CALLEDPATH/setup.conf"
55f7d1ec 20source "$CALLEDPATH/setup.lib.sh"
6a488035
TO
21
22if [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
1b086766
TO
23 echo
24 echo "Usage: setup.sh [options] [schema file] [database data file] [database name] [database user] [database password] [database host] [database port] [additional args]"
25 echo "[options] is a mix of zero or more of following:"
26 echo " -a (All) Implies -Dgsdf (default)"
27 echo " -D (Download) Download depdencies"
28 echo " -g (GenCode) Generate DAO files, SQL files, etc"
29 echo " -s (Schema) Load new schema in DB"
30 echo " -d (Data) Load default data in DB"
31 echo " -f (Flush) Flush caches and settings"
32 echo
33 echo "Example: Perform a full setup"
34 echo " setup.sh -a"
35 echo
36 echo "Example: Setup all the code but leave the DB alone"
37 echo " setup.sh -Dg"
38 echo
39 echo "Example: Keep the existing code but reset the DB"
40 echo " setup.sh -sdf"
6a488035
TO
41 exit 0
42fi
43
1b086766
TO
44###############################################################################
45## Parse command line options
46
47FOUND_ACTION=
48DO_DOWNLOAD=
49DO_GENCODE=
50DO_SCHEMA=
51DO_DATA=
52DO_FLUSH=
53
54while getopts "aDgsdf" opt; do
55 case $opt in
56 a)
57 DO_DOWNLOAD=1
58 DO_GENCODE=1
59 DO_SCHEMA=1
60 DO_DATA=1
61 DO_FLUSH=1
62 FOUND_ACTION=1
63 ;;
64 D)
65 DO_DOWNLOAD=1
66 FOUND_ACTION=1
67 ;;
68 g)
69 DO_GENCODE=1
70 FOUND_ACTION=1
71 ;;
72 s)
73 DO_SCHEMA=1
74 FOUND_ACTION=1
75 ;;
76 d)
77 DO_DATA=1
78 FOUND_ACTION=1
79 ;;
80 f)
81 DO_FLUSH=1
82 FOUND_ACTION=1
83 ;;
84 \?)
85 echo "Invalid option: -$OPTARG" >&2
86 exit 1
87 ;;
88 :)
89 echo "Option -$OPTARG requires an argument." >&2
90 exit 1
91 ;;
92 esac
93done
94if [ -z "$FOUND_ACTION" ]; then
95 DO_DOWNLOAD=1
96 DO_GENCODE=1
97 DO_SCHEMA=1
98 DO_DATA=1
99 DO_FLUSH=1
100fi
101
102shift $((OPTIND-1))
6a488035
TO
103
104# fetch command line arguments if available
5e43f8f4
TO
105if [ ! -z "$1" ] ; then SCHEMA="$1"; fi
106if [ ! -z "$2" ] ; then DBLOAD="$2"; fi
107if [ ! -z "$3" ] ; then DBNAME="$3"; fi
108if [ ! -z "$4" ] ; then DBUSER="$4"; fi
109if [ ! -z "$5" ] ; then DBPASS="$5"; fi
28c32cc5
PH
110if [ ! -z "$6" ] ; then DBHOST="$6"; fi
111if [ ! -z "$7" ] ; then DBPORT="$7"; fi
6a488035
TO
112
113# verify if we have at least DBNAME given
114if [ -z $DBNAME ] ; then
115 echo "No database name defined!"
116 exit 1
117fi
118if [ -z $DBUSER ] ; then
119 echo "No database username defined!"
120 exit 1
121fi
122if [ -z $DBPASS ] ; then
123 read -p "Database password:"
124 DBPASS=$REPLY
125fi
126
1b086766
TO
127MYSQLCMD=$(mysql_cmd)
128###############################################################################
129## Execute tasks
130set -x
131
132if [ -n "$DO_DOWNLOAD" ]; then
133 pushd "$CALLEDPATH/.."
77665792
TO
134 COMPOSER=$(pickcmd composer composer.phar)
135 $COMPOSER install
136
137 if has_commands bower karma ; then
138 ## dev dependencies have been installed globally; don't force developer to redownload
139 npm install --production
140 else
141 npm install
142 fi
143
144 BOWER=$(pickcmd node_modules/bower/bin/bower bower)
145 $BOWER install
1b086766
TO
146 popd
147fi
07565b0a 148
6a488035
TO
149# run code generator if it's there - which means it's
150# checkout, not packaged code
1b086766
TO
151if [ -n "$DO_GENCODE" -a -d "$CALLEDPATH/../xml" ]; then
152 pushd "$CALLEDPATH/../xml"
153 if [ -z "$DBPORT" ]; then
154 PHP_MYSQL_HOSTPORT="$DBHOST"
155 else
156 PHP_MYSQL_HOSTPORT="$DBHOST:$DBPORT"
157 fi
158 "$PHP5PATH"php -d mysql.default_host="$PHP_MYSQL_HOSTPORT" -d mysql.default_user=$DBUSER -d mysql.default_password=$DBPASS GenCode.php $SCHEMA '' ${GENCODE_CMS}
159 popd
28c32cc5
PH
160fi
161
1b086766
TO
162if [ -n "$DO_SCHEMA" ]; then
163 pushd "$CALLEDPATH/../sql"
164 echo; echo "Dropping civicrm_* tables from database $DBNAME"
165 echo "SELECT table_name FROM information_schema.TABLES WHERE TABLE_SCHEMA='${DBNAME}' AND TABLE_TYPE = 'VIEW'" \
166 | $MYSQLCMD \
167 | grep '^\(civicrm_\|log_civicrm_\)' \
168 | awk -v NOFOREIGNCHECK='SET FOREIGN_KEY_CHECKS=0;' 'BEGIN {print NOFOREIGNCHECK}{print "drop view " $1 ";"}' \
169 | $MYSQLCMD
170 echo "SELECT table_name FROM information_schema.TABLES WHERE TABLE_SCHEMA='${DBNAME}' AND TABLE_TYPE = 'BASE TABLE'" \
171 | $MYSQLCMD \
172 | grep '^\(civicrm_\|log_civicrm_\)' \
173 | awk -v NOFOREIGNCHECK='SET FOREIGN_KEY_CHECKS=0;' 'BEGIN {print NOFOREIGNCHECK}{print "drop table " $1 ";"}' \
174 | $MYSQLCMD
175
176 echo; echo Creating database structure
177 $MYSQLCMD < civicrm.mysql
178 popd
6a488035
TO
179fi
180
1b086766
TO
181if [ -n "$DO_DATA" ]; then
182 pushd "$CALLEDPATH/../sql"
183 # load civicrm_generated.mysql sample data unless special DBLOAD is passed
184 if [ -z $DBLOAD ]; then
185 echo; echo Populating database with example data - civicrm_generated.mysql
186 $MYSQLCMD < civicrm_generated.mysql
187 else
188 echo; echo Populating database with required data - civicrm_data.mysql
189 $MYSQLCMD < civicrm_data.mysql
190 echo; echo Populating database with $DBLOAD data
191 $MYSQLCMD < $DBLOAD
192 fi
193
194 # load additional script if DBADD defined
195 if [ ! -z $DBADD ]; then
196 echo; echo Loading $DBADD
197 $MYSQLCMD < $DBADD
198 fi
199 popd
6a488035
TO
200fi
201
1b086766
TO
202if [ -n "$DO_FLUSH" ]; then
203 pushd "$CALLEDPATH/.."
204 # run the cli script to build the menu and the triggers
205 "$PHP5PATH"php bin/cli.php -e System -a flush --triggers 1 --session 1
6a488035 206
1b086766
TO
207 # reset config_backend and userFrameworkResourceURL which gets set
208 # when config object is initialized
209 $MYSQLCMD -e "UPDATE civicrm_domain SET config_backend = NULL; UPDATE civicrm_setting SET value = NULL WHERE name = 'userFrameworkResourceURL';"
210 popd
211fi
6a488035 212
1b086766 213echo; echo "NOTE: Logout from your CMS to avoid session conflicts."
6a488035 214