#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2010, 2011, 2013, 2014, 2015, 2016 Synacor, Inc.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software Foundation,
# version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
# ***** END LICENSE BLOCK *****
# 

if [ x`whoami` != "xzimbra" ]; then
  echo "Error: must be run as user zimbra"
  exit 1
fi

source /opt/zimbra/bin/zmshutil || exit 1
zmsetvars -f

#
# Sanity checks
#
zmassert -x /opt/zimbra/common/bin/mysqladmin
zmassert -x /opt/zimbra/common/bin/mysql
zmassert -x /opt/zimbra/bin/zmlocalconfig
zmassert -x /opt/zimbra/bin/zmcontrol
zmassert -r ${zimbra_db_directory}/db.sql
if [ ! -x /opt/zimbra/common/bin/mysql ]; then
  echo "Mysql not found on this host."
  exit 1
fi

usage() {
  echo "$0 [-help] password"
}

ask() {
  PROMPT=$1
  DEFAULT=$2

  echo ""
  echo -n "$PROMPT [$DEFAULT] "
  read response

  if [ -z $response ]; then
    response=$DEFAULT
  fi
}
askYN() {
  PROMPT=$1
  DEFAULT=$2

  if [ "x$DEFAULT" = "xyes" -o "x$DEFAULT" = "xYes" -o "x$DEFAULT" = "xy" -o "x$DEFAULT" = "xY" ]; then
    DEFAULT="Y"
  else
    DEFAULT="N"
  fi

  while [ 1 ]; do
    ask "$PROMPT" "$DEFAULT"
    response=$(perl -e "print lc(\"$response\");")
    if [ -z $response ]; then
      :
    else
      if [ $response = "yes" -o $response = "y" ]; then
        response="yes"
        break
      else
        if [ $response = "no" -o $response = "n" ]; then
          response="no"
          break
        fi
      fi
    fi
    echo "A Yes/No answer is required"
  done
}

for opt in "$@"; do
  case "$opt" in
    -help|--help|-h|--help)
      usage
      exit 0
      shift
      ;;
    --*|-*)
      echo "Unknown option $opt"
      usage
      exit 1
      shift
      ;;
    *)
      password=$1
      shift
      ;;
  esac
done



if [ x"$password" = "x" ]; then
  usage
  exit 1
fi

askYN "WARNING: All zimbra services will be stopped.  Would you like to continue?" "N"
if [ $response != "yes" ]; then
  echo "All services must be stopped in order to reset mysql password. Exiting."
  exit
fi

/opt/zimbra/bin/zmcontrol stop

echo "Starting mysqld"
/opt/zimbra/common/bin/mysqld_safe --defaults-file=${mysql_mycnf} --skip-grant-tables --ledir=/opt/zimbra/common/sbin &
sleep 10

echo "Changing zimbra passwd"
/opt/zimbra/bin/mysql -Dmysql -P ${mysql_port} -e "update user set password=PASSWORD(\"$password\") where user = 'zimbra';"
/opt/zimbra/bin/zmlocalconfig -f -e zimbra_mysql_password=$password

echo "Changing root passwd"
/opt/zimbra/bin/mysql -Dmysql -P ${mysql_port} -e "update user set password=PASSWORD(\"$password\") where user = 'root';"
/opt/zimbra/bin/zmlocalconfig -f -e mysql_root_password=$password

echo "Flushing privileges";
/opt/zimbra/bin/mysql -Dmysql -P ${mysql_port} -e "flush privileges;"
/opt/zimbra/bin/mysql.server stop

echo "Restarting zimbra services"
/opt/zimbra/bin/zmcontrol start
