#!/bin/bash

vercomp ()
{
    if [[ $1 == $2 ]]; then
        return 0
    fi
    local IFS=.
    local i ver1=($1) ver2=($2)
    # fill empty fields in ver1 with zeros
    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)); do
        ver1[i]=0
    done
    for ((i=0; i<${#ver1[@]}; i++)); do
        if [[ -z ${ver2[i]} ]]; then
            # fill empty fields in ver2 with zeros
            ver2[i]=0
        fi
        if ((10#${ver1[i]} > 10#${ver2[i]})); then
            return 1
        fi
        if ((10#${ver1[i]} < 10#${ver2[i]})); then
            return 2
        fi
    done
    return 0
}


writeTestConfigLua()
{
    cat >/data/config.lua <<EOF
config = {
	struct_Box = {
		bool_GatewayMode = true,
		bool_SimpleConfigMode = false,
		string_TimeZone = "Europe/Berlin",
		string_NtpServer = "pool.ntp.org",
		ushort_IsdnPcmPairs = 1,
		bool_SipTrunkRegister = false,
		string_SipProxyHost = "",
		string_SipProxyPort = "5060",
		bool_PresenceHandling = false,
		string_PresenceHandlerPort = "8001",
		string_PresenceHandlerAddress = "",
		string_PresenceHandlerIpMode = "TLS",
		bool_IsdnT1 = false,
		bool_IsdnUlaw = false,
		ushort_IsdnOutboundTypeOfNumber = 1,
		ushort_IsdnOutboundNumberingPlan = 2,
		string_IsdnClockMaster = "auto",
		bool_SipSendT38Reinvite = false,
		bool_VoiceActivityDectection = false,
		bool_VoipEchoCanceller = false,
		ushort_VoipPacketTime = 20,
		string_earlyMedia = "on",
		string_earlyProgress = "on",
		string_VoipAdditionalParams = "",
		string_SipSupportPrack = "supported",
		string_SipSupportSRTP = "supported",
		string_SipFromPhoneContext = "",
		string_SipToPhoneContext = "",
		bool_SipSendNoUserPhoneFrom = true,
		bool_SipSendNoUserPhoneTo = true,
		bool_SipNoAlert = false,
		bool_SipOnlyOneAlert = true,
		bool_SipNoSdpIn18x = false,
		ushort_SipInviteTimeout = 60,
		ushort_SipInviteFallbackTimeout = 5,
		ushort_SipTryingTimeout = 60,
		string_SipOcsDomain = "",
		string_SipBindPort = "5060",
		string_SipTlsBindPort = "5061",
		bool_SipNoProgressToIsdn = false,
		bool_SipKeepRingingOnTemporaryNotAvailable = false,
		ushort_OptionsInterval = 0,
		bool_SipAlwaysRtpOnRinging = true,
		string_RingBackCountry = "49",
		bool_SipIgnorePiOnRinging = false,
		bool_SipIgnorePiOnProgress = false,
		bool_RingBackToneToIsdn = true,
		bool_RingBackToneToRtp = true,
		bool_CheckCallerIpAddress = false,
		string_PreferedIpMode = "4",
		bool_UseToForRouting = false,
		string_FaxMailHost = "",
		string_Hostname = "",
		string_HostnameForTls = "",
		struct_Codecs = {
			[1] = {
				string_Codec = "pcma/8000",
				string_Name = "G.711a (a-law)",
				short_PayLoad = 8,
				bool_Active = true
			},
			[2] = {
				string_Codec = "pcmu/8000",
				string_Name = "G.711u (u-law)",
				short_PayLoad = 0,
				bool_Active = true
			},
			[3] = {
				string_Codec = "G721/8000",
				string_Name = "ITU-T G.721 ADPCMLinear audio 32 kbit/s",
				short_PayLoad = 2,
				bool_Active = false
			},
			[4] = {
				string_Codec = "GSM/8000",
				string_Name = "European GSM Full Rate audio 13 kbit/s",
				short_PayLoad = 3,
				bool_Active = false
			},
			[5] = {
				string_Codec = "G723/8000",
				string_Name = "ITU-T G.723.1 audio",
				short_PayLoad = 4,
				bool_Active = false
			},
			[6] = {
				string_Codec = "G722/8000",
				string_Name = "ITU-T G.722 audio 64 kbit/s",
				short_PayLoad = 9,
				bool_Active = false
			},
			[7] = {
				string_Codec = "L16/44100",
				string_Name = "Linear PCM 16-bit audio",
				short_PayLoad = 10,
				bool_Active = false
			},
			[8] = {
				string_Codec = "G729/8000",
				string_Name = "ITU-T G.729 and G.729a audio 8 kbit/s",
				short_PayLoad = 18,
				bool_Active = false
			},
			[9] = {
				string_Codec = "G726-16/8000",
				string_Name = "ITU-T G.726 audio 16 kbit/s",
				short_PayLoad = 102,
				bool_Active = false
			},
			[10] = {
				string_Codec = "G726-24/8000",
				string_Name = "ITU-T G.726 audio 24 kbit/s",
				short_PayLoad = 103,
				bool_Active = false
			},
			[11] = {
				string_Codec = "G726-32/8000",
				string_Name = "ITU-T G.726 audio 32 kbit/s",
				short_PayLoad = 104,
				bool_Active = false
			},
			[12] = {
				string_Codec = "G726-40/8000",
				string_Name = "ITU-T G.726 audio 40 kbit/s",
				short_PayLoad = 105,
				bool_Active = false
			},
			[13] = {
				string_Codec = "SILK/8000",
				string_Name = "SILK narrow band 8kHz",
				short_PayLoad = 106,
				bool_Active = false
			},
			[14] = {
				string_Codec = "SILK/12000",
				string_Name = "SILK medium band 12kHz",
				short_PayLoad = 107,
				bool_Active = false
			},
			[15] = {
				string_Codec = "SILK/16000",
				string_Name = "SILK wide band 16kHz",
				short_PayLoad = 108,
				bool_Active = false
			},
			[16] = {
				string_Codec = "SILK/24000",
				string_Name = "SILK super wide band 24kHz",
				short_PayLoad = 109,
				bool_Active = false
			},
			[17] = {
				string_Codec = "OPUS/48000",
				string_Name = "OPUS 48000 kbit/s",
				short_PayLoad = 110,
				bool_Active = false
			}
		},
		string_SyslogDestination = "10.6.0.26",
		string_SyslogPort = "514",
		string_SyslogProtocol = "udp",
		string_SnmpLocation = "",
		string_SnmpContact = "root",
		string_SnmpRoc = "public",
		string_SnmpRwc = "private",
		string_SnmpTrapsink = "127.0.0.1",
		bool_SnmpRawConfig = false,
		string_SnmpRawConfigContent = "",
		string_mux_jobcontrol_dbg = "1",
		string_mux_isdn_dbg = "1",
		string_mux_sip_dbg = "1",
		string_mux_options_dbg = "0",
		string_sip_isdn_dbg = "3",
		string_jobcontrol_isdn_dbg = "0",
		string_command_dbg = "0",
		string_ict_dbg = "0",
		string_ip_dbg = "0",
		string_options_dbg = "0",
		string_AddJcPar = "",
		string_DbgDL1 = "0",
		string_DbgDL2 = "1",
		string_DbgDL3 = "0",
		string_DbgDL4 = "0",
		string_AddDcPar = "",
		string_DbgFc = "0",
		string_DbgT30 = "0",
		string_DbgT4 = "0",
		string_DbgModem = "0",
		string_DbgLapi = "0",
		string_DbgSnfs = "0",
		string_AddBcPar = "",
		string_T38Debug = "0",
		string_AddT38Par = "",
		string_AddIctPar = "",
		string_AddRtpPar = "",
		struct_DChannel = {
			[1] = {
				string_IlidSlot = "1",
				string_IlidIndex = "a",
				string_IlidType = "S0",
				string_IlidTitle = "",
				string_IlidDescription = "",
				string_UI_Caption = {
					[1] = "",
					[2] = ""
				},
				bool_CfgValid = true,
				string_ExtLength = "3",
				string_IsdnBase = "",
				string_DialPrefix = "0",
				ushort_BChannelNum = 2,
				string_UseMsn = "any",
				long_Protocol = 3,
				bool_Qsig = false,
				bool_TerminationEnabled = true,
				bool_Crc4 = false,
				ushort_UseChannels = 2,
				ushort_UseChannelsRX = 2,
				ushort_UseChannelsTX = 2,
				ushort_DialTimeout = 0,
				short_FacilityMode = -1,
				ushort_PPFlags = 0,
				string_Costs = "",
				string_AdditionalPar = "",
				string_CpnPrefixInterNational = "",
				string_CpnPrefixNational = "",
				string_CpnPrefixSubscriber = "",
				bool_PrefixAlsoOnCpn = true,
				bool_PrefixAlsoOnCgn = true,
				bool_PrefixAlsoOnRgn = true,
				string_OutboundPrefixInterNational = "",
				string_OutboundPrefixNational = "",
				string_OutboundPrefixSubscriber = "",
				bool_OutboundPrefixAlsoOnCpn = true,
				bool_OutboundPrefixAlsoOnCgn = true,
				bool_OutboundPrefixAlsoOnRgn = true,
				struct_GateParams = {
					ulong_Mode = 2,
					string_UseConfigFromChannel = "",
					string_SimpleDestination = "",
					string_SimplePilotNumber = "",
					string_SimpleNumMatch = "",
					string_SimpleNumReplace = "",
					string_SimpleRedirExceptions = "",
					bool_SimpleRedirSimulation = false,
					string_InternationalPhone = "",
					string_CountryCode = "49",
					string_SipProtocol = "TCP",
					struct_CallProcTable = {
						[1] = {
							string_Description = "Music",
							string_NumberList = "",
							string_SpecificConfigSet = "",
							bool_InvertNumberList = false,
							string_cngDetection = "off",
							string_cngDetectionTimeout = "15",
							string_action = "stop",
							string_disabled = "no",
							string_Destination = "ISDN",
							string_RegularExpression = "^(.*),(.*),(.*),(.*)",
							string_replacedCalledPartyNumber = "play file /usr/fb/moh.raw",
							string_replacedCallingPartyNumber1 = "\\2",
							string_replacedCallingPartyNumber2 = "\\3",
							string_replacedRedirectedPartyNumber = "\\4",
							string_replacedFaxCSID = "",
							string_sipDefaultGatewayIpAddress = "",
							string_sipDefaultGatewayIpMode = "TCP",
							string_sipDefaultGatewayIpPort = "5060",
							string_AdditionalParameters = "",
							string_faxRouting = "off",
							string_faxDestination = "SIP",
							string_faxRegularExpression = "(.*),(.*),(.*),(.*)",
							string_faxReplacedCalledPartyNumber = "\\1",
							string_faxReplacedCallingPartyNumber1 = "\\2",
							string_faxReplacedCallingPartyNumber2 = "\\3",
							string_faxReplacedRedirectedPartyNumber = "\\4",
							string_faxReplacedFaxCSID = "",
							string_faxSipDefaultGatewayIpAddress = "",
							string_faxSipDefaultGatewayIpMode = "TCP",
							string_faxSipDefaultGatewayIpPort = "5060",
							string_faxAdditionalParameters = ""
						}
					}
				},
				string_IlidSubType = "",
				string_SpecificConfigSet = ""
			},
			[2] = {
				string_IlidSlot = "1",
				string_IlidIndex = "b",
				string_IlidType = "S0",
				string_IlidTitle = "",
				string_IlidDescription = "",
				string_UI_Caption = {
					[1] = "",
					[2] = ""
				},
				bool_CfgValid = true,
				string_ExtLength = "3",
				string_IsdnBase = "",
				string_DialPrefix = "0",
				ushort_BChannelNum = 2,
				string_UseMsn = "any",
				long_Protocol = 3,
				bool_Qsig = false,
				bool_TerminationEnabled = true,
				bool_Crc4 = false,
				ushort_UseChannels = 2,
				ushort_UseChannelsRX = 2,
				ushort_UseChannelsTX = 2,
				ushort_DialTimeout = 0,
				short_FacilityMode = -1,
				ushort_PPFlags = 0,
				string_Costs = "",
				string_AdditionalPar = "",
				string_CpnPrefixInterNational = "",
				string_CpnPrefixNational = "",
				string_CpnPrefixSubscriber = "",
				bool_PrefixAlsoOnCpn = true,
				bool_PrefixAlsoOnCgn = true,
				bool_PrefixAlsoOnRgn = true,
				string_OutboundPrefixInterNational = "",
				string_OutboundPrefixNational = "",
				string_OutboundPrefixSubscriber = "",
				bool_OutboundPrefixAlsoOnCpn = true,
				bool_OutboundPrefixAlsoOnCgn = true,
				bool_OutboundPrefixAlsoOnRgn = true,
				struct_GateParams = {
					ulong_Mode = 2,
					string_UseConfigFromChannel = "1.a",
					string_SimpleDestination = "",
					string_SimplePilotNumber = "",
					string_SimpleNumMatch = "",
					string_SimpleNumReplace = "",
					string_SimpleRedirExceptions = "",
					bool_SimpleRedirSimulation = false,
					string_InternationalPhone = "",
					string_CountryCode = "49",
					string_SipProtocol = "TCP",
					struct_CallProcTable = {
						[1] = {
							string_Description = "Music",
							string_NumberList = "",
							string_SpecificConfigSet = "",
							bool_InvertNumberList = false,
							string_cngDetection = "off",
							string_cngDetectionTimeout = "15",
							string_action = "stop",
							string_disabled = "no",
							string_Destination = "ISDN",
							string_RegularExpression = "^(.*),(.*),(.*),(.*)",
							string_replacedCalledPartyNumber = "play file /usr/fb/moh.raw",
							string_replacedCallingPartyNumber1 = "\\2",
							string_replacedCallingPartyNumber2 = "\\3",
							string_replacedRedirectedPartyNumber = "\\4",
							string_replacedFaxCSID = "",
							string_sipDefaultGatewayIpAddress = "",
							string_sipDefaultGatewayIpMode = "TCP",
							string_sipDefaultGatewayIpPort = "5060",
							string_AdditionalParameters = "",
							string_faxRouting = "off",
							string_faxDestination = "SIP",
							string_faxRegularExpression = "(.*),(.*),(.*),(.*)",
							string_faxReplacedCalledPartyNumber = "\\1",
							string_faxReplacedCallingPartyNumber1 = "\\2",
							string_faxReplacedCallingPartyNumber2 = "\\3",
							string_faxReplacedRedirectedPartyNumber = "\\4",
							string_faxReplacedFaxCSID = "",
							string_faxSipDefaultGatewayIpAddress = "",
							string_faxSipDefaultGatewayIpMode = "TCP",
							string_faxSipDefaultGatewayIpPort = "5060",
							string_faxAdditionalParameters = ""
						}
					}
				},
				string_IlidSubType = "",
				string_SpecificConfigSet = ""
			},
			[3] = {
				string_IlidSlot = "1",
				string_IlidIndex = "c",
				string_IlidType = "S0",
				string_IlidTitle = "",
				string_IlidDescription = "",
				string_UI_Caption = {
					[1] = "",
					[2] = ""
				},
				bool_CfgValid = true,
				string_ExtLength = "3",
				string_IsdnBase = "",
				string_DialPrefix = "0",
				ushort_BChannelNum = 2,
				string_UseMsn = "any",
				long_Protocol = 3,
				bool_Qsig = false,
				bool_TerminationEnabled = true,
				bool_Crc4 = false,
				ushort_UseChannels = 2,
				ushort_UseChannelsRX = 2,
				ushort_UseChannelsTX = 2,
				ushort_DialTimeout = 0,
				short_FacilityMode = -1,
				ushort_PPFlags = 0,
				string_Costs = "",
				string_AdditionalPar = "",
				string_CpnPrefixInterNational = "",
				string_CpnPrefixNational = "",
				string_CpnPrefixSubscriber = "",
				bool_PrefixAlsoOnCpn = true,
				bool_PrefixAlsoOnCgn = true,
				bool_PrefixAlsoOnRgn = true,
				string_OutboundPrefixInterNational = "",
				string_OutboundPrefixNational = "",
				string_OutboundPrefixSubscriber = "",
				bool_OutboundPrefixAlsoOnCpn = true,
				bool_OutboundPrefixAlsoOnCgn = true,
				bool_OutboundPrefixAlsoOnRgn = true,
				struct_GateParams = {
					ulong_Mode = 2,
					string_UseConfigFromChannel = "1.a",
					string_SimpleDestination = "",
					string_SimplePilotNumber = "",
					string_SimpleNumMatch = "",
					string_SimpleNumReplace = "",
					string_SimpleRedirExceptions = "",
					bool_SimpleRedirSimulation = false,
					string_InternationalPhone = "",
					string_CountryCode = "49",
					string_SipProtocol = "TCP",
					struct_CallProcTable = {
						[1] = {
							string_Description = "Music",
							string_NumberList = "",
							string_SpecificConfigSet = "",
							bool_InvertNumberList = false,
							string_cngDetection = "off",
							string_cngDetectionTimeout = "15",
							string_action = "stop",
							string_disabled = "no",
							string_Destination = "ISDN",
							string_RegularExpression = "^(.*),(.*),(.*),(.*)",
							string_replacedCalledPartyNumber = "play file /usr/fb/moh.raw",
							string_replacedCallingPartyNumber1 = "\\2",
							string_replacedCallingPartyNumber2 = "\\3",
							string_replacedRedirectedPartyNumber = "\\4",
							string_replacedFaxCSID = "",
							string_sipDefaultGatewayIpAddress = "",
							string_sipDefaultGatewayIpMode = "TCP",
							string_sipDefaultGatewayIpPort = "5060",
							string_AdditionalParameters = "",
							string_faxRouting = "off",
							string_faxDestination = "SIP",
							string_faxRegularExpression = "(.*),(.*),(.*),(.*)",
							string_faxReplacedCalledPartyNumber = "\\1",
							string_faxReplacedCallingPartyNumber1 = "\\2",
							string_faxReplacedCallingPartyNumber2 = "\\3",
							string_faxReplacedRedirectedPartyNumber = "\\4",
							string_faxReplacedFaxCSID = "",
							string_faxSipDefaultGatewayIpAddress = "",
							string_faxSipDefaultGatewayIpMode = "TCP",
							string_faxSipDefaultGatewayIpPort = "5060",
							string_faxAdditionalParameters = ""
						}
					}
				},
				string_IlidSubType = "",
				string_SpecificConfigSet = ""
			},
			[4] = {
				string_IlidSlot = "1",
				string_IlidIndex = "d",
				string_IlidType = "S0",
				string_IlidTitle = "",
				string_IlidDescription = "",
				string_UI_Caption = {
					[1] = "",
					[2] = ""
				},
				bool_CfgValid = true,
				string_ExtLength = "3",
				string_IsdnBase = "",
				string_DialPrefix = "0",
				ushort_BChannelNum = 2,
				string_UseMsn = "any",
				long_Protocol = 3,
				bool_Qsig = false,
				bool_TerminationEnabled = true,
				bool_Crc4 = false,
				ushort_UseChannels = 2,
				ushort_UseChannelsRX = 2,
				ushort_UseChannelsTX = 2,
				ushort_DialTimeout = 0,
				short_FacilityMode = -1,
				ushort_PPFlags = 0,
				string_Costs = "",
				string_AdditionalPar = "",
				string_CpnPrefixInterNational = "",
				string_CpnPrefixNational = "",
				string_CpnPrefixSubscriber = "",
				bool_PrefixAlsoOnCpn = true,
				bool_PrefixAlsoOnCgn = true,
				bool_PrefixAlsoOnRgn = true,
				string_OutboundPrefixInterNational = "",
				string_OutboundPrefixNational = "",
				string_OutboundPrefixSubscriber = "",
				bool_OutboundPrefixAlsoOnCpn = true,
				bool_OutboundPrefixAlsoOnCgn = true,
				bool_OutboundPrefixAlsoOnRgn = true,
				struct_GateParams = {
					ulong_Mode = 2,
					string_UseConfigFromChannel = "1.a",
					string_SimpleDestination = "",
					string_SimplePilotNumber = "",
					string_SimpleNumMatch = "",
					string_SimpleNumReplace = "",
					string_SimpleRedirExceptions = "",
					bool_SimpleRedirSimulation = false,
					string_InternationalPhone = "",
					string_CountryCode = "49",
					string_SipProtocol = "TCP",
					struct_CallProcTable = {
						[1] = {
							string_Description = "Music",
							string_NumberList = "",
							string_SpecificConfigSet = "",
							bool_InvertNumberList = false,
							string_cngDetection = "off",
							string_cngDetectionTimeout = "15",
							string_action = "stop",
							string_disabled = "no",
							string_Destination = "ISDN",
							string_RegularExpression = "^(.*),(.*),(.*),(.*)",
							string_replacedCalledPartyNumber = "play file /usr/fb/moh.raw",
							string_replacedCallingPartyNumber1 = "\\2",
							string_replacedCallingPartyNumber2 = "\\3",
							string_replacedRedirectedPartyNumber = "\\4",
							string_replacedFaxCSID = "",
							string_sipDefaultGatewayIpAddress = "",
							string_sipDefaultGatewayIpMode = "TCP",
							string_sipDefaultGatewayIpPort = "5060",
							string_AdditionalParameters = "",
							string_faxRouting = "off",
							string_faxDestination = "SIP",
							string_faxRegularExpression = "(.*),(.*),(.*),(.*)",
							string_faxReplacedCalledPartyNumber = "\\1",
							string_faxReplacedCallingPartyNumber1 = "\\2",
							string_faxReplacedCallingPartyNumber2 = "\\3",
							string_faxReplacedRedirectedPartyNumber = "\\4",
							string_faxReplacedFaxCSID = "",
							string_faxSipDefaultGatewayIpAddress = "",
							string_faxSipDefaultGatewayIpMode = "TCP",
							string_faxSipDefaultGatewayIpPort = "5060",
							string_faxAdditionalParameters = ""
						}
					}
				},
				string_IlidSubType = "",
				string_SpecificConfigSet = ""
			},
			[5] = {
				string_IlidSlot = "2",
				string_IlidIndex = "a",
				string_IlidType = "FXS",
				string_IlidTitle = "FAX15",
				string_IlidDescription = "",
				string_UI_Caption = {
					[1] = "",
					[2] = ""
				},
				bool_CfgValid = false,
				string_ExtLength = "3",
				string_IsdnBase = "",
				string_DialPrefix = "0",
				ushort_BChannelNum = 1,
				string_UseMsn = "any",
				long_Protocol = 2,
				bool_Qsig = false,
				bool_TerminationEnabled = false,
				bool_Crc4 = false,
				ushort_UseChannels = 1,
				ushort_UseChannelsRX = 1,
				ushort_UseChannelsTX = 1,
				ushort_DialTimeout = 3,
				short_FacilityMode = -1,
				ushort_PPFlags = 0,
				string_Costs = "",
				string_AdditionalPar = "",
				string_CpnPrefixInterNational = "",
				string_CpnPrefixNational = "",
				string_CpnPrefixSubscriber = "",
				bool_PrefixAlsoOnCpn = true,
				bool_PrefixAlsoOnCgn = true,
				bool_PrefixAlsoOnRgn = false,
				string_OutboundPrefixInterNational = "",
				string_OutboundPrefixNational = "",
				string_OutboundPrefixSubscriber = "",
				bool_OutboundPrefixAlsoOnCpn = true,
				bool_OutboundPrefixAlsoOnCgn = true,
				bool_OutboundPrefixAlsoOnRgn = false,
				struct_GateParams = {
					ulong_Mode = 2,
					string_UseConfigFromChannel = "",
					string_SimpleDestination = "",
					string_SimplePilotNumber = "",
					string_SimpleNumMatch = "",
					string_SimpleNumReplace = "",
					string_SimpleRedirExceptions = "",
					bool_SimpleRedirSimulation = false,
					string_InternationalPhone = "",
					string_CountryCode = "49",
					string_SipProtocol = "TCP"
				},
				string_IlidSubType = "",
				string_SpecificConfigSet = ""
			},
			[6] = {
				string_IlidSlot = "2",
				string_IlidIndex = "b",
				string_IlidType = "FXS",
				string_IlidTitle = "",
				string_IlidDescription = "",
				string_UI_Caption = {
					[1] = "",
					[2] = ""
				},
				bool_CfgValid = false,
				string_ExtLength = "3",
				string_IsdnBase = "",
				string_DialPrefix = "0",
				ushort_BChannelNum = 1,
				string_UseMsn = "any",
				long_Protocol = 2,
				bool_Qsig = false,
				bool_TerminationEnabled = false,
				bool_Crc4 = false,
				ushort_UseChannels = 1,
				ushort_UseChannelsRX = 1,
				ushort_UseChannelsTX = 1,
				ushort_DialTimeout = 3,
				short_FacilityMode = -1,
				ushort_PPFlags = 0,
				string_Costs = "",
				string_AdditionalPar = "",
				string_CpnPrefixInterNational = "+",
				string_CpnPrefixNational = "+49",
				string_CpnPrefixSubscriber = "+493574",
				bool_PrefixAlsoOnCpn = true,
				bool_PrefixAlsoOnCgn = true,
				bool_PrefixAlsoOnRgn = false,
				string_OutboundPrefixInterNational = "+",
				string_OutboundPrefixNational = "+49",
				string_OutboundPrefixSubscriber = "",
				bool_OutboundPrefixAlsoOnCpn = true,
				bool_OutboundPrefixAlsoOnCgn = true,
				bool_OutboundPrefixAlsoOnRgn = false,
				struct_GateParams = {
					ulong_Mode = 2,
					string_UseConfigFromChannel = "",
					string_SimpleDestination = "",
					string_SimplePilotNumber = "",
					string_SimpleNumMatch = "",
					string_SimpleNumReplace = "",
					string_SimpleRedirExceptions = "",
					bool_SimpleRedirSimulation = false,
					string_InternationalPhone = "",
					string_CountryCode = "49",
					string_SipProtocol = "TCP"
				},
				string_IlidSubType = "",
				string_SpecificConfigSet = ""
			},
			[7] = {
				string_IlidSlot = "3",
				string_IlidIndex = "a",
				string_IlidType = "S2M",
				string_IlidTitle = "",
				string_IlidDescription = "",
				string_UI_Caption = {
					[1] = "",
					[2] = ""
				},
				bool_CfgValid = false,
				string_ExtLength = "3",
				string_IsdnBase = "",
				string_DialPrefix = "0",
				ushort_BChannelNum = 30,
				string_BChannelMap = "111111111111111111111111111111",
				string_UseMsn = "any",
				long_Protocol = 2,
				bool_Qsig = false,
				bool_TerminationEnabled = false,
				bool_Crc4 = true,
				ushort_UseChannels = 30,
				ushort_UseChannelsRX = 30,
				ushort_UseChannelsTX = 30,
				ushort_DialTimeout = 0,
				short_FacilityMode = -1,
				ushort_PPFlags = 0,
				string_Costs = "",
				string_AdditionalPar = "",
				string_CpnPrefixInterNational = "",
				string_CpnPrefixNational = "",
				string_CpnPrefixSubscriber = "",
				bool_PrefixAlsoOnCpn = false,
				bool_PrefixAlsoOnCgn = true,
				bool_PrefixAlsoOnRgn = false,
				string_OutboundPrefixInterNational = "",
				string_OutboundPrefixNational = "",
				string_OutboundPrefixSubscriber = "",
				bool_OutboundPrefixAlsoOnCpn = false,
				bool_OutboundPrefixAlsoOnCgn = false,
				bool_OutboundPrefixAlsoOnRgn = false,
				struct_GateParams = {
					ulong_Mode = 2,
					string_UseConfigFromChannel = "",
					string_SimpleDestination = "",
					string_SimplePilotNumber = "",
					string_SimpleNumMatch = "",
					string_SimpleNumReplace = "",
					string_SimpleRedirExceptions = "",
					bool_SimpleRedirSimulation = false,
					string_InternationalPhone = "",
					string_CountryCode = "49",
					string_SipProtocol = "TCP"
				},
				string_IlidSubType = "",
				string_SpecificConfigSet = ""
			},
			[8] = {
				string_IlidSlot = "3",
				string_IlidIndex = "b",
				string_IlidType = "S2M",
				string_IlidTitle = "",
				string_IlidDescription = "",
				string_UI_Caption = {
					[1] = "",
					[2] = ""
				},
				bool_CfgValid = false,
				string_ExtLength = "3",
				string_IsdnBase = "",
				string_DialPrefix = "0",
				ushort_BChannelNum = 30,
				string_BChannelMap = "111111111111111111111111111111",
				string_UseMsn = "any",
				long_Protocol = 2,
				bool_Qsig = false,
				bool_TerminationEnabled = false,
				bool_Crc4 = true,
				ushort_UseChannels = 30,
				ushort_UseChannelsRX = 30,
				ushort_UseChannelsTX = 30,
				ushort_DialTimeout = 0,
				short_FacilityMode = -1,
				ushort_PPFlags = 0,
				string_Costs = "",
				string_AdditionalPar = "",
				string_CpnPrefixInterNational = "",
				string_CpnPrefixNational = "",
				string_CpnPrefixSubscriber = "",
				bool_PrefixAlsoOnCpn = false,
				bool_PrefixAlsoOnCgn = true,
				bool_PrefixAlsoOnRgn = false,
				string_OutboundPrefixInterNational = "",
				string_OutboundPrefixNational = "",
				string_OutboundPrefixSubscriber = "",
				bool_OutboundPrefixAlsoOnCpn = false,
				bool_OutboundPrefixAlsoOnCgn = false,
				bool_OutboundPrefixAlsoOnRgn = false,
				struct_GateParams = {
					ulong_Mode = 2,
					string_UseConfigFromChannel = "3.a",
					string_SimpleDestination = "",
					string_SimplePilotNumber = "",
					string_SimpleNumMatch = "",
					string_SimpleNumReplace = "",
					string_SimpleRedirExceptions = "",
					bool_SimpleRedirSimulation = false,
					string_InternationalPhone = "",
					string_CountryCode = "49",
					string_SipProtocol = "TCP"
				},
				string_IlidSubType = "",
				string_SpecificConfigSet = ""
			}
		},
		struct_IsdnSipMap = {
			struct_Isdn2Sip = {
				ushort_Isdn = {
					[1] = 1,
					[2] = 2,
					[3] = 3,
					[4] = 17,
					[5] = 18,
					[6] = 19,
					[7] = 20,
					[8] = 21,
					[9] = 22,
					[10] = 23,
					[11] = 26,
					[12] = 27,
					[13] = 28,
					[14] = 29,
					[15] = 31,
					[16] = 34,
					[17] = 38,
					[18] = 41,
					[19] = 42,
					[20] = 47,
					[21] = 55,
					[22] = 57,
					[23] = 58,
					[24] = 65,
					[25] = 70,
					[26] = 79,
					[27] = 87,
					[28] = 88,
					[29] = 102,
					[30] = 111,
					[31] = 127
				},
				ushort_Sip = {
					[1] = 404,
					[2] = 404,
					[3] = 404,
					[4] = 486,
					[5] = 408,
					[6] = 480,
					[7] = 480,
					[8] = 403,
					[9] = 410,
					[10] = 410,
					[11] = 404,
					[12] = 502,
					[13] = 484,
					[14] = 501,
					[15] = 480,
					[16] = 503,
					[17] = 503,
					[18] = 503,
					[19] = 503,
					[20] = 503,
					[21] = 403,
					[22] = 403,
					[23] = 503,
					[24] = 488,
					[25] = 488,
					[26] = 501,
					[27] = 403,
					[28] = 400,
					[29] = 504,
					[30] = 500,
					[31] = 500
				}
			},
			struct_Sip2Isdn = {
				ushort_Sip = {
					[1] = 400,
					[2] = 401,
					[3] = 402,
					[4] = 403,
					[5] = 404,
					[6] = 405,
					[7] = 406,
					[8] = 407,
					[9] = 408,
					[10] = 410,
					[11] = 413,
					[12] = 414,
					[13] = 415,
					[14] = 416,
					[15] = 420,
					[16] = 421,
					[17] = 423,
					[18] = 480,
					[19] = 481,
					[20] = 482,
					[21] = 483,
					[22] = 484,
					[23] = 485,
					[24] = 486,
					[25] = 487,
					[26] = 488,
					[27] = 500,
					[28] = 501,
					[29] = 502,
					[30] = 503,
					[31] = 504,
					[32] = 505,
					[33] = 513,
					[34] = 600,
					[35] = 603,
					[36] = 604,
					[37] = 606
				},
				ushort_Isdn = {
					[1] = 41,
					[2] = 21,
					[3] = 21,
					[4] = 21,
					[5] = 1,
					[6] = 63,
					[7] = 79,
					[8] = 21,
					[9] = 102,
					[10] = 22,
					[11] = 127,
					[12] = 127,
					[13] = 79,
					[14] = 127,
					[15] = 127,
					[16] = 127,
					[17] = 127,
					[18] = 18,
					[19] = 41,
					[20] = 25,
					[21] = 25,
					[22] = 28,
					[23] = 1,
					[24] = 17,
					[25] = 127,
					[26] = 65,
					[27] = 41,
					[28] = 79,
					[29] = 38,
					[30] = 41,
					[31] = 102,
					[32] = 127,
					[33] = 127,
					[34] = 17,
					[35] = 21,
					[36] = 1,
					[37] = 65
				}
			}
		},
		string_RegisterMode = "open",
		string_RegisterPassword = "",
		string_RouteMode = "1",
		string_dns_dbg = "2",
		string_callp_dbg = "2",
		string_PublicFqdn = "",
		string_PublicIp = "",
		bool_T38PassThrough = false,
		string_IsdnClockMasterPriority = {
			[1] = "Bri",
			[2] = "Pri",
			[3] = "Intern"
		}
	}
}
EOF
    return $?
}

RET=0

VERSION_BUILD=$(omgversion | grep "omg2\?\.version" | sed "s/.*=\([0-9]\+\.[0-9]\+-[0-9]\+\).*/\1/")
VERSION=${VERSION_BUILD%-*}
BUILD=${VERSION_BUILD#*-}

vercomp "${VERSION}" 4.1
VERCOMP_RETVAL=$?
if [ ${VERCOMP_RETVAL} -eq 2 -o $((BUILD < 430)) -eq 1 ]; then
    echo "Minimum required firmware version is 4.1-430. Please update the firmware before testing the S0 ports."
    RET=1
elif ! omgstop; then
    echo "Could not stop services"
    RET=1
elif ! cp /data/config.lua /data/config.lua.s0test.orig; then
    echo "Could not create backup file of config.lua"
    RET=1
else
    if ! writeTestConfigLua; then
        echo "Could not install test config.lua"
        RET=1
    elif ! sed -i "s/BOWL=1/BOWL=0/" /tmp/cmdline; then
        echo "Could not disable licensing"
        RET=1
    else
        cd /usr/fb/bin/sipisdn
        . /tmp/cmdline
        ./callp &
        CALLP_PID=$!

        sleep 3

        if ! pgrep callp >/dev/null; then
            echo "Could not start callp"
            RET=1
        else
            echo "Make test calls to numbers 20, 30, 40 and 50. You must hear some music in each call."
            echo "Press Enter, when done:"
            read dummy

            echo "Stopping callp..."
            kill -1 ${CALLP_PID}
            i=0
            while true; do
                if [ ${i} -ge 10 ]; then
                    kill -9 ${CALLP_PID} >/dev/null 2>/dev/null
                    break
                elif ! pgrep callp >/dev/null; then
                    break
                fi
                sleep 1
                i=$((i + 1))
            done
        fi
    fi

    if ! mv -f /data/config.lua.s0test.orig /data/config.lua; then
        echo "Could not restore config.lua"
        RET=1
    fi
fi
