#!/bin/sh

# This is the script which will be invoked in /etc/rc.sysinit only when the
# target board for the first time. It configures basic services to keep and
# make factory configuration backup for future restore.
. /etc/functions.sh
. /lib/config/uci.sh

#Impoart Intel gpg public key
[ -f /usr/bin/rpm ] && rpm --import /etc/gw_rpm_pgp_public_key 2>/dev/null

if [ -e /etc/.extend_partition ] ; then
    # When dd a image to a lager size device and boot it, need
    # to extend partition to use all the space in the device.
    /sbin/part_extend -E -e >/var/log/part_extend.log 2>&1
    rm -rf /etc/.extend_partition
else
    # Fix backup GPT table is not at the end of the disk issue
    /sbin/part_extend >/var/log/part_extend.log 2>&1
fi

# Quark board initialization
if grep -q "intel-quark" /etc/board_name 2>/dev/null; then
    board_name="$(cat /sys/class/dmi/id/board_name)"
    if [ -z "$board_name" ] ; then
        board_name="$(dmidecode -s baseboard-product-name 2>/dev/null)"
    fi

    if [ -n "$board_name" ] ; then
        echo "$board_name Board Initializing ..."
        if [ "$board_name" == "ReliaGate" ] ; then
            sed -i "s/HE910.*/HE910|1bc7|0021|3g|ttyACM5|ttyACM8/g" \
                /etc/modem_cell_default
        fi

        echo "$board_name" > /etc/board_name
        echo "$board_name" > /etc/device_name
    fi
fi

# Set hostname from ethernet MAC address/kernel UUID/date random
# eth0 up as workaround for quark board eth0 hw addr isn't ready
ifconfig eth0 up
hostname_id=$(cat /sys/class/net/eth0/address 2>&- | sed 's/://g')
[ -z "${hostname_id}" ] && {
    hostname_id=$(cat /proc/sys/kernel/random/uuid 2>&-)
    [ -z "${hostname_id}" ] && hostname_id=$(date +%N)
}
declare -u hostname
hostname="WR-IDP-${hostname_id:0-4}"
uci set system.@system[0].hostname="$hostname"
uci commit system
echo "$hostname" > /proc/sys/kernel/hostname
sed -i "s/127\.0\..*//g" /etc/hosts > /dev/null 2>&1
sed -i "1i127.0.0.1 localhost\\" /etc/hosts > /dev/null 2>&1
sed -i "2i127.0.1.1 $hostname\\" /etc/hosts > /dev/null 2>&1

# Find eth1~x interfaces and add them into lan bridge
lan_type="$(uci -q get network.lan.type)"
lan_ifname="$(uci -q get network.lan.ifname)"
if [ "$lan_type" == "bridge" -a ! -e /etc/.saved_rootfs ] ; then
    for i in $(find /sys/class/net/ -name "eth*" | grep -v "eth0") ; do
        iface=$(basename $i)
        echo $lan_ifname | grep -q "$iface" && continue
        lan_ifname="$iface $lan_ifname"
    done
    uci -q set network.lan.ifname="$lan_ifname"
    uci commit network
fi

# create softlink for pptp init script
update-alternatives --install /bin/ip bin-ip /sbin/ip 99

# Generate factory configuration tar ball
copy_files="/etc/hosts
/etc/ethers"
copy_dirs="/etc/config"
config_file="factory_config.tgz"
tmp=/tmp/config.$$
[ ! -e /etc/$config_file ] && {
	rm -rf $tmp 2>/dev/null
	mkdir -p $tmp 2>/dev/null
	date > $tmp/config.date
	echo "WR_IntelligentDevice_Default" > $tmp/config.name
	cat /etc/board_name > $tmp/config.boardtype
	for file in $copy_files
	do
		mkdir -p `dirname $tmp$file` 2>/dev/null
		cp $file $tmp$file
	done
	for dir in $copy_dirs
	do
		mkdir -p $tmp$dir
		cp -afr $dir/* $tmp$dir/ 2>/dev/null
	done
	( cd $tmp ; tar -zcf $config_file * )
	mv $tmp/$config_file /etc/$config_file
	rm -rf $tmp 2>/dev/null
}

#fix the bug: the secondary screen is dark when dual display
if [ -e /etc/X11/xorg.conf -a ! -e /etc/.saved_rootfs ] ; then
    sed -i '/Attr\/26/s/18/24/g'  /etc/X11/xorg.conf
    sed -i '/Attr\/26/a\\ \ \ \ Option\ \ \ \ \ \"ALL\/1\/Port\/4\/Attr\/45\"\ \ \ \ \"0\"' /etc/X11/xorg.conf
fi

# The openwrt initrcs are enabled by rpm-postinst. They will not start by
# systemd sysv generator, so manually start these initrcs.
systemctl daemon-reload
for i in /etc/rc.d/S* ; do
	[ -f "$i" ] || continue
        service_path=`readlink -f $i`
        service_name=`basename $service_path`
        systemctl start $service_name
done

exit 0
