#!/bin/sh
set -e
. ./passwords

echo 'Updating root password'
passwd <<EOF
$NEWPASSWD
$NEWPASSWD
EOF

# === Set the Time Zone ========================
# Set the time zone to non-default (other than UTC)
# Full list of time zones is at:
#    http://wiki.openwrt.org/doc/uci/system#time.zones
# Use the URL above to find the desired ZONENAME and TIMEZONE,
# then uncomment six lines
#
TIMEZONE='EET-2EEST,M3.5.0/3,M10.5.0/4'
ZONENAME='Europe/Bucharest'
echo 'Setting timezone to' $TIMEZONE
uci set system.@system[0].timezone="$TIMEZONE"
echo 'Setting zone name to' $ZONENAME
uci set system.@system[0].zonename="$ZONENAME"
uci commit system


# === Update SSID ==============================
# SSID:
# Supply a value for NEWSSID and uncomment three lines
#
NEWSSID2=thunder-300-2
NEWSSID5=thunder-300-5
echo 'Changing SSID to' $NEWSSID2 $NEWSSID5
sed -i s#CEROwrt5#$NEWSSID5#g /etc/config/wireless
sed -i s#CEROwrt\'#$NEWSSID2\'#g /etc/config/wireless

# === Update WiFi security info ================
# Update the wifi password/security. To see all the wireless info:
#    uci show wireless
# Interface indexes are:
#    0 - CEROwrt
#    1 - CEROwrt-guest
#    3 - CEROwrt5
#    4 - CEROwrt-guest5
# The full list of encryption modes is at: 
#    http://wiki.openwrt.org/doc/uci/wireless#wpa.modes
# Set WIFIPASSWD and the ENCRMODE (psk2 gives WPA2-PSK), 
# and then uncomment 12 lines.
#
# echo 'Updating WiFi security information'
ENCRMODE=psk2
uci set wireless.@wifi-iface[0].key=$WIFIPASSWD
#uci set wireless.@wifi-iface[1].key=$WIFIPASSWD
uci set wireless.@wifi-iface[1].disabled=1
uci set wireless.@wifi-iface[3].key=$WIFIPASSWD
uci set wireless.@wifi-iface[4].disabled=1
uci set wireless.@wifi-iface[0].encryption=$ENCRMODE
uci set wireless.@wifi-iface[3].encryption=$ENCRMODE

uci set wireless.radio0.country=RO
uci set wireless.radio0.channel=11
uci set wireless.radio1.country=RO
uci set wireless.radio1.channel=44
uci set wireless.radio1.htmode=HT40+
uci commit wireless

# Configure SSH
echo "$SSHPUBKEY" >/etc/dropbear/authorized_keys
uci set dropbear.@dropbear[0].Interface=se00
uci set dropbear.@dropbear[0].PasswordAuth=off
uci set dropbear.@dropbear[0].RootPasswordAuth=off
uci commit dropbear
sed -i -e 's/server_args.*/server_args = -i -s/' /etc/xinetd.d/ssh

# Update Internet connection
uci set network.ge00.proto=pppoe
uci set network.ge00.username=$PPPOEUSER
uci set network.ge00.password=$PPPOEPASS
uci set network.ge00._orig_ifname='ge00'
uci set network.ge00._orig_bridge='false'
uci set network.ge00.ipv6=1
uci set network.wan6.reqprefix=auto
uci del network.sw00.ip6assign || true
uci del network.sw10.ip6assign || true
uci del network.gw00.ip6assign || true
uci del network.gw10.ip6assign || true
uci commit network

uci set bcp38.@bcp38[0].enabled=1
uci set bcp38.@bcp38[0].interface=pppoe-ge00
uci commit bcp38

ifdown ge00
ifup ge00
echo "Plug in ISP cable"
logread -f| grep pppd&
read
/etc/init.d/network reload
/etc/init.d/firewall reload
ip route add default dev pppoe-ge00
# === Update the software packages =============
# Download and update all the interesting packages
# Some of these are pre-installed, but there is no harm in
# updating/installing them a second time.
opkg update               # retrieve updated packages
opkg install p910nd luci-app-p910nd kmod-usb-printer # install printer packages
opkg install wifitoggle


DOWNLOADSPEED=46000
UPLOADSPEED=50000
echo 'Setting QoS to' $DOWNLOADSPEED/$UPLOADSPEED 'kbps down/up'
uci set sqm.ge00.enabled=1
uci set sqm.ge00.download=$DOWNLOADSPEED
uci set sqm.ge00.upload=$UPLOADSPEED
uci set sqm.ge00.interface=pppoe-ge00
uci commit sqm
uci set upnpd.config.download=$DOWNLOADSPEED
uci set upnpd.config.upload=$UPLOADSPEED
uci commit upnpd

# Configure LEDs
uci set system.@led[0].default=0
uci set 'system.@led[0].name=WPS LED'
uci set system.@led[0].sysfs=netgear:orange:wps
uci set system.@led[0].trigger=none
uci set system.@led[0]=led
uci set system.@led[1].default=1
uci set system.@led[1].delayoff=500
uci set system.@led[1].delayon=500
uci set system.@led[1].mode=link
uci set system.@led[1].sysfs=wndr3700:orange:wps
uci set system.@led[1].trigger=timer
uci set system.@led[1]=led
uci set system.led_usb.default=0
uci set system.led_usb.dev=1-1
uci set system.led_usb.interval=50
uci set system.led_usb.name=USB
uci set system.led_usb.sysfs=netgear:green:usb
uci set system.led_usb.trigger=usbdev
uci set system.led_usb=led
uci set system.led_wan.default=0
uci set 'system.led_wan.name=WAN LED (green)'
uci set system.led_wan.sysfs=netgear:green:wan
uci set system.led_wan.trigger=none
uci set system.led_wan=led
uci commit system

# Configure NTP
uci del system.ntp.server || true
uci add_list system.ntp.server='ntp2.usv.ro'
uci add_list system.ntp.server='ntp3.usv.ro'
uci add_list system.ntp.server='0.openwrt.pool.ntp.org'
uci add_list system.ntp.server='1.openwrt.pool.ntp.org'
uci commit system

# wifitoggle
uci set wifitoggle.@wifitoggle[0]=wifitoggle
uci set wifitoggle.@wifitoggle[0].button=BTN_2
uci set wifitoggle.@wifitoggle[0].led_disable_default=0
uci set wifitoggle.@wifitoggle[0].led_enable_delayoff=500
uci set wifitoggle.@wifitoggle[0].led_enable_delayon=500
uci set wifitoggle.@wifitoggle[0].led_enable_trigger=timer
uci set wifitoggle.@wifitoggle[0].led_sysfs=wndr3700:orange:wps
uci set wifitoggle.@wifitoggle[0].persistent=1
uci set wifitoggle.@wifitoggle[0].timer=0
uci commit wifitoggle

# Configure firewall
## SSH port redir
uci set firewall.@redirect[0]=redirect
uci set firewall.@redirect[0].dest=lan
uci set firewall.@redirect[0].dest_ip=172.30.42.12
uci set firewall.@redirect[0].dest_port=22
uci set firewall.@redirect[0].enabled=1
uci set firewall.@redirect[0].name=SSH
uci set firewall.@redirect[0].proto=tcp
uci set firewall.@redirect[0].src=wan
uci set firewall.@redirect[0].src_dport=2020
uci set firewall.@redirect[0].target=DNAT
## upnpd
uci set firewall.miniupnpd.family=IPv4
uci set firewall.miniupnpd.path=/usr/share/miniupnpd/firewall.include
uci set firewall.miniupnpd.reload=1
uci set firewall.miniupnpd.type=script
uci set firewall.miniupnpd=include
uci commit firewall

# Configure printer
uci set p910nd.@p910nd[0]=p910nd
uci set p910nd.@p910nd[0].bidirectional=1
uci set p910nd.@p910nd[0].device=/dev/usb/lp0
uci set p910nd.@p910nd[0].enabled=1
uci set p910nd.@p910nd[0].port=9
uci commit p910nd

# Disable polipo
uci set polipo.general.enabled=0
uci commit polipo

# Enable dynamic DNS
uci set ddns.myddns.use_https=1
uci set ddns.myddns.update_url="$DDNSURL"
uci set ddns.myddns.ip_source=network
uci set ddns.myddns.ip_network=ge00
uci set ddns.myddns.domain=etorok.net
uci set ddns.myddns.username=home
uci del ddns.myddns.ip_url || true
uci del ddns.myddns.service_name || true
uci set ddns.myddns.enabled=1
uci commit ddns

uci set dhcp.@dnsmasq[0].rebind_domain=skylable.com
uci commit dhcp


# ==============================================
echo 'You should restart the router now for these changes to take effect...'
# --- end of script ---
