--- /dev/null
+#!/bin/sh
+
+# Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE.
+
+# This is kea-admin script that conducts administrative tasks on the Kea
+# installation. Currently supported operations are:
+#
+# - database version check
+# - database version upgrade
+
+
+# Get the location of the kea-admin scripts
+SCRIPTS_DIR_DEFAULT=@datarootdir@/@PACKAGE@/upgrade-scripts
+scripts_dir=${SCRIPTS_DIR_DEFAULT}
+
+# These are the default parameters. They will likely not work in any
+# specific deployment.
+db_user="keatest"
+db_password="keatest"
+db_name="keatest"
+
+usage() {
+ printf "kea-admin @PACKAGE_VERSION@\n"
+ printf "\n"
+ printf "This is a kea-admin script that conducts administrative tasks on\n"
+ printf "the Kea installation.\n"
+ printf "\n"
+ printf "Usage: $0 COMMAND BACKEND [parameters]\n"
+ printf "\n"
+ printf "COMMAND: Currently supported operations are:\n"
+ printf "\n"
+ printf " - init: Initalizes new database. Useful for first time installation.\n"
+ printf " - version: Checks version of the existing database scheme. Useful\n"
+ printf " - for checking DB version when preparing for an upgrade.\n"
+ printf " - upgrade: Upgrades your database scheme\n"
+ printf "\n"
+ printf "BACKEND - one of the supported backends: memfile|mysql|pgsql\n"
+ printf "\n"
+ printf "PARAMETERS: Parameters are optional in general, but may be required\n"
+ printf " for specific operation.\n"
+ printf " -u or --user name - specifies username when connecting to a database\n"
+ printf " -p or --password pass - specifies a password when connecting to a database\n"
+ printf " -n or --name database - specifies a database name to connect to\n"
+ printf " -d or --directory - path to upgrade scripts (default: ${SCRIPTS_DIR_DEFAULT})\n"
+}
+
+
+### Logging functions ###
+
+# Logs message at the error level.
+log_error() {
+ printf "ERROR/kea-admin: ${1}\n"
+}
+
+# Logs message at the warning level.
+log_warning() {
+ printf "WARNING/kea-admin: ${1}\n"
+}
+
+# Logs message at the info level.
+log_info() {
+ printf "INFO/kea-admin: ${1}\n"
+}
+
+### Convenience functions ###
+
+# Checks if the value is in the list. An example usage of this function
+# is to determine whether the kea-admin command belongs to the list of
+# supported commands.
+is_in_list() {
+ local member=${1} # Value to be checked
+ local list="${2}" # Comma separated list of items
+ _inlist=0 # Return value: 0 if not in list, 1 otherwise.
+ if [ -z ${member} ]; then
+ log_error "missing ${class}"
+ fi
+ # Iterate over all items on the list and compare with the member.
+ # If they match, return, otherwise log error and exit.
+ for item in ${list}
+ do
+ if [ ${item} = ${member} ]; then
+ _inlist=1
+ return
+ fi
+ done
+}
+
+mysql_execute() {
+ RESULT=`mysql -N -B --user=$db_user --password=$db_password -e "${1}" $db_name | sed "s/\t/./g"`
+ printf ${RESULT}
+}
+
+### Functions that implement database initialization commands
+init_memfile() {
+ log_error "NOT IMPLEMENTED"
+ exit 1
+}
+
+init_mysql() {
+ log_error "NOT IMPLEMENTED"
+ exit 1
+}
+
+init_pgsql() {
+ log_error "NOT IMPLEMENTED"
+ exit 1
+}
+
+### Functions that implement database version checking commands
+version_memfile() {
+ log_error "NOT IMPLEMENTED"
+ exit 1
+}
+
+version_mysql() {
+ MYSQL_VERSION=$(mysql_execute "SELECT version,minor FROM schema_version")
+ echo $MYSQL_VERSION
+}
+
+version_pgsql() {
+ log_error "NOT IMPLEMENTED"
+ exit 1
+}
+
+### Functions used for upgrade
+upgrade_memfile() {
+ log_error "NOT IMPLEMENTED"
+ exit 1
+}
+
+upgrade_mysql() {
+
+ printf "Version reported before upgrade: "
+ version_mysql
+
+ # Check if the scripts directory exists at all.
+ if [ ! -d ${scripts_dir}/mysql ]; then
+ log_error "Invalid scripts directory: ${scripts_dir}/mysql"
+ exit 1
+ fi
+
+ # Check if there are any files in it
+ num_files=`ls -1 ${scripts_dir}/mysql | wc -l &> /dev/null`
+ if [ $num_files -eq 0 ]; then
+ log_error "No scripts in ${scripts_dir}/mysql or the directory is not readable"
+ exit 1
+ fi
+
+ for script in ${scripts_dir}/mysql/*
+ do
+ echo "Processing $script file..."
+ sh ${script} --user=${db_user} --password=${db_password} ${db_name}
+ done
+
+ printf "Version reported after upgrade: "
+ version_mysql
+}
+
+upgrade_pgsql() {
+ log_error "NOT IMPLEMENTED"
+}
+
+
+### Script starts here ###
+
+command=${1}
+if [ -z ${command} ]; then
+ log_error "missing command"
+ usage
+ exit 1
+fi
+is_in_list "${command}" "init version upgrade"
+if [ ${_inlist} -eq 0 ]; then
+ log_error "invalid command: ${command}"
+ exit 1
+fi
+shift
+
+backend=${1}
+if [ -z ${backend} ]; then
+ log_error "missing backend"
+ usage
+ exit 1
+fi
+is_in_list "${backend}" "memfile mysql pgsql"
+if [ ${_inlist} -eq 0 ]; then
+ log_error "invalid backend: ${backend}"
+ exit 1
+fi
+shift
+
+# Ok, let's process parameters (if there are any)
+while [ ! -z "${1}" ]
+do
+ option=${1}
+ case ${option} in
+ # Specify database user
+ -u|--user)
+ shift
+ db_user=${1}
+ if [ -z ${db_user} ]; then
+ log_error "-u or --user requires a parameter"
+ usage
+ exit 1
+ fi
+ ;;
+ # Specify database password
+ -p|--password)
+ shift
+ db_pass=${1}
+ if [ -z ${db_pass} ]; then
+ log_error "-p or --password requires a parameter"
+ usage
+ exit 1
+ fi
+ ;;
+ # Specify database name
+ -n|--name)
+ shift
+ db_name=${1}
+ if [ -z ${db_name} ]; then
+ log_error "-n or --name requires a parameter"
+ usage
+ exit 1
+ fi
+ ;;
+ -d|--directory)
+ shift
+ scripts_dir=${1}
+ if [ -z ${scripts_dir} ]; then
+ log_error "-d or --directory requires a parameter"
+ usage
+ exit 1
+ fi
+ ;;
+ *)
+ log_error "invalid option: ${option}"
+ usage
+ exit 1
+ esac
+ shift
+done
+
+case ${command} in
+ # Initialize the database
+ init)
+ case ${backend} in
+ memfile)
+ init_memfile
+ ;;
+ mysql)
+ init_mysql
+ ;;
+ pgsql)
+ init_pgsql
+ ;;
+ esac
+ ;;
+ version)
+ case ${backend} in
+ memfile)
+ version_memfile
+ ;;
+ mysql)
+ version_mysql
+ ;;
+ pgsql)
+ version_pgsql
+ ;;
+ esac
+ ;;
+ upgrade)
+ case ${backend} in
+ memfile)
+ upgrade_memfile
+ ;;
+ mysql)
+ upgrade_mysql
+ ;;
+ pgsql)
+ upgrade_pgsql
+ ;;
+ esac
+ ;;
+esac
+
+exit 0