#!/bin/bash
#
#Copyright 2012 - 2018 CurlyMo, mk01 & mkreisl <development@xbian.org>
#
#This file is part of XBian - XBMC on the Raspberry Pi.
#
#XBian 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 3 of the License, or (at your option) any later
#version.
#
#XBian 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 XBian. If not, see <http://www.gnu.org/licenses/>

# Get list of installed services
function getInstalledServicesFn() {
	for s in $(printf "%s\n" ${!SERVICES[@]}); do
		a=${ISALIAS[$s]}; [ -z "$a" ] && u=$s || u=$a
		[ ! -e /etc/init.d/$u ] && [ ! -e /etc/init/$u.conf ] && [ ! -e $BASEPATH/config/$u.inetd ] && unset SERVICES[$s]
	done
        X=$((${#SERVICES[@]}-1))
        IDX=($(printf "%s\n" ${!SERVICES[@]} | sort))
}

in_list() 
{
#        case $PROCESSES in
#                *" $1 "*)
#                        echo "$1 ok" >> /run/xbian-config
#                        return 0
#                        ;;
#        esac
#
#        echo "$1 nok" >> /run/xbian-config
        true
#        return 1
}

function getServiceAutoStartStatusFn() {
	[ -z "$1" ] && return 1
	a=${ISALIAS[$1]}; [ -z "$a" ] || set -- $a
        [ -e $BASEPATH/config/$1.inetd ] && { while read a; do ! grep -qx ^"$a" /etc/inetd.conf && return 1; done < $BASEPATH/config/$1.inetd ; return 0; }

        grep -qx manual /etc/init/$1.override /etc/init/$1.conf 2>/dev/null && return 1
        [ ! -e /etc/init/$1.conf ] && [ ! -e /etc/rc2.d/S??$1 -a ! -e /etc/rcS.d/S??$1 ] && return 1
	return 0
}

refreshProcessesFn()
{
#        echo "Reloading process list" >> /run/xbian-splash
        PROCESSES=$(ps eax | awk '{print $5" "$6}')
}
function getServiceRunningStatusFn() {
        [ -z "$1" ] && return 2
        a=${ISALIAS[$1]}; [ -z "$a" ] || set -- $a
	[ -e $BASEPATH/config/$1.inetd ] && { getServiceAutoStartStatusFn $1 && { getServiceRunningStatusFn "openbsd-inetd" && return 0; } || return 1; }

        p=${SERVICES[$1]//,/ }

	[ -z "$PROCESSES" ] && refreshProcessesFn
	[ -z "$p" ] && { [[ $PROCESSES =~ "$1" ]] && return 0 || { status "$1" 2>/dev/null| grep -q "start/running"; return $?; }; }
	(
		IFS=$' '
		for pn in $p; do
			[[ $PROCESSES =~ "$pn" ]] || exit 1
		done
		exit 0
	)
	return $?
}

function getServiceNameFn() {
        NAME=$(echo ${IDX[$1]})
}

function getServiceIDFn() {
	I=0
	for s in $(printf "%s\n" ${IDX[@]} | sort); do
                [ "$1" = $s ] && return $I
		I=$(($I+1));
	done
	return 999
}

function checkServiceExistsFn() {
	a=${ISALIAS[$1]}; [ -z "$a" ] || set -- $a
	[ -e "/etc/init.d/$1" -o -e "/etc/init/$1.conf" -o -e $BASEPATH/config/$1.inetd ] && return 0
	return 1
}

function restartServiceFn() {
	a=${ISALIAS[$1]}; [ -z "$a" ] || set -- $a $2
	[ -z "$2" ] && c="restart" || c="$2"

	if [ -e $BASEPATH/config/$1.inetd ]; then
		[ "$c" =  stop ] && return 1
		restartServiceFn "openbsd-inetd" restart && return 0 || return 1
	elif [ -f "/etc/init/$1.conf" ]; then
		[ $c = restart ] && c=reload
		$c -q $1
	else 
		/etc/init.d/$1 $c > /dev/null
	fi

	r=$?
	[ "$c" = stop ] && sleep 1
	refreshProcessesFn
	return $r
}

function startServiceFn() {
        restartServiceFn $1 start
}

function stopServiceFn() {
        restartServiceFn $1 stop
}

function disableAutoStartFn() {
	a=${ISALIAS[$1]}; [ -z "$a" ] || set -- $a
        [ -e $BASEPATH/config/$1.inetd ] && { 	while read service details; do
						    [ -z "$service" ] && continue
						    service=$(echo $service | awk '{print $1}')
						    update-inetd --multi --disable $service || return 1
						done < $BASEPATH/config/$1.inetd; return 0; }

	if [ -f "/etc/init/$1.conf" ]; then
		grep -xq manual "/etc/init/$1.override" 2>/dev/null || {
			[ -f "/etc/init/$1.override" ] && sed -i "1 i\manual" "/etc/init/$1.override" || echo "manual" > "/etc/init/$1.override"
		}
		grep -xq manual "/etc/init/$1.override"; return $?
        else
		update-rc.d -f "$1" remove > /dev/null 2>&1;insserv -rf "/etc/init.d/$1"  > /dev/null 2>&1; return $?
        fi
}

function enableAutoStartFn() {
	a=${ISALIAS[$1]}; [ -z "$a" ] || set -- $a
        [ -e $BASEPATH/config/$1.inetd ] && { 	while read service; do
						    details="$service"
						    [ -z "$service" ] && continue
						    service=$(echo "$service" | sed -r 's/[\\ ]+/[\\t\\ ]\*/g' | sed 's/-/\\-/g' )
						    grep -q "$service" /etc/inetd.conf || echo "$details" >> /etc/inetd.conf
						    update-inetd --multi --enable $(echo $details |awk '{print $1}') || return 1
						done < $BASEPATH/config/$1.inetd; return 0; }

	if [ -f "/etc/init/$1.conf" ]; then
		grep -xq manual "/etc/init/$1.override" && sed -i "/^manual$/d" "/etc/init/$1.override"
		[ "$(stat -c %s "/etc/init/$1.override")" -eq 0 ] && rm -f /etc/init/$1.override && return 0
		return $(grep -cx manual "/etc/init/$1.override")
        else
		update-rc.d -f "$1" remove > /dev/null 2>&1 ;insserv -f "/etc/init.d/$1" > /dev/null 2>&1; return $?
        fi
}

function getServiceConfFn() {
	DATA=();
        getServiceIDFn $1; I=$?
        DATA[0]=$1
	DATA[1]=${SERVICES[$1]}

        [ $I != 999 ] || return 1
        return 0 
}

function updateServiceConfFn() {
        [ -z "$1" ] && return 1
	if [ -f "$BASEPATH/config/services" ]; then
		sed -i "/$1:/d" $BASEPATH/config/services 
	fi
	echo "$1:$2" >> $BASEPATH/config/services;
	if [ -f "$BASEPATH/config/services" ] && [ $(cat $BASEPATH/config/services | grep -c "$1:$2" ) -eq 1 ]; then
		return 0;
	else
		return 1;
	fi
}

function deleteServiceFn() {
        [ -z "$1" ] && return 1
	if [ -f "$BASEPATH/config/services" ]; then
		sed -i "/$1:/d" $BASEPATH/config/services;
	fi
	if [ -f "$BASEPATH/config/services" ] && [ $(cat $BASEPATH/config/services | grep -c "$1:") -eq 0 ]; then
		return 0
	else
		return 1
	fi
}
