#!/bin/sh INPUTDIR=/usr/hl7/var/spool/ANAE_IOP ERRORDIR=/usr/hl7/var/spool/ANAE_IOP/ERROR/ MSGDIR=$INPUTDIR OUTPUTDIR=/usr/hl7/var/log/ANAE_IOP ACKDIR=$OUTPUTDIR/ACK JOURNAL=$OUTPUTDIR/REQ RUNDIR=/var/tmp PATH="/usr/hl7/bin:$PATH" HOST=localhost SERVICE=carevuehl7 REPORT=/usr/hl7/var/ANAE_IOP/REPORT if [ -z "$PAGER" ] ; then PAGER=more ; fi # # ADMIN and CAREVUE, CVHOST, CVUSER are set from either the environment # or from the files /usr/hl7/etc/{admin,carevue} # if [ -z "$ADMIN" ] then if [ -r /usr/hl7/etc/admin ] then export ADMIN=`cat /usr/hl7/etc/admin` else if [ -n "$USER" ] then export ADMIN="$USER" else echo who are you? please define USER, ADMIN or /usr/hl7/etc/admin >&2 exit 2 fi fi fi if [ -z "$CAREVUE" ] then if [ -r /usr/hl7/etc/carevue ] then export CAREVUE=`cat /usr/hl7/etc/carevue` export CVUSER=`expr "$CAREVUE" : '^\([^@]*\)@.*$'` export CVHOST=`expr "$CAREVUE" : '^[^@]*@\(.*\)$'` else echo must define environment CAREVUE or /usr/hl7/etc/carevue >&2 exit 2 fi fi mlcishost=$CVHOST mlcisuser=$CVUSER mlcisfile=/usr/M1215A/carevue/data/logs/ml.cis cd $RUNDIR allmsg=$MSGDIR/msg*.dat msg="" ack="" lck="" skip=0 retry=0 smid="" # # no user interaction is required in batch mode, error reports are mailed # to the administrator instead and standard out goes to logfile # if [ "$1" = "batch" ] then batch=true exec >>/tmp/hl7tocv.log else batch=false fi # # locking: hl7tocv can run only once per input dir # RUNLOCK=$INPUTDIR/hl7tocv.lck ERROR_MARK=$INPUTDIR/hl7tocv.err # lock_made=false INTR_DFL="finish k 1" trap "$INTR_DFL" 15 lock() { lock_val=$USER'@'`hostname`'['$$']' if [ -f $RUNLOCK ] then lock_made=false else echo $lock_val > $RUNLOCK lock_made=true fi if $lock_made then return 0 else return 1 fi } unlock() { if [ "$1" = "f" ] || $lock_made then rm -f $RUNLOCK fi } remote() { rsh -K -l $mlcisuser $mlcishost " . /usr/M1215A/carevue/etc/carevue_env ; . /usr/M1215A/carevue/etc/go_charting ; $*" 2>/tmp/$$.err error=`cat /tmp/$$.err` if [ -n "$error" ] then echo $error >&2 fi rm -f /tmp/$$.err } report() { echo REQUEST $msg: vimsg < $msg echo echo RESPONSE $ack: vimsg < $ack echo echo CAREVUE LOGFILE: $mlcisuser@$mlcishost:$mlcisfile remote tail $mlcisfile } sendmsg() { echo printf "$msg ..." if [ ! -f $lck ] then if [ -e $ack ] then if ! ( yes |rm $ack ) then echo "can't remove old ACK file" return 3 fi fi cat $msg \ |tr '\344\366\374\304\326\334\337' '\314\316\317\330\332\333\336' \ |fclient -q - $SERVICE $HOST >$ack result=$? if [ 0$result -eq 0 ] then echo -n "success" cp $msg $JOURNAL && rm -f $msg return 0 else if $batch && [ 0$result -eq 1 ] # AE error, will be reported but does not cause the message to be resent then echo -n "error reported" report |mail -s "HL7 MESSAGE WARNING REPORT" $ADMIN cp $msg $JOURNAL && rm -f $msg return 1 else echo -n "failed" return 2 fi fi else echo -n "locked" return 1 fi } finish() { exit_val=0 for o in $* do case $o in i) echo echo interrupt ;; k) echo echo killed ;; q) if ! $batch then echo -n 'do you really want to exit [yn]? ' yn=n read yn $ERROR_MARK else if ! $batch then if [ -f $ERROR_MARK ] then echo -n "ERROR MARK: " cat $ERROR_MARK rm -f $ERROR_MARK fi fi fi exit $exit_val } interrupt() { echo echo interrupt if ! $batch then while true do echo -n '(v)iew, (e)dit, (l)og, (r)eport, (d)elete, (s)skip, (c)ontinue, e(x)it > ' read ch >$REPORT ;; esac done fi } ############################## MAIN ############################## if ! $batch then stty intr ^C fi if ! lock then if ! $batch then trap "unlock f" 2 echo -n "waiting for lock to become free (^C to forcefully remove lock)" while ! lock do sleep 1 echo -n . done echo ok trap "$INTR_DFL" 2 else echo $INPUTDIR is locked finish 1 fi fi ################################################################ # error mark does not stop the queue # 16.12.1996 UF # now copy Messages to ERROR-Directory ################################################################# remove_error=false if $batch && [ -f $ERROR_MARK ] then rm -f $ERROR_MARK remote pmstart -r FSISERVER remote pmstart -r FSICLIENT # exit 1 remove_error=true fi if ! $batch then # remove any previously issued error message remote smtool -f HL7_ERROR # remove any previously set ERROR_MARK if [ -f $ERROR_MARK ] then echo -n "ERROR MARK: " cat $ERROR_MARK rm -f $ERROR_MARK fi fi if ( echo $allmsg | grep -q msg[0-9]*.dat ) then if $batch then date fi for msgi in $allmsg do trap "" 2 msg=$msgi dir=`dirname $msg` base=`basename $msg .dat` lck=$dir/${base}.lck ack=$ACKDIR/${base}.ack sendmsg $msg $lck $ack retries="" while [ 0$? -eq 02 ] || [ 0$retry -eq 01 ] do trap interrupt 2 retry=0 skip=0 retries="x$retries" if [ "$retries" = "xxxxxxxxxx" ] then if $batch then if ! $remove_error then finish 2 else remote smtool -f HL7_ERROR smid=`remote smtool -t 30 -D HL7_ERROR \'Fehler bei HL7 0xdbbertra gung -- Bitte Systemmanager verst0xccndigen!\'` report |mail -s "HL7 MESSAGE ERROR REPORT" $ADMIN mv $msg $ERRORDIR rm -f $ERROR_MARK remove_error=false finish 0 fi fi fi test 0$skip -eq 01 && break test 0$retry -eq 01 && continue echo -n "." sleep 2 test 0$skip -eq 01 && break test 0$retry -eq 01 && continue echo -n "." sleep 2 test 0$skip -eq 01 && break test 0$retry -eq 01 && continue echo -n "." sleep 2 test 0$skip -eq 01 && break test 0$retry -eq 01 && continue echo -n "." sleep 2 test 0$skip -eq 01 && break test 0$retry -eq 01 && continue echo printf "retry: $msg ..." sendmsg $msg $lck $ack done test -n "$smid" && remote smtool -f HL7_ERROR smid="" done echo echo ready fi finish 0