#!/bin/sh /etc/rc.common

. /lib/functions/chargingpoint.sh

START=30
USE_PROCD=1

enabled_visa() {
	sn=`hostname`
	case "${sn:0:2}" in
        6H|6M|6J|6K)
        factory_code=`uci get ingeteam.@ingeteam[0].factory_code`
	[ "${sn:2:1}" == "V" ] && {
            [ "${factory_code:16:1}" == "1" ] || [ "${factory_code:16:1}" == "X" ] && {
                logger "Rapid 50 - Ingenico"
                uci set visad.internal.terminal_manufacturer='Ingenico'
                uci commit visad
            }
            [ "${factory_code:16:1}" == "S" ] || [ "${factory_code:16:1}" == "W" ] || [ "${factory_code:16:1}" == "R" ] && {
                logger "Rapid 50 - Payter"
                uci set visad.internal.terminal_manufacturer='Payter'
                uci commit visad
            }

            return 1
        }
	;;
        6L)
        factory_code=`uci get ingeteam.@ingeteam[0].factory_code`
        [ "${factory_code:17:1}" == "Y" ] && {
            return 1
        }
        ;;
	6Z)
        factory_code=`uci get ingeteam.@ingeteam[0].factory_code`
        [ "${factory_code:19:1}" == "1" ] || [ "${factory_code:19:1}" == "Y" ] || [ "${factory_code:19:1}" == "X" ] && {
            logger "Rapid 180 - Ingenico"
            uci set visad.internal.terminal_manufacturer='Ingenico'
            uci commit visad
            return 1
	}
        [ "${factory_code:19:1}" == "P" ] || [ "${factory_code:19:1}" == "Q" ] || [ "${factory_code:19:1}" == "R" ] || [ "${factory_code:19:1}" == "S" ] || [ "${factory_code:19:1}" == "V" ] || [ "${factory_code:19:1}" == "W" ] && {
            logger "Rapid 180 - Payter"
            uci set visad.internal.terminal_manufacturer='Payter'
            uci commit visad
            return 1
        }
	;;
        6Y)
        factory_code=`uci get ingeteam.@ingeteam[0].factory_code`
        [ "${factory_code:19:1}" == "Y" ] || [ "${factory_code:19:1}" == "X" ] && {
            logger "Rapid 60 - Ingenico"
            uci set visad.internal.terminal_manufacturer='Ingenico'
            uci commit visad
            return 1
        }
        [ "${factory_code:19:1}" == "S" ] || [ "${factory_code:19:1}" == "W" ] || [ "${factory_code:19:1}" == "R" ] || [ "${factory_code:19:1}" == "Q" ] || [ "${factory_code:19:1}" == "V" ] || [ "${factory_code:19:1}" == "P" ] && {
            logger "Rapid 60 - Payter"
            uci set visad.internal.terminal_manufacturer='Payter'
            uci commit visad
            return 1
        }
        ;;
        6S|6W)
        factory_code=`uci get ingeteam.@ingeteam[0].factory_code`
        [ "${factory_code:18:1}" == "1" ] || [ "${factory_code:18:1}" == "X" ] && {
            logger "Fusion - Ingenico"
            uci set visad.internal.terminal_manufacturer='Ingenico'
            uci commit visad
            return 1
        }
        [ "${factory_code:18:1}" == "S" ] || [ "${factory_code:18:1}" == "W" ] || [ "${factory_code:18:1}" == "R" ] || [ "${factory_code:18:1}" == "Q" ] || [ "${factory_code:18:1}" == "V" ] || [ "${factory_code:18:1}" == "P" ] && {
            logger "Fusion - Payter"
            uci set visad.internal.terminal_manufacturer='Payter'
            uci commit visad
            return 1
        }
        ;;
        esac

	return 0
}

wan3g_enabled() {
        # 3g wan up
        wan_enabled=0
        result=$(ubus call network.interface.wan status | jsonfilter -e '@.up')
        [ "$result" == "true" ] && {
                return 1
        }
        # check 3g modem+sim -> iccid
        #iccid=$(uci get simd.@simd[0].iccid)
        #[ "$iccid" != "" ] && {
        #        return 1
        #}
        return 0
}

#fusion
check_usbnet_kos() {
    inserted=$(lsmod|grep usbnet)
    [ -z "$inserted" ] && {
    modprobe usbnet
    }
    lsusb | grep "0bda:8153"
    [ "$?" == "0" ] && {
            inserted=$(lsmod|grep r8152)
            [ -z "$inserted" ] && {
            modprobe r8152
            }
    }
    inserted=$(lsmod|grep cdc_ether)
    [ -z "$inserted" ] && {
    modprobe cdc_ether
    }
}

check_usbeth1_settings() {
	eth1_exists=$(uci get network.usblan)
	[ -z "$eth1_exists" ] && {
	uci set network.usblan=interface
	uci set network.usblan.ifname='eth1'
	uci set network.usblan.proto='static'
	uci set network.usblan.ipaddr='192.168.3.1'
	uci set network.usblan.netmask='255.255.255.0'
	uci set network.usblan.ipv6='0'
	uci commit network
	/etc/init.d/network/reload
	}
}

visa_settings() {
	#uci settings
	sn=`hostname`
	case "${sn:0:2}" in
        6H|6M|6J|6K|6L|6Z)
        localip=$(uci get network.glan.ipaddr)
        logger "visad localip $localip"
        uci set visad.internal.localip=$localip
        uci set visad.internal.local_interface=eth0
        uci set visad.internal.wan_interface=eth1
        case "${localip:0:16}" in
        192.168.1.33)
        uci set visad.payter.server_ip=192.168.1.34
        ;;
        192.168.252.33)
        uci set visad.payter.server_ip=192.168.252.34
        ;;
        esac
	;;
        6S|6W)
        localip=$(uci get network.usblan.ipaddr)
        uci set visad.internal.localip=192.168.3.1
        uci set visad.payter.server_ip=192.168.3.2
        uci set visad.ingenico.interface=$localip
        check_usbnet_kos
        check_usbeth1_settings
        uci set visad.internal.local_interface=eth1
        uci set visad.internal.wan_interface=eth0
        ;;
        6Y)
        localip=$(uci get network.laninternal.ipaddr)
        logger "visad localip $localip"
        uci set visad.ingenico.interface=$localip
        case "${localip:0:16}" in
        192.168.1.33)
        uci set visad.payter.server_ip=192.168.1.34
        ;;
        192.168.252.33)
        uci set visad.payter.server_ip=192.168.252.34
        ;;
        esac
	uci set visad.internal.localip=192.168.255.33
	uci set visad.internal.local_interface=eth1
	uci set visad.internal.wan_interface=eth0
        ;;
	esac

	wan3g_enabled
	[ "$?" == "1" ] && {
		uci set visad.internal.wan_interface=wwan-wan
	}
	uci commit visad
}

visa_iptables() {
	#forwarding
	echo 1 > /proc/sys/net/ipv4/ip_forward
	#internal lan
	local_interface=$(uci get visad.internal.local_interface)
	iptables -A FORWARD -i $local_interface -j ACCEPT
	iptables -A FORWARD -o $local_interface -j ACCEPT
	logger "visa, local interface $local_interface"
	#ipv4 masquerade
	wan_interface=$(uci get visad.internal.wan_interface)
	iptables -t nat -A POSTROUTING -o $wan_interface -j MASQUERADE
	logger "visa, nat $wan_interface interface"
	#dont duplicate
	/usr/sbin/iptables-save | awk '!COMMIT||!x[$0]++' | /usr/sbin/iptables-restore
	#iptables-save > /tmp/iptables.conf
	#uniq /tmp/iptables.conf > /tmp/iptables_new.conf
	#iptables-restore < /tmp/iptables_new.conf	
}


reload_service() {
	config_load visad
}

service_triggers() {
	enabled_visa
	[ "$?" == "1" ] && {
	procd_add_reload_trigger "visad"
	}
}

start_service() {
	enabled_visa
	[ "$?" == "1" ] && {
	#settings
	visa_settings
	#forwarding
	visa_iptables
	#instance
	procd_open_instance
	procd_set_param command /usr/sbin/visad

	#respawn automatically if something died, be careful
	procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-2} ${respawn_retry:-0}
	#reload
	procd_set_param file /etc/config/visad
	#log printfs
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param user root

	procd_close_instance
	}
}

stop_service() {
supervisor_unsubscriber visad
}

