From c450e69fb1ab106ec4564aeed17c227ab77bde65 Mon Sep 17 00:00:00 2001
From: aTanW <villy@sft.ru>
Date: Fri, 13 Feb 2015 16:22:17 +0300
Subject: [PATCH] gammu: add uci config and procd service control

Signed-Off-By: Vitaly Protsko <villy@sft.ru>
---
 Makefile   |    8 +++++---
---
 utils/gammu/Makefile        |  11 ++--
 utils/gammu/files/gammurc   |  37 ------------
 utils/gammu/files/smsd      |   5 ++
 utils/gammu/files/smsd.init | 116 ++++++++++++++++++++++++++++++++++++
 4 files changed, 128 insertions(+), 41 deletions(-)
 delete mode 100644 utils/gammu/files/gammurc
 create mode 100644 utils/gammu/files/smsd
 create mode 100755 utils/gammu/files/smsd.init

diff --git a/utils/gammu/Makefile b/utils/gammu/Makefile
index 3f2137a41..a0a625cdb 100644
--- a/utils/gammu/Makefile
+++ b/utils/gammu/Makefile
@@ -1,5 +1,6 @@
 #
-# Copyright (C) 2006-2014 OpenWrt.org
+# Copyright (C) 2006-2015 OpenWrt.org
+#		2014-2015 Vitaly Protsko
 #
 # This is free software, licensed under the GNU General Public License v2.
 # See /LICENSE for more information.
@@ -63,12 +64,14 @@ define Package/gammu/install
 	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/gammu-{smsd,smsd-inject,smsd-monitor} $(1)/usr/bin
 	$(INSTALL_DIR) $(1)/usr/lib
 	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/lib/lib{Gammu*,gsmsd*} $(1)/usr/lib
-	$(INSTALL_DIR) $(1)/etc
-	$(INSTALL_CONF) ./files/gammurc $(1)/etc/gammurc
+	$(INSTALL_DIR) $(1)/etc/init.d
+	$(INSTALL_DIR) $(1)/etc/config
+	$(INSTALL_CONF) ./files/smsd $(1)/etc/config/smsd
+	$(INSTALL_BIN) ./files/smsd.init $(1)/etc/init.d/smsd
 endef
 
 define Package/gammu/conffiles
-/etc/gammurc
+/etc/config/smsd
 endef
 
 $(eval $(call BuildPackage,gammu))
diff --git a/utils/gammu/files/gammurc b/utils/gammu/files/gammurc
deleted file mode 100644
index d547c04d7..000000000
--- a/utils/gammu/files/gammurc
+++ /dev/null
@@ -1,37 +0,0 @@
-# Port		: "/dev/ttyS%" or "/dev/ircomm%" ("irda" connection), "/dev/ttyUSB%"
-#		  (instead of "%" please put "0", "1", "2", etc.)
-# Model		: use only, when Gammu doesn't recognize your phone model.
-#		  Put it here. Example values: "6110", "6150", "6210", "8210"
-# Connection	: type of connection. Use "fbus" or "mbus" or "dlr3" or
-#		  "irda" (Infrared over sockets) or "infrared" (DirectIR)
-#		  or "at19200" (AT commands on 19200, 8 bits, None parity,
-#		  1 stop bit, no flow control) or "at115200" (AT commands on
-#		  115200, 8 bits, None parity, 1 stop bit, no flow control)
-#		  or "atblue" (AT over BlueTooth) or "dlr3blue" (FBUS
-#		  over BlueTooth)
-# SynchronizeTime: If you want to set time from computer to phone during
-#		  starting connection. Do not rather use this option when want
-#		  to reset phone during connection (in some phones need to
-#		  set time again after restart)
-# Logfile	: Use, when want to have logfile from communication.
-# Logformat	: What debug info and format should be used:
-#		  "nothing" - no debug level, "text" - transmission dump in
-#		  text format, "textall" - all possible info in text format,
-#		  "errors"  - errors in text format, "binary" - transmission
-#		  dump in binary format
-# Use_Locking	: under Unix/Linux use "yes", if want to lock used device
-#		  to prevent using it by other applications
-# GammuLoc	: name of localisation file
-
-[gammu]
-
-port = /dev/ttyUSB0
-#model =
-connection = at
-#synchronizetime = yes
-#logfile =
-#logformat = errors
-use_locking = yes
-#gammuloc =
-
-# EOF /etc/gammurc
diff --git a/utils/gammu/files/smsd b/utils/gammu/files/smsd
new file mode 100644
index 000000000..d05fb255c
--- /dev/null
+++ b/utils/gammu/files/smsd
@@ -0,0 +1,5 @@
+config daemon default
+	option enabled		0
+	option device		/dev/ttyUSB0
+	option connection	at
+	option logfile		syslog
diff --git a/utils/gammu/files/smsd.init b/utils/gammu/files/smsd.init
new file mode 100755
index 000000000..0a78c4884
--- /dev/null
+++ b/utils/gammu/files/smsd.init
@@ -0,0 +1,116 @@
+#!/bin/sh /etc/rc.common
+# Copyright (C) 2015 villy@sft.ru
+
+START=98
+
+USE_PROCD=1
+PROG=/usr/bin/gammu-smsd
+CONFPFX=/var/smsd
+
+start_instance() {
+	local conf="$1"
+	local item
+	local conffile="$CONFPFX-$conf.conf"
+
+	config_get_bool item "$conf" enabled 0
+	[ $item = 0 ] && return
+
+	echo -e "; dont edit. instead look into /etc/config/smsd\n[gammu]\n" > $conffile
+
+	config_get item "$conf" device
+	[ -z "$item" ] && return
+	echo "device = $item" >> $conffile
+
+	config_get item "$conf" connection
+	[ -z "$item" ] && return
+	echo "connection = $item" >> $conffile
+
+	config_get item "$conf" logfile
+	[ -z "$item" ] && item=syslog
+	echo "logfile = $item" >> $conffile
+
+	echo -e "\n[smsd]\nphoneid = $conf\nservice = files\nlogfile = $item" >> $conffile
+	echo -e "checkbattery = 0\nhangupcalls = 1" >> $conffile
+
+	config_get item "$conf" pin
+	[ -n "$item" ] && echo "pin = $item" >> $conffile
+
+	config_get item "$conf" phonecode
+	[ -n "$item" ] && echo "phonecode = $item" >> $conffile
+
+	config_get item "$conf" send
+	[ -n "$item" ] && echo "send = $item" >> $conffile
+
+	config_get item "$conf" receive
+	[ -n "$item" ] && echo "receive = $item" >> $conffile
+
+	config_get item "$conf" timeout
+	[ -n "$item" ] && echo "commtimeout = $item" >> $conffile
+
+	config_get item "$conf" sleep
+	[ -n "$item" ] && echo "loopsleep = $item" >> $conffile
+
+	config_get item "$conf" report
+	[ -n "$item" ] && echo "deliveryreport = $item" >> $conffile
+
+	config_get item "$conf" onreceive
+	[ -n "$item" ] && echo "runonreceive = $item" >> $conffile
+
+	config_get item "$conf" onerror
+	[ -n "$item" ] && echo "runonfailure = $item" >> $conffile
+
+	config_get item "$conf" spool
+	[ -z "$item" ] && item=/var/sms
+	[ ! -d $item ] && {
+	  mkdir $item
+	  mkdir $item/inbox
+	  mkdir $item/outbox
+	  mkdir $item/sent
+	  mkdir $item/error
+	}
+	echo -e "inboxpath = $item/inbox/\noutboxpath = $item/outbox/" >> $conffile
+	echo -e "sentsmspath = $item/sent/\nerrorsmspath = $item/error/" >> $conffile
+
+	config_get item "$conf" allow
+	[ -n "$item" ] && {
+	  local data
+	  local allowfile=$CONFPFX-$conf.allow
+	  for data in $item ; do
+	    echo $data >> $allowfile
+	  done
+
+	  echo "includenumbersfile = $allowfile" >> $conffile
+	}
+
+	config_get item "$conf" deny
+	[ -n "$item" ] && {
+	  local data
+	  local denyfile=$CONFPFX-$conf.deny
+	  for data in $item ; do
+	    echo $data >> $denyfile
+	  done
+
+	  echo "excludenumbersfile = $denyfile" >> $conffile
+	}
+
+	procd_open_instance
+
+	config_get_bool item "$conf" respawn 0
+	[ $item != 0 ] && procd_set_param respawn
+
+	procd_set_param command $PROG --config=$conffile
+
+	config_get item "$conf" pidfile
+	[ -n "$item" ] && procd_append_param command --pid=$item
+
+	procd_close_instance
+}
+
+start_service() {
+	config_load smsd
+	config_foreach start_instance daemon
+}
+
+service_triggers() {
+	procd_add_reload_trigger "smsd"
+}