#!/bin/bash
# copyright (C) 2013 FUJITSU LIMITED All Rights Reserved

# 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}} != 0 ]; then
    echo "error: Because you are not root, you cannot execute this command. "
    exit 1
fi


FLG_C=0
PARM_C=""
FLG_A=0
PARM_A=""
FLG_I=0
PARM_I=""
FLG_N=0
PARM_N=""
FLG_M=0
PARM_M=""
FLG_S=0
PARM_S=""

# check options
while getopts c:a:i:n:m:s: OPT
do
  case $OPT in
    "c" ) FLG_C=1; PARM_C="-c"; CPURATE=$OPTARG  ;;
    "a" ) FLG_A=1; PARM_A="-a"; CPUALLOCATION=$OPTARG  ;;
    "i" ) FLG_I=1; PARM_I="-i"; CPUN=$OPTARG  ;;
    "n" ) FLG_N=1; PARM_N="-n"; NUMA=$OPTARG  ;;
    "m" ) FLG_M=1; PARM_M="-m"; MEMLIMIT=$OPTARG  ;;
    "s" ) FLG_S=1; PARM_S="-s"; MEMSWLIMIT=$OPTARG  ;;
  esac
done

shift `expr $OPTIND - 1`

if [ $# -ne 2  -a  $# -ne 3 ] ; then
  echo "usage : set-n [ -c CPURATE ] [ -a CPUALLOCATION ] [ -i CPUN ] [ -n NUMA ] [ -m MEMLIMIT ] [ -s MEMSWLIMIT ] NAME NUM"
  echo "        set-n [ -c CPURATE ] [ -a CPUALLOCATION ] [ -i CPUN ] [ -n NUMA ] [ -m MEMLIMIT ] [ -s MEMSWLIMIT ] NAME NUM1 NUM2"
  exit -1
fi

if [ $FLG_A == 1 -a $FLG_I == 1 ] ; then 
  echo "error : -a and -i cannot be specified at the same time. "
  exit -1
fi


MAXNUM=50
LNAME=$1

if [ $# -eq 3 ] ; then
  NUM1=$2
  if ! expr "$NUM1" : '[0-9]*' > /dev/null ; then
    echo "illegal number :" $NUM1
    exit -1
  fi

  NUM2=$3
  if ! expr "$NUM2" : '[0-9]*' > /dev/null ; then
    echo "illegal number :" $NUM2
    exit -1
  fi
else
  NUM1=1
  NUM2=$2
  if ! expr "$NUM2" : '[0-9]*' > /dev/null ; then
    echo "illegal number :" $NUM2
    exit -1
  fi
fi

if ! /usr/lib64/lxcf/lxcf-parmchk-num $NUM1 ; then
  echo $NUM1 "is not a integer"
  exit 1
fi

if ! /usr/lib64/lxcf/lxcf-parmchk-num $NUM2 ; then
  echo $NUM2 "is not a integer"
  exit 1
fi

if [ $NUM1 -le 0 -o $NUM1 -gt $MAXNUM ]; then
  echo "illegal range :" $NUM1
  exit -1
fi

if [ $NUM1 -ge $NUM2 -o $NUM2 -le 0 -o $NUM2 -gt $MAXNUM ]; then
  echo "illegal range :" $NUM2
  exit -1
fi

coren=0
maxcore=`cat /proc/cpuinfo | egrep processor | tail -n 1 | awk '{printf "%d", $3}'`

if [ $CPUALLOCATION -gt `expr $maxcore + 1` ]; then
  echo "error : cpu allocation is greater than" `expr $maxcore + 1`
  exit -1
fi

printf "Name\tCPU\n"

for i in `seq $NUM1 $NUM2`
do
  if [ $FLG_A == 1 ]; then
    PARM_I="-i"
    coren2=`expr $coren + $CPUALLOCATION - 1`
    if [ $CPUALLOCATION == 1 ] ; then
      CPUN=$coren
    else
      if [ $coren2 -gt $maxcore ] ; then
        coren3=`expr $coren2 - $maxcore - 1`
        if [ $coren == $maxcore ]; then
          if [ $coren3 == 0 ]; then
            CPUN=`echo ${coren}",0"`
          else
            CPUN=`echo ${coren}",0-"${coren3}`
          fi
        else
          if [ $coren3 == 0 ]; then
            CPUN=`echo ${coren}"-"${maxcore}",0"`
          else
            CPUN=`echo ${coren}"-"${maxcore}",0-"${coren3}`
          fi
        fi
        coren2=$coren3
      else
        CPUN=`echo ${coren}"-"${coren2}`
      fi
    fi
    coren=`expr $coren2 + 1`
  fi

  # set parameter variable
  parmvar=`echo "${PARM_C} ${CPURATE} ${PARM_I} ${CPUN} ${PARM_N} ${NUMA} ${PARM_M} ${MEMLIMIT} ${PARM_S} ${MEMSWLIMIT}"`

  echo "`printf "%s%04d\t" ${LNAME} ${i}`${CPUN}"
  /usr/lib64/lxcf/sbin/set ${parmvar} ${LNAME}`printf "%04d" ${i}`

  if [ $coren -gt $maxcore ]; then
    coren=0
  fi
done

exit 0

