#!/bin/sh
#----------------------------------------------------------------------------
# /etc/init.d/rtc - initial boot script
#
# Creation   :  2015-12-07 hbfl
# Last Update:  $Id$
#
# Copyright (c) 2015-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# ---------------------------------------------------------------------------
# Update the mount program's mtab file after
# all local filesystems have been mounted.
# ---------------------------------------------------------------------------

lsb=false

if ${lsb}
then
    # read init-functions
    . /lib/lsb/init-functions
else
    # read functions
    . /etc/init.d/functions
fi

# ---------------------------------------------------------------------------
# usage
# ---------------------------------------------------------------------------
usage()
{
cat <<EOF

 Usage:
 ${0}
                -q, --quiet      suppress all normal output
                [start]
EOF
}

# ---------------------------------------------------------------------------
# start
# ---------------------------------------------------------------------------
start()
{
    /usr/sbin/modprobe rtc-cmos 2>/dev/null

    call_dir=$(pwd)
    cd /dev

    if [ -f /sys/class/rtc/rtc0/dev ]
    then
        RTC_MINOR=$(/usr/bin/gawk -F: '{print $1}' /sys/class/rtc/rtc0/dev)
        RTC_MAJOR=$(/usr/bin/gawk -F: '{print $2}' /sys/class/rtc/rtc0/dev)
    else 
        if [ -f /sys/class/misc/rtc/dev ]
        then
            RTC_MINOR=$(/usr/bin/gawk -F: '{print $1}' /sys/class/misc/rtc/dev)
            RTC_MAJOR=$(/usr/bin/gawk -F: '{print $2}' /sys/class/misc/rtc/dev)
        fi
    fi

    if [ -n "${RTC_MAJOR}" -a -n "${RTC_MINOR}" ]
    then
        /usr/bin/rm -f rtc rtc0
        /usr/bin/mknod -m 0600 rtc0 c ${RTC_MINOR} ${RTC_MAJOR}
        /usr/bin/ln -s rtc0 rtc
    fi

    cd ${call_dir}
}
# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
main()
{
    while [ ${#} -gt 0 ]
    do
        case "${1}" in
        -q|--quiet)
            _quiet=true
            shift
            ;;
        *)
            _action=${1}
            shift
            ;;
        esac
    done

    case "${_action}" in
    start)
        start
        ;;
    *)
        usage
        exit 1
        ;;
    esac

    exit 0
}

# ---------------------------------------------------------------------------
# call function main
# ---------------------------------------------------------------------------
main "${@}"

# ---------------------------------------------------------------------------
# end
# ---------------------------------------------------------------------------