From 64752fc197dc0da718e0a779d72e5fa74275dedc Mon Sep 17 00:00:00 2001
From: Tim Repke <timmothey@gmx.de>
Date: Wed, 21 Oct 2015 02:48:38 +0200
Subject: [PATCH] added setup script (currently update only)

---
 .gitignore                                  |   3 +-
 registration-system/other/backups/.htaccess |   1 +
 registration-system/other/setup.sh          | 128 ++++++++++++++++++++
 3 files changed, 131 insertions(+), 1 deletion(-)
 create mode 100644 registration-system/other/backups/.htaccess
 create mode 100755 registration-system/other/setup.sh

diff --git a/.gitignore b/.gitignore
index dd07eed..a23e6e8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,5 +4,6 @@
 *.swp
 **/config_current_fahrt_id
 */passwd/users.txt
-*/other/backups
+*/other/backups/*
+!*/other/backups/.htaccess
 mysql_data
diff --git a/registration-system/other/backups/.htaccess b/registration-system/other/backups/.htaccess
new file mode 100644
index 0000000..a7a56f7
--- /dev/null
+++ b/registration-system/other/backups/.htaccess
@@ -0,0 +1 @@
+Require all denied
\ No newline at end of file
diff --git a/registration-system/other/setup.sh b/registration-system/other/setup.sh
new file mode 100755
index 0000000..29fb1d7
--- /dev/null
+++ b/registration-system/other/setup.sh
@@ -0,0 +1,128 @@
+#!/usr/bin/env bash
+set -e
+
+function install {
+    echo "starting install wizzard"
+
+    # sure?
+    echo "this might destroy your current installation (if existing)"
+    read -r -p "Are you sure you want to proceed? [y/N] " response
+    response=${response,,}    # tolower
+    if [[ ! $response =~ ^(yes|y)$ ]] ; then exit 1 ; fi
+
+    # change operation dir
+    cd "${BASH_SOURCE%/*}"
+    mkdir -p backups
+
+    # create last update file
+    date +"%Y%m%d" > backups/lastUpdate
+
+    # create required files
+    cp ../passwd/users.example.txt ../passwd/users.txt
+    echo "2" > ../config_current_fahrt_id
+
+    # adjust chmod
+    chmod -R 644 ../
+    chmod 777 ../config_current_fahrt_id
+    chmod 777 ../passwd/users.txt
+
+    # ask config stuff
+    read -p "baseurl (i.e. \"https://domain.com/fsfahrt\"): " base
+    read -p "database name: " dbname
+    read -p "database user: " dbuser
+    read -p "database pw: " dbpass
+    read -p "database host: " dbhost
+
+    # adapt config.local.php file
+
+    # run db init
+
+}
+
+function update {
+    echo "updating system"
+
+    # sure?
+    echo "this might destroy your current installation (if existing)"
+    read -r -p "Are you sure you want to proceed? [y/N] " response
+    response=${response,,}    # tolower
+    if [[ ! $response =~ ^(yes|y)$ ]] ; then exit 1 ; fi
+
+    # change operation dir
+    cd "${BASH_SOURCE%/*}"
+    mkdir -p backups
+
+    # backup config files
+    cp ../config_current_fahrt_id backups/
+    cp ../passwd/users.txt backups/
+    cp ../config.local.php backups/
+
+    # fetch from github
+    read -p "git branch to pull from [default: master]: " BRANCH
+    read -p "git origin to pull from [default: origin]: " ORIGIN
+    if [[ -z $BRANCH ]] ; then BRANCH=master ; fi
+    if [[ -z $ORIGIN ]] ; then ORIGIN=origin ; fi
+    git checkout $BRANCH
+    git pull $ORIGIN $BRANCH
+
+    # fetch db credentials
+    dbname=`cat backups/config.local.php | grep \"name\" | cut -d \" -f 4`
+    dbuser=`cat backups/config.local.php | grep \"user\" | cut -d \" -f 4`
+    dbpass=`cat backups/config.local.php | grep \"pass\" | cut -d \" -f 4`
+    dbhost=`cat backups/config.local.php | grep \"host\" | cut -d \" -f 4`
+
+    # get last update date
+    lastUpdate=`cat backups/lastUpdate`
+    today=`date +"%Y%m%d"`
+
+    # create db backup
+    mysqldump -h $dbhost -u $dbuser -p $dbpass $dbname > backups/backup_$today.sql
+
+    # look for pending SQL updates and execute
+    for update in sqlDumps/update_*.sql; do
+        if [ $lastUpdate -lt `echo $update | cut -d _ -f 2 | cut -d . -f 1` ] ; then
+            echo "applying update $update"
+            mysql -h $dbhost -u $dbuser -p $dbpass $dbname < $update
+        else
+            echo "already up to date with $update"
+        fi
+    done
+
+    # update last update file
+    echo $today > backups/lastUpdate
+
+    # move config files back
+    mv backups/config_current_fahrt_id ../
+    mv backups/config.local.php ../
+    mv backups/users.txt ../passwd/users.txt
+}
+
+function printHelp {
+    echo "Script usage"
+    echo "-u | --update to update the system"
+    echo "-i | --install to install the system"
+}
+
+if [[ $# -eq 0 ]]; then
+    echo "No param given"
+    printHelp
+else
+    while [[ $# -gt 0 ]];
+    do
+        opt="$1"
+        shift;
+        case "$opt" in
+            "-h" | "--help" )
+                printHelp
+                exit 0;;
+            "-u" | "--update" )
+                update ;;
+            "-i" | "--install"   )
+                install ;;
+            * )
+                echo "Unexpected option ${opt}"
+                printHelp
+                exit 1;;
+        esac
+    done
+fi
\ No newline at end of file
-- 
GitLab