#!/bin/sh
# Copyright(C) 2006 Nokia Corporation.

#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.

#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.

#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
#  USA



umask 022
PATH=/sbin:/bin:/usr/sbin:/usr/bin

mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys  

if [ -x /etc/ptmalloc.conf ]
then
	. /etc/ptmalloc.conf
fi

# This doesn't really belong here, but we have start this so early
start_bootchart(){
	if [ -e /bootchart -a -e /etc/init.d/bootchart ]
	then
		/etc/init.d/bootchart start
	fi
}

mount_devpts() {
	TTYGRP=5
	TTYMODE=620

	if [ ! -d /dev/pts ]
	then
	    mkdir /dev/pts
	fi

	if [ ! -c /dev/ptmx ]
	then
	    mknod /dev/ptmx c 5 2
		chmod 666 /dev/ptmx
	fi
			
	 mount -n -t devpts -ogid=$TTYGRP,mode=$TTYMODE devpts /dev/pts
#	 mount -n -t tmpfs  -osize=1M tmpfs /var/run
	rm -rf /var/tmp/*

	if [ ! -e /tmp/dsmesock ]
	then
		mount -n -o bind /mnt/initfs/tmp /tmp
	fi

}

# I hate this hack.  -- Md
make_extra_nodes () {
    if [ "$(echo /lib/udev/devices/*)" != "/lib/udev/devices/*" ]; then
	cp -a /lib/udev/devices/* /dev/
    fi
  grep '^[^#]' /etc/udev/links.conf | \
  while read type name arg1; do
    [ "$type" -a "$name" -a ! -e "/dev/$name" -a ! -L "/dev/$name" ] ||continue
    case "$type" in
    L)
      ln -s $arg1 /dev/$name
      ;;
    D)
      mkdir -p /dev/$name
      ;;
    M)
      mknod /dev/$name $arg1 && chmod 600 /dev/$name
      ;;
    *)
      echo "unparseable line ($type $name $arg1)"
      ;;
    esac
  done
}

# When modifying this script, do not forget that between the time that
# the new /dev has been mounted and udevsynthesize has been run there will be
# no /dev/null. This also means that you cannot use the "&" shell command.

	start_bootchart

if [ -x /sbin/udevd ]
then
    . /etc/udev/udev.conf
    ACTION=add
    echo -n "Mounting a tmpfs over /dev..."  
    mount -n -o size=1M,mode=0755 -t tmpfs none /dev
    echo "done."
    mkdir -p /dev/.udev/db /dev/.udev/queue  
    echo "" > /sys/kernel/uevent_helper
    echo -n "Creating device nodes... "
    /sbin/udevd --daemon
    /sbin/udevsynthesize
    echo -n " and some more.."
    make_extra_nodes
    echo "done."
fi

mount_devpts
mount -n -o size=1M,nosuid,nodev -t tmpfs tmpfs /dev/shm

chroot /mnt/initfs/ /usr/bin/retutime -i || true

if [ -e /home/user/first-boot-flag ]; then
    hostname="`osso-product-info -qOSSO_PRODUCT_SHORT_NAME | sed 's/ /-/g'`-`osso-product-info -qOSSO_PRODUCT_RELEASE_VERSION | cut -d. -f3`"
    echo $hostname > /etc/hostname

    if [ -e /etc/hosts ]
    then
	temphosts=`tempfile`
	sed -e "/^127.0.0.1\s/ s/^.*$/127.0.0.1 $hostname localhost/" /etc/hosts > $temphosts
	mv $temphosts /etc/hosts
    else
	echo "127.0.0.1 $hostname localhost" > /etc/hosts
    fi
    chmod 644 /etc/hosts
    full_name=`osso-product-info -qOSSO_PRODUCT_RELEASE_FULL_NAME`
    echo "$full_name \n \l" > /etc/issue
    echo "" >> /etc/issue
    echo "$full_name %h" > /etc/issue.net
fi

hostname -F /etc/hostname
ifup lo &

#echo /sbin/udevsend > /proc/sys/kernel/hotplug

#: > /var/run/utmp
#chmod 664 /var/run/utmp
#chgrp utmp /var/run/utmp

[ -L /etc/mtab ] || ln -sf /proc/mounts /etc/mtab
#cat /proc/mounts > /etc/mtab

echo 0 > /proc/sys/net/ipv4/conf/all/accept_redirects 
echo 0 > /proc/sys/net/ipv4/conf/default/accept_redirects
echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 1 > /proc/sys/net/ipv4/conf/default/rp_filter
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
echo "49152 65535" > /proc/sys/net/ipv4/ip_local_port_range 
echo 6000 > /sys/class/bluetooth/hci0/idle_timeout

exit 0
