#!/bin/bash
# +----------------------------------------------------------------------+
# | Issabel version 4.0                                                  |
# | http://www.issabel.org                                               |
# +----------------------------------------------------------------------+
# | Copyright (c) 2018 Issabel Foundation                                |
# +----------------------------------------------------------------------+
# | The contents of this file are subject to the General Public License  |
# | (GPL) Version 2 (the "License"); you may not use this file except in |
# | compliance with the License. You may obtain a copy of the License at |
# | http://www.opensource.org/licenses/gpl-license.php                   |
# |                                                                      |
# | Software distributed under the License is distributed on an "AS IS"  |
# | basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See  |
# | the License for the specific language governing rights and           |
# | limitations under the License.                                       |
# +----------------------------------------------------------------------+

MARIADB_PASS=$(cat /etc/issabel.conf  | grep mysqlrootpwd | cut -d"=" -f2)
PARSED_OPTIONS=$(getopt -n "$0"  -o a:hm:r: --long "admin:,help,mariadb:,root:"  -- "$@")

function parse_args {
        #PARSED_OPTIONS=$(getopt -n "$0"  -o dhb: --long "dadhi,help,backup-file:"  -- "$@")
        #Bad arguments, something has gone wrong with the getopt command.
        if [ $? -ne 0 ];
        then
                echo ERROR getting args
                exit 1
        fi
        eval set -- "$PARSED_OPTIONS"

        while true
        do
                case "$1" in
                -h|--help)
                        print_usage
                	 shift;;
                -m|--mariadb)
                        if [ -n "$2" ];
                        then
                                MARIADB_NEWPASS=$2
                        else
                                echo "ERROR: Password is empty"
				exit
                        fi
                        shift 2;;
                -r|--root)
                        if [ -n "$2" ];
                        then
                                ROOTPASS=$2
                        else
                                echo "ERROR: Password is empty"
				exit
                        fi
                        shift 2;;
                -a|--admin)
                        if [ -n "$2" ];
                        then
                                ADMINPASS=$2
			else
				echo "ERROR: Password is empty"
				exit
                        fi
                        shift 2;;
                 --)
                        shift
                        break;;
                *)
                        echo "Invalid option $1"
                        print_usage
                        exit 1 ;;
                esac
        done
}

function print_usage {
	echo -e "Change Issabel passwords"
	echo -e "-a/--admin <password>\t\t\t Web admin password"
	echo -e "-m/--mariadb <password>\t\t\t MariaDB root password"
	echo -e "-r/--root <password>\t\t\t root user password"
}

function change_root {
	yes "$ROOTPASS" | passwd root
	EXITROOT=$?
	return 0
}

function change_admin_mariadb {
	#/bin/mysqladmin -user=root --password="$MARIADB_PASS" password "$MARIADB_NEWPASS"
	/usr/bin/issabel-admin-passwords --cli change $MARIADB_NEWPASS $ADMINPASS
	EXITMARIADB=$?
	return 0
}

(
parse_args
change_root
change_admin_mariadb
) > /dev/null
