#!/bin/sh

# LXCF - LXC Facility
# Copyright (C) 2013-2014 FUJITSU LIMITED

# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

# check root
if [ ${EUID:-$UID} -ne 0 ] ; then
  echo "error: Because you are not root, you cannot execute this command."
  exit 1
fi

# make queue and message
mkdir -p /var/lib/lxcf
touch /var/lib/lxcf/hqueue
chmod 644 /var/lib/lxcf/hqueue
touch /var/lib/lxcf/qqueue
chmod 644 /var/lib/lxcf/qqueue
touch /var/lib/lxcf/lqueue
chmod 644 /var/lib/lxcf/lqueue
touch /var/lib/lxcf/exjob
chmod 644 /var/lib/lxcf/exjob
mkdir -p /var/log/lxcf
touch /var/log/lxcf/lxcf-messages
chmod 644 /var/log/lxcf/lxcf-messages

# check systemd for lxcf and lxcf-sched service
if [ -x /usr/bin/systemctl ] ; then
  if ! systemctl -q is-enabled lxcf.service ; then
    systemctl -q enable lxcf.service
  fi
  if ! systemctl -q is-active lxcf.service ; then
    systemctl start lxcf.service
  fi
  if ! systemctl -q is-enabled lxcf-sched.service ; then
    systemctl -q enable lxcf-sched.service
  fi
  if ! systemctl -q is-active lxcf-sched.service ; then
    systemctl start lxcf-sched.service
  fi
fi

# check on repetition execution
if [ -f /etc/lxcf/container_name ] ; then
  echo "The lxcf command is executed in the LXCF container."
  exit 1
fi

levenshtein() {
  word1=$1 ; word2=$2
  cat <<- EOF | awk -f -
	BEGIN {
	    print levdist("$word1", "$word2")
	}
	function levdist(str1, str2,    i, j, l1, l2, x, t, a, b, c) {
	    if (str1 == str2) {
	        return 0
	    } else if (str1 == "" || str2 == "") {
	        return length(str1 str2)
	    } else if (substr(str1, 1, 1) == substr(str2, 1, 1)) {
	        for (i = 2; substr(str1, i, 1) == substr(str2, i, 1); i++)
	            ;
	        return levdist(substr(str1, i), substr(str2, i))
	    } else if (substr(str1, l1 = length(str1), 1) \\
	            == substr(str2, l2 = length(str2), 1)) {
	        for (i = 1; substr(str1, l1 - i, 1) == substr(str2, l2 - i, 1); i++)
	            ;
	        return levdist(substr(str1, 1, l1 - i), substr(str2, 1, l2 - i))
	    }
	    for (i = 0; i < l2 + 1; i++)
	        x[0, i] = i
	    for (i = t = 0; i < l1; i++) {
	        x[t = !t, 0] = i + 1
	        for (j = 0; j < l2; j++) {
	            a = x[!t, j + 1] + 1
	            b = x[t, j] + 1
	            c = x[!t, j] + (substr(str1, i + 1, 1) != substr(str2, j + 1, 1))
	            x[t, j + 1] = (a < b && a < c) ? a : (b < a && b < c) ? b : c
	        }
	    }
	    return x[t, j]
	}
	EOF
}

export PATH=/usr/lib64/lxcf/sbin:$PATH

if [ $# -eq 0 ] ; then
  cat <<- "EOF"
	
	                   LXCF 0.8
	     GNU GENERAL PUBLIC LICENSE Version 2
	    Type: 'helpcmd' for help with commands
	          'exit' or '^d' to quit
	
	EOF
  PS1="LXCF # " exec /bin/sh
else
  cmds=(`ls -f --ind=none /usr/lib64/lxcf/sbin | sed '/^\.\{1,2\}$/d'`)
  for c in "${cmds[@]}" ; do [ "$c" == "$1" ] && exec "$@" ; done
  dist=() ; for c in "${cmds[@]}" ; do dist+=(`levenshtein "$c" "$1"`) ; done
  min=$dist ; for d in ${dist[@]} ; do [ $d -lt $min ] && min=$d ; done
  cand=() ; i=0
  for c in "${cmds[@]}" ; do
    [ ${dist[$i]} -eq $min ] && cand+=("$c") ; let i++
  done
  [ ${#cand[@]} -eq 1 ] && object="this" || object="one of these"
  cat <<- EOF
	${0##*/}: '$1' is not a ${0##*/} command. See '${0##*/} helpcmd'.
	
	Did you mean $object?
	EOF
  for c in "${cand[@]}" ; do echo $'\t'"$c" ; done
  exit 1
fi
