#!/bin/bash
####################################################################
#
#      File: S03cmos
#      Name: Stephen Wong
#
#      Description:
#       Initialization/Cleanup of klm_cmos
#	Actual location on disk: /etc/rc.d/rcS.d
#
#
# Copyright (c) 2009, 2015 by cisco Systems, Inc.
# All rights reserved.
#
#
# $Id: S15cmos,v 1.1 2002/10/19 01:03:31 hzhong Exp $
# $Source: /auto/vwsvua/kirsten/sanos6/VegasSW__isan__infra__kspace__cmos/S15cmos,v $
# $Author: hzhong $
#
#####################################################################

# it needs to be before S04check-flash-noinit
    
. /etc/init.d/mod_ins/module-load-functions

boot_debug "Executing /etc/init.d/mod_ins/module-load-functions"

sanitize_rtc_date() {
    local rtc_year=$(cat /sys/class/rtc/rtc0/date | cut -d'-' -f1)
    if [ $rtc_year -gt 2030 -o $rtc_year -lt 2000 ]; then
        boot_debug "HWCLOCK(RTC) Year is bad ($rtc_year). Setting to 2000."
        date -s "2000-01-01"
        /sbin/hwclock --systohc --utc
    fi
}

case $1 in
    start)
	if [ -n "$IS_N3K" ]; then
		action "" insmod -f /lib/modules/klm_cmos_n3k.o  > /dev/null
		devno=`grep cmos /proc/devices | awk '{print $1}'`
		mknod -m 666 /dev/cmos c $devno 0
		boot_debug " this is for N3K"

	else
		action "" insmod -f /lib/modules/klm_cmos.o  > /dev/null
		devno=`grep cmos /proc/devices | awk '{print $1}'`
		mknod -m 666 /dev/cmos c $devno 0

	fi
    # Sometimes we see that the RTC date (year) is coming up
    # with > 2038. This does not work well for the 32bit GTOD apps.
    # For example hwclock asserts out when the rtc date was in year 2041.
    # So sanitize it.
	sanitize_rtc_date

    ;;
    stop)
	if [ -n "$IS_N3K" ]; then
		action "" rmmod klm_cmos_n3k
        	rm -f /dev/cmos

	else
		action "" rmmod klm_cmos
        	rm -f /dev/cmos
	
	fi

    ;;
esac
