#!/bin/bash
################################################################################
# Licensed to Open Source Radio Access Network(OS-RAN) Alliance and OAI
# Software Alliance under one or more contributor license agreements. The
# initial OpenXG series projects are derided from OAI projects, the files from
# OAI projects are all in compliance with OAI Public License, Version 1.1.
# codes and files developed from OpenXG projects and from OS-RAN Alliance
# are all under OS-RAN licenses; you may not use this file except in compliance
# with the license.  You may get a copy of the license at:
#	http://www.openxg.org.cn/?falu_69.html
# For more information about OpenXG, please contact:
# contact@openxg.org.cn
################################################################################

# file hss_db_import
# brief
################################
# include helper functions
################################
THIS_SCRIPT_PATH=$(dirname $(readlink -f $0))
. $THIS_SCRIPT_PATH/build_helper


# arg 1 is mysql hostname  (ex: 127.0.0.1)
# arg 2 is hss username    (ex: hssadmin)
# arg 3 is hss password    (ex: admin)
# arg 4 is database name   (ex: oai_db)
# arg 5 is import file name(ex: oai_db_dump.sql)
function main(){
  EXPECTED_ARGS=5
  if [ $# -ne $EXPECTED_ARGS ]
  then
    echo_error "Usage: hss_db_import mysql_hostname hssuser hsspass databasename filename"
    return 1
  fi
  local mysql_hostname=$1
  local hss_username=$2
  local hss_password=$3
  local database_name=$4
  local file_name=$5
  
  mysqladmin --force -h $mysql_hostname -u $hss_username -p$hss_password drop $database_name 
  
  Q1="CREATE DATABASE IF NOT EXISTS $database_name;"
  mysql -h $mysql_hostname -u $hss_username --password=$hss_password -e "${Q1}"
  if [ $? -ne 0 ]; then
    echo_error "HSS: $database_name creation failed"
    return 1
  else
    echo_success "HSS: $database_name creation succeeded"
  fi
  
  mysql -h $mysql_hostname -u $hss_username -p$hss_password $database_name < $file_name 
  if [ $? -ne 0 ]; then
    echo_error "HSS: $database_name import failed:"
    cat $file_name
    return 1
  else
    echo_success "HSS: $database_name import succeeded"
  fi

  return 0
}

set_openair_env 

main "$@" 

