summaryrefslogtreecommitdiff
path: root/conf/dist/kolabsrv.in
blob: efca5df5604acb48da4192a1bfd6af7a407ca16f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!@BASH@

# check if everything is correct
function check() {
	if [ -x "$RCDIR/$SERVICE" ]; then
		case $PARAM in
			start|START)     PARAM="start" && rcrun;;
			stop|STOP)       PARAM="stop" && rcrun;;
			restart|RESTART) PARAM="restart" && rcrun;;
			reload|RELOAD)   PARAM="reload" && rcrun;;
			status|STATUS)   PARAM="status" && rcrun;;
			*) echo "ERROR: Parameter $PARAM not supported!"
		esac
	else
		echo "ERROR $RCDIR/$SERVICE NOT found - abort" >&2
	fi	
}

# starts/stops/restarts... the service
function rcrun() {
	$RCDIR/$SERVICE $PARAM
	retc=$?
	if [ $retc -ne 0 ] && [ $retc -ne 3 ]; then
		( echo "ERROR: $RCDIR/$SERVICE $PARAM failed"
		  echo "Run: @KOLABRC@ rc all stop"
		  echo "to stop all services"
		) >&2
		exit 1
	fi
}	

# map distribution specific rc script names
function getServiceName() {
	sname=$1
	# Perhaps better use:
	# lsb_release -i | cut -f2
	if [ -e /etc/SuSE-release ]; then
		case $sname in
			apache) SERVICE="apache2" ;;
			openldap) SERVICE="@LDAPD@" ;;
			sasl) SERVICE="saslauthd" ;;
			imapd) SERVICE="cyrus" ;;
			amavisd) SERVICE="amavis" ;;
			clamav) SERVICE="clamd" ;;
			*) SERVICE=$sname
		esac
	elif [ -e /etc/debian_version ]; then
		case $sname in
			apache) SERVICE="apache2" ;;
			openldap) SERVICE="@LDAPD@" ;;
			sasl) SERVICE="saslauthd" ;;
			imapd) SERVICE="kolab-cyrus" ;;
			amavisd) SERVICE="amavis" ;;
			clamav) SERVICE="clamav-daemon" ;;
			spamd) SERVICE="spamassassin" ;;
			freshclam) SERVICE="clamav-freshclam" ;;
			*) SERVICE=$sname
		esac
	fi
}	

# variables
RC=$1
SERVICE=$2
PARAM=$3

# Use the service name as defined in the openpkg environment
# Kolab installations on native distributions, should adap service names
# with the function getServiceName
SERVICES="openldap sasl spamd amavisd apache freshclam imapd postfix"
SERVICES="$SERVICES clamav kolabd"

case $RC in 
rc)
	# okay
	;;
*)
	SERVICES=$(echo $SERVICES | tr -s " " | sed 's, ,|,'g)

	( echo "ERROR: $RC not supported"
	  echo "Use: @KOLABRC@ rc [all|$SERVICES] [stop|start|restart|status]"
	) >&2

	exit 1
	;;
esac

if [[ "$SERVICE" != "all" ]]; then

	SERVICE_FOUND=no

	for S in $SERVICES; do
		if [[ $S == "$SERVICE" ]]; then
			SERVICE_FOUND=yes
			SERVICES=$SERVICE
			break
		fi
	done

	if [[ $SERVICE_FOUND == "no" ]]; then
		( echo "ERROR: unknown service: $SERVICE"
		  echo "Use: @KOLABRC@ rc [all|$SERVICES] [stop|start|restart|status]"
		) >&2

		exit 1
	fi
else
	if [[ "$PARAM" == "stop" ]]; then
		SERVICES=$(tr " " "\n" <<< "$SERVICES" | tac)
	fi
fi

[[ -f @sysconfdir@/kolab/profile.sh ]] && . @sysconfdir@/kolab/profile.sh

RCDIR=@kolab_rcdir@

for SERVICE in $SERVICES; do
	# Only start the virus scanning dependent services in case
	# virus scannning has been enabled on the kolab web admin interface.
	# An service is always stopped independent of the virus service	
	# scanning setting.
	if [[ "$PARAM" == "start" ]] || [[ "$PARAM" == "restart" ]]; then
		case $SERVICE in
		amavisd|clamav|freshclam|spamd)
			if [[ "$ENABLE_VIRUS_SCAN" == "no" ]]; then
				RUN_SERVICE=no
			else
				RUN_SERVICE=yes
			fi
			;;
		*)
			RUN_SERVICE=yes
			;;
		esac
	else
		RUN_SERVICE=yes
	fi

	[[ $RUN_SERVICE == "yes" ]] && {
		getServiceName $SERVICE
		check $SERVICE
	}
done