#raspberry
LICENSE=/etc/3S.dat

# function create_nodes
# creates nodes required to access device properties
# pre: none
# exit: none
# post:
#   - node /dev/char_dev is created for bcm2708_vcio
create_nodes () {
    local NODEID
    if [ ! -c /dev/char_dev ]; then
    	#Old Kernel, where /dev/vcio is not present
    	if [ ! -c /dev/vcio ]; then
	        NODEID=$(cat /sys/devices/virtual/bcm2708_vcio/vcio/dev | tr ':' ' ')
        	mknod /dev/char_dev c $NODEID
        #New Kernel, where /dev/vcio replaces the use of old /char_dev
        else
        	ln -sf /dev/vcio /dev/char_dev
        fi
    fi
}

# function create_symlinks
# determines correct CPU architecure and symlinks license and executable accordingly
# pre: none
# exit: 5 if correct executable does not exist
# post:
#   - correct executable is symlinked to $EXEC
#   - correct license ist symlinked to $LICENSE
create_symlinks () {
    local DAEMON
    local DAEMON_ARMV6=/opt/codesys/bin/codesyscontrol_armv6l_raspberry.bin
    local DAEMON_ARMV7=/opt/codesys/bin/codesyscontrol_armv7l_raspberry.bin
    local USERCONFIGFILE=/etc/CODESYSControl_User.cfg

    # on armv6 always start armv6 runtime
    ARCH=$(lscpu | sed -n '/^Architecture/ !d; s,^Architecture[:\t ]*\([a-zA-Z0-9_]*\).*,\1, p');

    # if lscpu returns Architecture = armv6l :
    if [ $ARCH = 'armv6l' ]; then
        DAEMON=$DAEMON_ARMV6
        CONFIG_ARCH=armv6l
    else
        CONFIG_ARCH=$(sed -n '/^\[CmpRasPi\]/,/^[\t ]*$/ !d; /^[\[;]/ d; /^Architecture=/ s,^Architecture=\([a-zA-Z0-9_]*\).*,\1,p  ' $USERCONFIGFILE);
        if [ $CONFIG_ARCH = 'armv7l' ]; then
            DAEMON=$DAEMON_ARMV7
        else
            DAEMON=$DAEMON_ARMV6
            CONFIG_ARCH=armv6l
        fi
    fi

    # exit script if package is not installed
    if [ ! -x "$DAEMON" ]; then
        log_failure_msg "Error: codesyscontrol is not installed"
        exit 5
    fi

    # link correct daemon
    ln -sf $DAEMON $EXEC

    # link correct license
    ln -sf /etc/3S_$CONFIG_ARCH.dat $LICENSE
}

# function pin_runtime
# conditionally pins runtime process to specific CPU
# pre: runtime process's PID must be available in $PIDFILE
# exit: none
# post: if armv6 is configured runtime process will be pinned to first CPU
pin_runtime () {
    local PID=$(cat $PIDFILE)

    if [ $CONFIG_ARCH = 'armv6l' ]; then
        # pin codesys to first cpu!
        /usr/bin/taskset -a -p 01 $PID >/dev/null
    fi
}

# function remove_symlinks
# cleans up symlinks creaed by create_symlinks
# pre: none
# exit: none
# post: symlinks created by create_symlinks will be removed
remove_symlinks () {
    rm -f $LICENSE
    rm -f $EXEC
}

# function before_start_runtime
# hook function called from init-script
# pre: none
# exit: none
# post: see post conditions of create_symlinks
before_start_runtime () {
    create_nodes
    create_symlinks
}

# function after_start_runtime
# hook function called from init-script
# pre: none
# exit: none
# post: see post conditions of pin_runtime
after_start_runtime () {
    pin_runtime
}

# function before_stop_runtime
# hook function called from init-script
# pre: none
# exit: none
# post: none
before_stop_runtime () {
    :
}

# function after_stop_runtime
# hook function called from init-script
# pre: none
# exit: none
# post: see post conditions of remove_symlinks
after_stop_runtime () {
    remove_symlinks
}
