#!/bin/bash

. /tmp/cmdline

function doKill {
    PROCESSES=$((if [ -e /dev/ilid ]; then lsof -F p /dev/ilid | sed "s/^p//"; fi; pgrep callp; pgrep .prc) | sort -u)
    if [ "${PROCESSES}" != "" ];then
        echo "process ids are ${PROCESSES}"
        PROCESSESGROUPS=$(ps -o pgrp= -p ${PROCESSES} | sort -u)
        echo "process groups are ${PROCESSESGROUPS}"

        if [ -n "${PROCESSESGROUPS}" ]; then
            for GROUP in ${PROCESSESGROUPS}; do
                echo $(pgrep -lg ${GROUP})
                echo "kill process group ${GROUP} with $1"
                pkill -${1} -g ${GROUP}
            done
        fi

        if [ -n "${PROCESSES}" ]; then
            for PROCESS in ${PROCESSES}; do
                if ps --pid ${PROCESS} &>/dev/null; then
                    echo $(ps -o command= -p ${PROCESS})
                    echo "kill process ${PROCESS} with $1"
                    pkill -${1} ${PROCESS}
                fi
            done
        fi
        sleep 5
    fi
}

stopEyeSDN() {
    if [ -r /etc/init.d/eyesdn-cols ]; then
        service eyesdn-cols stop
    fi
    if [ -r /etc/init.d/eyesdn ]; then
        service eyesdn stop
    fi
}

#if [ "$HWTYPE" = "AtomBox" ]; then
    if [ ! -f /tmp/noled ] ;then echo "led 0 1832000" | tee /tmp/led | /usr/bin/confTool /dev/ilid 0.a/X >/dev/null 2>&1; fi
#fi

OS=$(if [ -e /etc/redhat-release ]; then cat /etc/redhat-release | head -n 1 | cut -d " " -f 1 ; else if [ -r /etc/issue ] ; then cat /etc/issue | head -n 1 | cut -d " " -f 1 ; else echo unknown ; fi; fi)

if [ "${OS}" == "CentOS" ]; then
    stopEyeSDN
fi

if [ "${OS}" == "CentOS" ]; then
    service omgproducermanager stop
    service omgwebui stop
fi

echo "stop following processes"
doKill SIGTERM
doKill SIGHUP
doKill SIGINT
doKill SIGKILL

