#!/bin/sh
trap '/bin/rm -f ${inFile} > /dev/null 2>&1' EXIT

######################################################################
#
# Copyright 2008, 2009, 2010, 2011, 2012  Oki Data Corporation
#
# 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; either version 2, or (at your option)
# any later version.
#
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
######################################################################
# version: 4.0
######################################################################

PATH=/bin:/usr/sbin:/usr/bin
export PATH

CAT="/bin/cat"
CUT="/usr/bin/cut"
ECHO="/bin/echo"
GREP="/bin/grep"
SED="/bin/sed"
MKTEMP="/bin/mktemp"
RM="/bin/rm"
SORT="/usr/bin/sort"

GROUP="/etc/group"

inFile=$(${MKTEMP} ${TMPDIR:-/tmp}/XXXXXX)

${RM} -f ${inFile} > /dev/null 2>&1

_jobid="${1}"
_user="${2}"
_title="${3}"
_options="${5}"

tmp_id=`${GREP} "^${_user}:" ${GROUP}| ${CUT} -d":" -f3 2> /dev/null`
_id=`${ECHO} ${tmp_id:-999999988} | ${SED} -e "s/.*\([0-9]\{9\}\)/\1/" 2>/dev/null`

OKIJOB_ACCT="@PJL OKIJOBACCOUNTJOB JOBACCOUNTID=${_id} USERID=\"${_user}\" JOBNAME=\"${_title}\""

${CAT} - > ${inFile}

LANG_STR="@PJL ENTER LANGUAGE" 

my_string=$(${GREP} -iw "^${LANG_STR}"  ${inFile} 2> /dev/null | ${SORT} -u 2> /dev/null)

if [ $? -eq 0 ] ; then
	${CAT} ${inFile} | ${SED} "s/${my_string}/${OKIJOB_ACCT}\n&/g" 2> /dev/null
else
	${CAT} ${inFile} 
fi

${RM} -f ${inFile} > /dev/null 2>&1

exit 0
