From 3ad0ef000d80212b25d89642450186534b4fb392 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <timm.fitschen@ds.mpg.de>
Date: Wed, 19 Dec 2018 23:51:01 +0100
Subject: [PATCH] EHN: use .config for makefile and patches

---
 configure                        |  2 +-
 makefile                         | 47 +++++++++++++++++++-------------
 patches/patch20150715-0/patch.sh | 10 +++----
 patches/utils/patch_header.sh    |  7 +++--
 4 files changed, 38 insertions(+), 28 deletions(-)

diff --git a/configure b/configure
index b8510b3..9c6e754 100755
--- a/configure
+++ b/configure
@@ -84,5 +84,5 @@ for opt in $OPTIONS; do
     esc_dollar=${esc_hash//\$/'$$'}
     esc_dq=${esc_dollar//\"/\\\"}
     esc_sq=${esc_dq//\'/\\\'}
-    echo "$opt=$esc_sq" >> .config
+    echo "$opt=\"$esc_sq\"" >> .config
 done
diff --git a/makefile b/makefile
index af9ffab..6717454 100644
--- a/makefile
+++ b/makefile
@@ -23,21 +23,30 @@
 SHELL=/bin/bash
 
 include .config
+M_MYSQL_USER=$(patsubst "%", %, $(MYSQL_USER))
+M_MYSQL_USER_PASSWORD=$(patsubst "%", %, $(MYSQL_USER_PASSWORD))
+M_LOGIN_PATH=$(patsubst "%", %, $(LOGIN_PATH))
+M_DATABASE_NAME=$(patsubst "%", %, $(DATABASE_NAME))
+M_DATABASE_USER_HOST_LIST=$(patsubst "%", %, $(DATABASE_USER_HOST_LIST))
+M_DATABASE_USER=$(patsubst "%", %, $(DATABASE_USER))
+M_MYSQL_CMD=$(patsubst "%", %, $(MYSQL_CMD))
+M_MYSQLDUMP_CMD=$(patsubst "%", %, $(MYSQLDUMP_CMD))
+M_MYSQLADMIN_CMD=$(patsubst "%", %, $(MYSQLADMIN_CMD))
 
 INSTALL_SQL_FILE=db_2_0.sql
 ifdef LOGIN_PATH
-	MYSQL_CONNECTION=--login-path=$(LOGIN_PATH)
+	M_MYSQL_CONNECTION=--login-path=$(M_LOGIN_PATH)
 else
-	MYSQL_CONNECTION=--user="$(MYSQL_USER)" --password="$(MYSQL_USER_PASSWORD)"
+	M_MYSQL_CONNECTION=--user="$(M_MYSQL_USER)" --password="$(M_MYSQL_USER_PASSWORD)"
 endif
 
 .PHONY: test-connection
 test-connection:
-	@$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "select 0;"
+	$(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "select 0;"
 
 .PHONY: upgrade
 upgrade:
-	@cd patches; ./applyPatches.sh $(MYSQL_CONNECTION) --database=$(DATABASE_NAME)
+	@cd patches; ./applyPatches.sh $(M_MYSQL_CONNECTION) --database=$(M_DATABASE_NAME)
 
 .PHONY: install
 install: _install _grant upgrade
@@ -45,35 +54,35 @@ install: _install _grant upgrade
 .PHONY: _install
 _install:
 	@if $(MAKE) _exists > /dev/null 2>&1; then \
-		printf "\n\nA database with with the name \"$(DATABASE_NAME)\" does already exist.\nCall 'make drop-$(DATABASE_NAME)' to delete that database or reconfigure with './configure'.\n"; \
+		printf "\n\nA database with with the name \"$(M_DATABASE_NAME)\" does already exist.\nCall 'make drop-$(M_DATABASE_NAME)' to delete that database or reconfigure with './configure'.\n"; \
 		exit 1; \
-	else sed 's/db_2_0/$(DATABASE_NAME)/g' $(INSTALL_SQL_FILE) | $(MYSQL_CMD) $(MYSQL_CONNECTION); fi
+	else sed 's/db_2_0/$(M_DATABASE_NAME)/g' $(INSTALL_SQL_FILE) | $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION); fi
 
 comma:=,
 .PHONY: _grant
 _grant:
-	@for host in $(subst $(comma), ,$(DATABASE_USER_HOST_LIST)); do \
-		( $(MYSQL_CMD) $(MYSQL_CONNECTION) -e "SELECT COUNT(*) FROM mysql.user WHERE user='$(DATABASE_USER)' AND host=\"$$host\";" | grep 0 ) || ( echo "The user '$(DATABASE_USER)@$$host' is already in the database. Please use another user or delete her. E.g. with 'mysql -u $(MYSQL_USER) -p -e \"DROP USER $(DATABASE_USER)@$$host;\"'" ; exit 1 ) ; \
+	@for host in $(subst $(comma), ,$(M_DATABASE_USER_HOST_LIST)); do \
+		( $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "SELECT COUNT(*) FROM mysql.user WHERE user='$(M_DATABASE_USER)' AND host=\"$$host\";" | grep 0 ) || ( echo "The user '$(M_DATABASE_USER)@$$host' is already in the database. Please use another user or delete her. E.g. with 'mysql -u $(M_MYSQL_USER) -p -e \"DROP USER $(M_DATABASE_USER)@$$host;\"'" ; exit 1 ) ; \
 	done
 	@while true; do \
-		printf "\n Please enter the password for $(DATABASE_USER): "; read -s password; \
-		printf "\nPlease repeat the password for $(DATABASE_USER): "; read -s password2; \
+		printf "\n Please enter the password for $(M_DATABASE_USER): "; read -s password; \
+		printf "\nPlease repeat the password for $(M_DATABASE_USER): "; read -s password2; \
 		if [ "$$password" != "$$password2" ]; then printf "\n\nThe passwords didn't match. Try again." ; else break ; fi; \
 	done ; \
 	echo "" ; \
-	for host in $(subst $(comma), ,$(DATABASE_USER_HOST_LIST)); do \
-		$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "CREATE USER IF NOT EXISTS '$(DATABASE_USER)'@'$$host' identified by '$$password';" ; \
-		$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT USAGE ON *.* TO '$(DATABASE_USER)'@'$$host';"; \
-		$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT ALL PRIVILEGES ON *.* TO '$(DATABASE_USER)'@'$$host' WITH GRANT OPTION;"; \
-		$(MYSQL_CMD) $(MYSQL_CONNECTION) -e "GRANT EXECUTE ON *.* TO '$(DATABASE_USER)'@'$$host';"; \
+	for host in $(subst $(comma), ,$(M_DATABASE_USER_HOST_LIST)); do \
+		$(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "CREATE USER IF NOT EXISTS '$(M_DATABASE_USER)'@'$$host' identified by '$$password';" ; \
+		$(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "GRANT USAGE ON *.* TO '$(M_DATABASE_USER)'@'$$host';"; \
+		$(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "GRANT ALL PRIVILEGES ON *.* TO '$(M_DATABASE_USER)'@'$$host' WITH GRANT OPTION;"; \
+		$(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -e "GRANT EXECUTE ON *.* TO '$(M_DATABASE_USER)'@'$$host';"; \
 	done
 
 .PHONY: drop-%
 drop-%:
-	@$(MYSQLADMIN_CMD) $(MYSQL_CONNECTION) -f drop $(patsubst drop-%,%,$@)
+	@$(M_MYSQLADMIN_CMD) $(M_MYSQL_CONNECTION) -f drop $(patsubst drop-%,%,$@)
 
 .PHONY: _exists
 _exists:
-	@if $(MYSQL_CMD) $(MYSQL_CONNECTION) -D "$(DATABASE_NAME)" -e "show tables;" > /dev/null 2>&1 ; then \
-		echo "$(DATABASE_NAME) does exist." ; exit 0 ; \
-	else echo "$(DATABASE_NAME) does not exist." ; exit 1; fi
+	@if $(M_MYSQL_CMD) $(M_MYSQL_CONNECTION) -D "$(M_DATABASE_NAME)" -e "show tables;" > /dev/null 2>&1 ; then \
+		echo "$(M_DATABASE_NAME) does exist." ; exit 0 ; \
+	else echo "$(M_DATABASE_NAME) does not exist." ; exit 1; fi
diff --git a/patches/patch20150715-0/patch.sh b/patches/patch20150715-0/patch.sh
index 1df77d6..4941c81 100755
--- a/patches/patch20150715-0/patch.sh
+++ b/patches/patch20150715-0/patch.sh
@@ -37,7 +37,7 @@ fi
 mysql_execute "CREATE FUNCTION CaosDBVersion() RETURNS VARCHAR(255) DETERMINISTIC RETURN 'v2.0.0';"
 
 # create transaction_log table with new schema
-mysql $MYSQL_CONNECTION -D $DATABASE -e "CREATE TABLE new_transaction_log (
+mysql_execute "CREATE TABLE new_transaction_log (
  transaction VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'TRANSACTION.',
  user_id INT UNSIGNED NOT NULL COMMENT 'User.',
  date INT NOT NULL COMMENT 'Date of transaction.',
@@ -48,19 +48,19 @@ mysql $MYSQL_CONNECTION -D $DATABASE -e "CREATE TABLE new_transaction_log (
 ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;"
 
 # copy from old transaction_log
-mysql $MYSQL_CONNECTION -D $DATABASE -e "INSERT INTO new_transaction_log SELECT transaction, user_id, (Year(timestamp) * 10000 + Month(timestamp) * 100 + Day(timestamp)) as date, ((Hour(timestamp) + 1) * 10000 + (Minute(timestamp) + 1)*100 + Second(timestamp) + 1 ) as time, NULL as ns, entity_id from transaction_log";
+mysql_execute "INSERT INTO new_transaction_log SELECT transaction, user_id, (Year(timestamp) * 10000 + Month(timestamp) * 100 + Day(timestamp)) as date, ((Hour(timestamp) + 1) * 10000 + (Minute(timestamp) + 1)*100 + Second(timestamp) + 1 ) as time, NULL as ns, entity_id from transaction_log";
 
 # check if both have same number of rows
-ROWSTEST=$(mysql $MYSQL_CONNECTION -D $DATABASE -e "SELECT count(*) from new_transaction_log into @newRows; SELECT count(*) from transaction_log into @oldRows; Select (@newRows = @oldRows) as test" | sed -e ':a;N;$!ba;s/[^01]//g') 
+ROWSTEST=$(mysql_execute "SELECT count(*) from new_transaction_log into @newRows; SELECT count(*) from transaction_log into @oldRows; Select (@newRows = @oldRows) as test" | sed -e ':a;N;$!ba;s/[^01]//g') 
 
 if [ 0 -eq "$ROWSTEST" ]; then
  failure "The rows do not match. Something went wrong."
 fi
 
 # delete old transaction_log
-mysql $MYSQL_CONNECTION -D $DATABASE -e "DROP TABLE transaction_log;" 
+mysql_execute "DROP TABLE transaction_log;" 
 
 # rename new_transaction_log to transaction_log
-mysql $MYSQL_CONNECTION -D $DATABASE -e "ALTER TABLE new_transaction_log RENAME TO transaction_log" 
+mysql_execute "ALTER TABLE new_transaction_log RENAME TO transaction_log" 
 
 success
diff --git a/patches/utils/patch_header.sh b/patches/utils/patch_header.sh
index 1ce4972..2f08e86 100644
--- a/patches/utils/patch_header.sh
+++ b/patches/utils/patch_header.sh
@@ -22,12 +22,11 @@
 #
 #header for patch scripts
 if test -e ../.config ; then
-    source ../.config
+    . ../.config
 fi
 if test -e .config ; then
-    source .config
+    . .config
 fi
-USAGE="$1 --database=DATABASE_NAME [ --login-path=LOGIN_PATH | --user=USER [ --password=PASSWORD ] ]\n\n"
 CMD_OPTIONS="options:\n\n-h, --help\n\tShow brief help.\n-l LOGIN_PATH, --login-path=LOGIN_PATH\n\tA login-path for the mysql connection (see 'man mysql' and 'man mysql-config-editor').\n-u USER, --user=USER\n\tA mysql user.\n-p PASSWORD, --password=PASSWORD\n\tThe password for the mysql user.\n"
 set -e
 
@@ -189,3 +188,5 @@ function mysql_execute {
 function redo_table {
 	$MYSQL_CMD $MYSQL_CONNECTION -D $DATABASE_NAME < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql
 }
+
+echo "$MYSQL_CMD $MYSQL_CONNECTION -D $DATABASE_NAME -e $1"
-- 
GitLab