#!/bin/sh

## Enable copy configuration files. ("yes", "no" or otherwise "no")
## If set to "no", you can not switch to that mode after booted.
COPY_ONBOARD_WIFI_AP="yes"
COPY_ONBOARD_WIFI_STA="yes"

COPY_USB_WIFI_AP_RT28XX="yes"
COPY_USB_WIFI_AP_RTL8812="yes"
COPY_USB_WIFI_STA="yes"

COPY_USB_LAN_ADAPTER="yes"

## Set preferentially in AP mode. ("yes", "no" or otherwise "no")
## Even if set to "yes" or "no", you can switch to another mode after booted.
SET_AP_MODE="yes"

## Select preferentially USB device. ("rt28xx" or "rtl8812")
## If it is set to a name not in the list (in the supported device name list),
## it will not be configured device automatically.
SELECT_USB_DEVICE="rtl8812"

mkdir -p /Net

# Copy Ojbects
if [ -e /mnt/Net/_updates_ ] && [ -d /mnt/Net/_updates_ ]; then
  ## updated or added kernel drivers.
  for file in `(cd /mnt/Net/_updates_ && ls *.ko)`; do
    if [ "${file}" != "" ]; then
      cp -af /mnt/Net/_updates_/${file} /root/drv/
    fi
  done
  ## updated hostapd and wpa_supplicant.
  ## buildroot-2015.05 already has changed been hostapd-2.4 and wpa_supplicant-2.4.
  for file in hostapd hostapd_cli wpa_cli wpa_passphrase wpa_supplicant ; do
    if [ "${file}" != "" ]; then
      cp -af /mnt/Net/_updates_/${file} /usr/sbin/
    fi
  done
fi

## For use onboard (Marvell SD8787) WiFi chipset
if [ "${COPY_ONBOARD_WIFI_AP}" == "yes" ]; then
  ## copied Marvel WiFi Host Mode files
  cp -af /mnt/Net/onboard-ap/net-uap.sh /Net/
  cp -af /mnt/Net/onboard-ap/udhcpd-uap.conf /Net/
  cp -af /mnt/Net/onboard-ap/hostapd-uap.conf /Net/
fi
if [ "${COPY_ONBOARD_WIFI_STA}" == "yes" ]; then
  ## copied Marvel WiFi Client Mode files
  cp -af /mnt/Net/onboard-sta/net-mlan.sh /Net/
fi

## For use USB WiFi dongle chipsets
if [ "${COPY_USB_WIFI_AP_RT28XX}" == "yes" ] || [ "${COPY_USB_WIFI_AP_RTL8812}" == "yes" ]; then
  ## copied USB-WiFi Host Mode files
  cp -af /mnt/Net/usb-ap/net-wlan-ap.sh /Net/
  cp -af /mnt/Net/usb-ap/udhcpd-wlan.conf /Net/
  if [ "${COPY_USB_WIFI_AP_RT28XX}" == "yes" ]; then
    cp -af /mnt/Net/usb-ap/hostapd-rt28x.conf /Net/
  fi
  if [ "${COPY_USB_WIFI_AP_RTL8812}" == "yes" ]; then
    cp -af /mnt/Net/usb-ap/hostapd-8812an.conf /Net/
  fi
fi
if [ "${ENABLE_USB_WIFI_STA}" == "yes" ]; then
  ## copied USB-WiFi Client Mode files
  cp -af /mnt/Net/usb-sta/net-wlan-sta.sh /Net/
fi

## For select AP or STA mode, because the interface name is the same "wlan*".
if [ "${SET_AP_MODE}" == "yes" ]; then
  cp -af /mnt/Net/usb-ap/net-wlan-ap.sh /Net/net-wlan.sh
else
  cp -af /mnt/Net/usb-sta/net-wlan-sta.sh /Net/net-wlan.sh
fi

## For select the USB dongle device,
## because the device features are the different (e.g. 11a, 11g or 11n).
if [ "${SELECT_USB_DEVICE}" == "rt28xx" ]; then
  cp -af /mnt/Net/usb-ap/hostapd-rt28x.conf /Net/hostapd-wlan.conf
elif [ "${SELECT_USB_DEVICE}" == "rtl8812" ]; then
  cp -af /mnt/Net/usb-ap/hostapd-8812an.conf /Net/hostapd-wlan.conf
  ## 802.11ac Driver Load
  insmod /root/drv/8812au.ko
fi

## For use USB Ether dongle chipsets
if [ "${ENABLE_USB_LAN_ADAPTER}" == "yes" ]; then
  ## copied USB-Ether Mode files
  cp -af /mnt/Net/usb-lan/net-usblan.sh /Net/
fi

# EOF #

