#!/bin/sh

Q_DIR_NEW_DATABASE="Enter the database directory"
M_CREATE_DB_DIR="Directory \$NEW_DATABASE doesn't exist"
Q_CREATE_DB_DIR="Create directory \$NEW_DATABASE?"
M_ERR_CRE_DB_DIR="Directory \$NEW_DATABASE creation error"
M_ERR_DISTRIBUTIVE="This procedure should be started from the Linter root directory.
Database creation aborted."
M_EXIST_DATABASE="There is some database already in the directory \$NEW_DATABASE.
Can't create the database."
M_ERROR_CRE_DATA="Database creation error. Verify the database directory access mode
and the hard drive free space size."
M_SUCCESS_DB="Database has been created in the directory \$NEW_DATABASE."
Q_CREATE_RUN_SCRIPTS="Create Linter start/stop procedure for the database $NEW_DATABASE ?"
M_CURRENT_PROC="Create database"
Q_CONFIGURE_ASYNC="Create auxiliary database needed for asynchronous replication feature activation?"
M_M_INCORRECT_RUN="This procedure must be started from the Linter root directory."

[ "$INSTALL_LANG" = "" ] && {
  [ -f scripts/lang ] && {
    i=0
    LANGUAGES=`cat scripts/lang`
    for L in "english" $LANGUAGES; do
      i=`expr $i + 1`
      echo "$i - $L"
    done
    echo "Select language please ( from 1 to $i ):"
    read lang
    i=0
    LLL="english"
    for L in "english" $LANGUAGES; do
      i=`expr $i + 1`
      [ "$i" = "$lang" ] && {
        LLL=$L
      }
    done
    [ "$LLL" = "english" ] || {
      INSTALL_LANG=$LLL
      export INSTALL_LANG
    }
  }
}

[ "$INSTALL_LANG" = "" ] || {
  [ -f create_database."$INSTALL_LANG" ] && {
    . create_database."$INSTALL_LANG"
  }
  [ -f scripts/create_database."$INSTALL_LANG" ] && {
    . scripts/create_database."$INSTALL_LANG"
  }
}

[ -f ./scripts/dialog.sh ] || {
    echo "$M_M_INCORRECT_RUN"
    exit 1
    }

. ./scripts/dialog.sh

push_path "$M_CURRENT_PROC"

[ A"$LINTER_DIR" = A ] && {
  LINTER_DIR=`pwd`
  export LINTER_DIR
  [ -x "$LINTER_DIR/bin/linter" ] || [ -x "$LINTER_DIR/bin/gendb" ] || {
    say_comment "$M_ERR_DISTRIBUTIVE"
    pop_path
    exit 1
    }
  }

while true; do
  input_value "DIR_NEW_DATABASE" "$LINTER_DIR/database"
  NEW_DATABASE="$rets"
  [ -d "$NEW_DATABASE" ] || {
    def_yes_no "CREATE_DB_DIR" "Y" && mkdir "$NEW_DATABASE"
    [ -d "$NEW_DATABASE" ] || {
      say_comment "$M_ERR_CRE_DB_DIR"
      continue
      }
    }
  break
done

if [ -f "$NEW_DATABASE/1.01" ] ;then
  say_comment "$M_EXIST_DATABASE"
else

  SY00="$NEW_DATABASE"
  save_dir=`pwd`
  export SY00
  cd "$NEW_DATABASE"
  "$LINTER_DIR"/db/make_db && say_comment "$M_SUCCESS_DB"
  [ -x "$LINTER_DIR"/db/make_storage ] &&  def_yes_no "CONFIGURE_ASYNC" Y && "$LINTER_DIR"/db/make_storage && say_comment "$M_SUCCESS_DB"
  cd "$save_dir"
fi

[ -f ./scripts/setcp.sh ] && {
  DATABASE_DIR="$NEW_DATABASE"
  export DATABASE_DIR
  ./scripts/setcp.sh
  }


def_yes_no "CREATE_RUN_SCRIPTS" "Y" && 
            {
            DATABASE_DIR="$NEW_DATABASE"
            export DATABASE_DIR
            export PATH_STACK
            ./scripts/make_scripts
            }

pop_path
