source scripts/usart-config.sh
source scripts/rfm12-config.sh
source scripts/namedPinCopy.sh
mainmenu_name "Ethersex Configuration"
get_usart_count

mainmenu_option next_comment
comment "General Setup"
	get_usart_count

	choice 'Target Architecture'			\
		"AVR		ARCH_AVR		\
		 Host		ARCH_HOST"		\
		'AVR' ARCH

if [ "$ARCH_AVR" = "y" ]; then
	choice 'Target MCU'			\
		"ATmega8      atmega8		\
		 ATmega88     atmega88		\
		 ATmega128    atmega128		\
		 ATmega16     atmega16		\
		 ATmega162    atmega162		\
		 ATmega168    atmega168		\
		 ATmega168p   atmega168p	\
		 ATmega169    atmega169		\
		 ATmega32     atmega32		\
		 ATmega328p   atmega328p	\
		 ATmega64     atmega64		\
		 ATmega644    atmega644		\
		 ATmega644p   atmega644p	\
		 ATmega1284p  atmega1284p	\
		 AT90CAN32    at90can32		\
		 AT90CAN64    at90can64		\
		 AT90CAN128   at90can128"	\
		'ATmega644' MCU

	int "MCU frequency" FREQ 20000000

	choice 'Hardware/Periphery Class'	\
			"$(scripts/pinning_hardware_list.sh)" \
		'Etherrape' HARDWARE

	dep_bool "Use CPU sleep" CPU_SLEEP $ARCH_AVR
	source hardware/sram/config.in


	# get the flash size according to the cpu type
	FLASH_SIZE=$(( `echo -e "#include <avr/io.h>\nFLASHEND" | avr-cpp -mmcu=$MCU | sed '$!d'` + 1 ))

	# set the default bootloader bytes according to cpu flash size
	if [ "$BOOTLOADER_SIZE" = "" ] ; then
		if [ "$FLASH_SIZE" = 8192 ] ; then BOOTLOADER_SIZE=2048 ; fi
		if [ "$FLASH_SIZE" = 16384 ] ; then BOOTLOADER_SIZE=2048 ; fi
		if [ "$FLASH_SIZE" = 32768 ] ; then BOOTLOADER_SIZE=4096 ; fi
		if [ "$FLASH_SIZE" = 65536 ] ; then BOOTLOADER_SIZE=8192 ; fi
		if [ "$FLASH_SIZE" = 131072 ] ; then BOOTLOADER_SIZE=8192 ; fi
	fi

	if [ "$FLASH_SIZE" -gt 65535 ] ; then
		bool 'Build a bootloader' BOOTLOADER_SUPPORT
	else
		dep_bool 'Build a bootloader' BOOTLOADER_SUPPORT "n"
	fi


	bool "Enable bootloader jump" BOOTLOADER_JUMP
	if [ "$BOOTLOADER_JUMP" = "y" -o "$BOOTLOADER_SUPPORT" = "y" ] ; then
		int 'Bootloader Size in bytes' BOOTLOADER_SIZE ""
	else
		define_int BOOTLOADER_SIZE "$((BOOTLOADER_SIZE))"
	fi

	define_hex BOOTLOADER_START_ADDRESS "$(printf '0x%X\n' $(($FLASH_SIZE - $BOOTLOADER_SIZE)))"

	define_hex FLASH_SIZE "$(printf '0x%X\n' $FLASH_SIZE)"
	define_hex EEPROM_SIZE "$(printf '0x%X\n' $((`echo -e "#include <avr/io.h>\nE2END" | avr-cpp -mmcu=$MCU | sed '$!d'` + 1)))"
	define_hex RAM_SIZE "$(printf '0x%X\n' $((`echo -e "#include <avr/io.h>\nRAMEND" | avr-cpp -mmcu=$MCU | sed '$!d'` + 1)))"
	define_hex FLASH_PAGESIZE "$(printf '0x%X\n' $((`echo -e "#include <avr/io.h>\nSPM_PAGESIZE" | avr-cpp -mmcu=$MCU | sed '$!d'`)))"
	define_hex EEPROM_PAGESIZE "$(printf '0x%X\n' $((`echo -e "#include <avr/io.h>\nE2PAGESIZE" | avr-cpp -mmcu=$MCU | sed '$!d'`)))"

	source core/crc/config.in
fi

	bool "MBR support" MBR_SUPPORT

	bool "Teensy build" TEENSY_SUPPORT

	dep_bool "Use SPI Timeout" SPI_TIMEOUT $ARCH_AVR
	dep_bool "Use Usart in SPI Mode" USART_SPI_SUPPORT $ARCH_AVR
    if [ "$USART_SPI_SUPPORT" = y ]; then
      choice '  SPI usart select' "$(usart_choice SPI)"
      usart_process_choice SPI
    else
      dep_bool "Use Software-SPI" SOFT_SPI_SUPPORT $ARCH_AVR
    fi

	choice 'Version String'			                    \
		"GIT-commit-hash       USE_GIT_VERSION	        \
		Release-Version       USE_RELEASE_VERSION	    \
		Build-Date-Time       USE_BUILDDATE_VERSION"	\
		'Release-Version' VERSION_STRING_CHOICE

        if [ "$VERSION_STRING_CHOICE" = "USE_GIT_VERSION" ] || \
            [ "$VERSION_STRING_CHOICE" = "USE_RELEASE_VERSION" ] ; then
            define_string VERSION_GIT "$(scripts/get-git-version)"
        fi

	bool 'Debug: Discard some packets' DEBUG_DISCARD_SOME
	dep_bool_menu "Enable Debugging" DEBUG y
		int "UART Baudrate" DEBUG_BAUDRATE 115200
		dep_bool 'Use SYSLOG instead UART' DEBUG_USE_SYSLOG $SYSLOG_SUPPORT $DEBUG
		dep_bool 'Software Uart (Output only)' SOFT_UART_SUPPORT $DEBUG $ARCH_AVR
		comment  '----- Debugging Flags -----'
		dep_bool 'Hooks' DEBUG_HOOK $DEBUG
		dep_bool 'Reset Reason' DEBUG_RESET_REASON $DEBUG
	endmenu

	source core/config.in
	source core/crypto/config.in
	source core/vfs/config.in
	source services/pam/config.in
	source control6/config.in
endmenu

mainmenu_option next_comment
	comment "Network"

	string "Hostname" CONF_HOSTNAME "ethersex"


#	if [ "$ENC28J60_SUPPORT" = "y" -o "$RFM12_IP_SUPPORT" = "y" -o "$ZBUS_SUPPORT" = "y" -o "$UIP_SUPPORT" = "y" -o "$UDP_SUPPORT" = "y" -o "$TCP_SUPPORT" = "y" -o "$ICMP_SUPPORT" = "y" ]; then
		if [ "$NET_MAX_FRAME_LENGTH" = "" ] ; then NET_MAX_FRAME_LENGTH=1500 ; fi
		if [ "$NET_MAX_FRAME_LENGTH" -lt 64 -o "$NET_MAX_FRAME_LENGTH" -gt 1500 ] ; then NET_MAX_FRAME_LENGTH=1500 ; fi

		if [ "$atmega8" = "y" -o "$atmega88" = "y" -o "$atmega16" = "y" -o "$atmega168" = "y" -o "$atmega169" = "y" -o "$atmega168p" = "y" ] ; then
			if [ "$NET_MAX_FRAME_LENGTH" -gt 192 ] ; then NET_MAX_FRAME_LENGTH=192 ; fi
		fi
		if [ "$atmega32" = "y" ] ; then
			if [ "$TEENSY_SUPPORT" = "y" ] ; then
				if [ "$NET_MAX_FRAME_LENGTH" -gt 192 ] ; then NET_MAX_FRAME_LENGTH=192 ; fi
			else
				if [ "$NET_MAX_FRAME_LENGTH" -gt 500 ] ; then NET_MAX_FRAME_LENGTH=500 ; fi
			fi
		fi
		if [ "$atmega162" = "y" -a "$NET_MAX_FRAME_LENGTH" -gt 600 ] ; then NET_MAX_FRAME_LENGTH=600 ; fi
		if [ "$atmega328p" = "y" -a "$NET_MAX_FRAME_LENGTH" -gt 384 ] ; then NET_MAX_FRAME_LENGTH=384 ; fi

		Value=$NET_MAX_FRAME_LENGTH

		int "Network Buffer Size" NET_MAX_FRAME_LENGTH $NET_MAX_FRAME_LENGTH
#	fi

	source hardware/ethernet/config.in
	source core/host/tap-config.in
	source hardware/radio/rfm12/config.in
	dep_bool 'RFM12 raw' RFM12_RAW_SUPPORT $RFM12_IP_SUPPORT $ROUTER_SUPPORT $UDP_SUPPORT
	if [ "$RFM12_RAW_SUPPORT" = y ]; then
		int 'RFM12 raw UDP Port' RFM12_RAW_UDP_PORT 16962
	fi
	source protocols/usb/config.in
	source protocols/zbus/config.in
	dep_bool 'ZBUS raw' ZBUS_RAW_SUPPORT $ZBUS_SUPPORT $ROUTER_SUPPORT $UDP_SUPPORT
	source scripts/ip-router-conf.sh

	comment "IP configuration"
	dep_bool 'IPv6 support'		IPV6_SUPPORT $UIP_SUPPORT
	if [ "$IPV6_SUPPORT" = "y" ]; then
		define_bool IPV4_SUPPORT n
	else
		define_bool IPV4_SUPPORT $UIP_SUPPORT
	fi

	dep_bool 'Enable IP forwarding' IP_FORWARDING_SUPPORT $ROUTER_SUPPORT

	dep_bool 'Enable TCP inactivity timeout' UIP_TIMEOUT_SUPPORT $UIP_SUPPORT
	if [ "$UIP_TIMEOUT_SUPPORT" = "y" ]; then
		int "  TCP inactivity timeout (in minutes)" UIP_TIMEOUT_COUNT 10
	fi

        if [ "$IPV6_SUPPORT" != "y" -a "$BOOTP_SUPPORT" != "y" -o "$IPV6_STATIC_SUPPORT" = "y" ]; then
		ip "Default gateway" CONF_ETHERSEX_GATEWAY "192.168.23.1" "2001:6f8:1209:f0:0:0:0:1"
	fi

	define_bool UIP_MULTI_STACK $ROUTER_SUPPORT
	source protocols/uip/ipchair/config.in
	source protocols/uip/config.in
	source protocols/dns/config.in

	if [ "$DHCP_SUPPORT" = "y" ]; then
		dep_bool 'BOOTP (DHCP-like) support' BOOTP_SUPPORT "n"
	else
		dep_bool 'BOOTP (DHCP-like) support' BOOTP_SUPPORT $BROADCAST_SUPPORT $ENC28J60_SUPPORT $IPV4_SUPPORT
	fi
	dep_bool 'Write BOOTP data to EEPROM' BOOTP_TO_EEPROM_SUPPORT $BOOTLOADER_SUPPORT $BOOTP_SUPPORT

	if [ "$BOOTP_SUPPORT" = "y" ]; then
		dep_bool "DHCP support" DHCP_SUPPORT "n"
	else
		dep_bool "DHCP support" DHCP_SUPPORT $BROADCAST_SUPPORT $ENC28J60_SUPPORT $IPV4_SUPPORT
	fi

	comment "Tunneling protocols"
	dep_bool_menu "OpenVPN" OPENVPN_SUPPORT $UDP_SUPPORT
		hex "OpenVPN key" CONF_OPENVPN_KEY "00000000000000000000000000000000"
		hex "OpenVPN HMAC key" CONF_OPENVPN_HMAC_KEY "00000000000000000000000000000000"

		comment "OpenVPN IP configuration"
		ip "OpenVPN IP address" CONF_OPENVPN_IP "192.168.23.244" "2001:6f8:1209:F0:0:0:0:1"
		if [ "$IPV6_SUPPORT" = "y" ]; then
			int "IP prefix length" CONF_OPENVPN_IP6_PREFIX_LEN 64
		else
			ipv4 "OpenVPN netmask" CONF_OPENVPN_IP4_NETMASK "255.255.255.0"
		fi

		comment "OpenVPN Remote"
		dep_bool "Static OpenVPN Remote" OPENVPN_STATIC_REMOTE $OPENVPN_SUPPORT
		if [ "$OPENVPN_STATIC_REMOTE" = "y" ]; then
			ip "Remote IP" CONF_OPENVPN_REMOTE_IP "192.168.23.254" "2001:6f8:1209:f0:230:5ff:fe23:3f8"
		fi
		comment "OpenVPN Port"
		int 'OpenVPN Port' CONF_OPENVPN_PORT 1194
	endmenu

	comment  "Debugging Flags"
	dep_bool 'Networking' DEBUG_NET $DEBUG
	dep_bool '  Config' DEBUG_NET_CONFIG $DEBUG_NET
	dep_bool '  IPv6' DEBUG_NET_IP6 $DEBUG
	dep_bool '  OpenVPN' DEBUG_OPENVPN $DEBUG
	dep_bool '  Router' DEBUG_ROUTER $DEBUG
	dep_bool '  uIP' DEBUG_UIP $DEBUG
	dep_bool '  NTP' DEBUG_NTP $DEBUG
	dep_bool '  Unknown packets' DEBUG_UNKNOWN_PACKETS $DEBUG_NET

endmenu

###############################################################################

mainmenu_option next_comment
comment "I/O"

	if [ "$ARCH_HOST" = "y" ]; then
		define_bool PORTIO_SUPPORT n
		define_bool PORTIO_SIMPLE_SUPPORT n
		define_bool NAMED_PIN_SUPPORT n
	else
		choice "I/O abstraction model (Port I/O)" \
			"None			CONFIG_IO_NONE		\
			 Simple			PORTIO_SIMPLE		\
			 Full-featured		PORTIO_FULL_FEATURED"	\
			Simple PORTIO_SCHEME

		if [ x"$PORTIO_SCHEME" = x"PORTIO_SIMPLE" ]; then
			define_bool PORTIO_SIMPLE_SUPPORT y;
		elif [ x"$PORTIO_SCHEME" = x"PORTIO_FULL_FEATURED" ]; then
			define_bool PORTIO_SUPPORT y;
		fi

		source core/portio/config.in
	fi

	source core/tty/config.in
	source hardware/config.in
	source mcuf/config.in
endmenu

###############################################################################

mainmenu_option next_comment
comment "Protocols"

source protocols/artnet/config.in
source protocols/dali/config.in
source protocols/dmx/config.in
source protocols/ecmd/config.in
source protocols/eltakoms/config.in
source protocols/ems/config.in
source protocols/fnordlicht/config.in
source protocols/httplog/config.in
source protocols/irc/config.in
source protocols/mdns_sd/config.in
source protocols/modbus/config.in
source protocols/mysql/config.in
source protocols/netstat/config.in
source protocols/soap/config.in
source protocols/snmp/config.in
source protocols/smtp/config.in
source protocols/sms77/config.in
source protocols/syslog/config.in
source protocols/twitter/config.in
source protocols/ustream/config.in
source protocols/yport/config.in
source protocols/msr1/config.in
source protocols/to1/config.in
source protocols/serial_line_log/config.in
source protocols/nmea/config.in
source protocols/udpstella/config.in
source protocols/udpIO/config.in
source protocols/udpcurtain/config.in
source protocols/cw/config.in
endmenu

###############################################################################


mainmenu_option next_comment
comment "Applications"

source services/appsample/config.in
source services/bulbdial/config.in
source services/clock/config.in
source services/cron/config.in
source services/dyndns/config.in
source services/dmx-storage/config.in
source services/dmx-fxslot/config.in
source services/echo/config.in
source services/wol/config.in
source services/motd/config.in
source services/httpd/config.in
source services/jabber/config.in
source services/moodlight/config.in
source services/stella/config.in
source services/starburst/config.in
source services/curtain/config.in
source services/tftp/config.in
source services/upnp/config.in
source services/watchcat/config.in
source services/vnc/config.in
source services/watchasync/config.in
source services/glcdmenu/config.in
source services/lome6/config.in
source services/projectors/sanyoZ700/config.in
source services/freqcount/config.in
source services/tanklevel/config.in
endmenu

###############################################################################

if [ "$ZBUS_SUPPORT" = "y"		\
	-o "$YPORT_SUPPORT" = "y"	\
	-o "$MODBUS_SUPPORT" = "y" ]; then
  define_bool USART_SUPPORT y
fi

if [ "$RFM12_IP_SUPPORT" = "y" -o "$RFM12_ASK_SUPPORT" = "y" -o "$RFM12_DISABLE" = "y" ]; then
  define_bool RFM12_SUPPORT y
else
  define_bool RFM12_SUPPORT n
fi

if [ "$UIP_SUPPORT" = "y" -a "$IPV6_SUPPORT" != "y" ]; then
  define_bool IPV4_SUPPORT y
fi
