--- /dev/null
+#!/bin/sh
+
+# Print usage
+if [ "$#" -eq 0 ] || [ "$1" == "-h" ]; then
+ echo "This is a simple script that register connected device into the system"
+ echo "/etc/config/hostapd-psk /etc/config/dhcp will be changed accordingly"
+ echo "~/vigilia_setup/register/register_device.sh will be executed"
+ echo ""
+ echo "Usage:"
+ echo " ./connect_device.sh [-h]"
+ echo " ./connect_device.sh [-co <specific-pw> <MAC address> <name> <default_pw>]"
+ echo ""
+ echo "Options:"
+ echo " -h show this usage"
+ echo " -co connect a new device"
+ echo ""
+
+elif [ "$1" == "-co" ]; then
+
+ # 0
+ # Get password from $2
+ # Supposing that key for radio0 and radio1 are equal
+ PW=$2
+
+ # Save default password from $5. If the argument is empty, let's use THE default one.
+ DEFAULT="1qaz2wsx3edc"
+ if [ ! -z "$5" ]; then
+ DEFAULT=$5
+ fi
+
+ # 1
+ # Get MAC address and IP address from dhcp.leases file.
+ # Below scripts will find the most recently connected device by sorting the first column of lease file,
+ # which is time of lease expiry, in epoch time
+ # Before that, the file named devices.dat could not exist, so touch it first
+ touch ~/vigilia_setup/register/devices.dat
+ chmod 666 ~/vigilia_setup/register/devices.dat
+
+ # now ready to use devices.dat. Get numbers of records in devices.dat
+ NR=$(cat ~/vigilia_setup/register/devices.dat | wc -l)
+
+ # use temp file for procedure so that we don't touch original lease file
+ cp /tmp/dhcp.leases leases.temp
+
+
+ grep $3 leases.temp > temp.temp
+ rm leases.temp
+ mv temp.temp leases.temp
+
+# for i in `seq 1 $NR`
+# do
+# #get line number(LN)
+# LN="${i}p"
+# #get target MAC address(TMAC) from devices.dat
+# TMAC=$(sed -n ${LN} ~/vigilia_setup/register/devices.dat | awk '{print $1}')
+# echo "TMAC: $TMAC"
+# #remove the record with certain TMAC in the dhcp file so that we can get new one at the end
+# sed -e /${TMAC}/d leases.temp > leases.temp.temp
+# rm leases.temp
+# mv leases.temp.temp leases.temp
+# done
+
+ # Filter done. There should be only one line in leases.temp, but just in
+ # case sort the epoch time
+# RECENT=$(sort -nrk1,1 leases.temp | head -1)
+# echo "Recent: $RECENT"
+
+ # Get MAC, IP, NAME from RECENT
+ MAC=$(awk '{print $2}' leases.temp)
+ IP=$(awk '{print $3}' leases.temp)
+ # below was used when there were no name argument in the script
+ # NAME=$(echo $RECENT | awk '{print $4}')
+ NAME=$4
+
+ # 2
+ # Add record to the database with ~/vigilia_setup/register/register_device.sh
+ echo "MAC: $MAC"
+ echo "IP: $IP"
+ echo "PW: $PW"
+ echo "Name: $NAME"
+ ~/vigilia_setup/register/register_device.sh -a $MAC $IP $PW $NAME
+
+ # 3
+ # rewind the default password and apply config files
+ uci commit
+ ~/vigilia_setup/register/change_default_pw.sh -ch $DEFAULT
+ /sbin/wifi
+
+else
+ echo "Unknown option. Please run ./connect_device.sh -h for usage."
+
+fi
+
+