#!/bin/sh
#
# functions      common functions 
#
# description: # This file contains functions to be used by most or all
#		apollo shell scripts in the /etc/platform and /root directories.
#
#              
#
### BEGIN INIT INFO
# Provides: functions
# Should-Start: functions 
# Short-Description: useful common function 
#  
### END INIT INFO


RETVAL=0

#########################################
# Get pid of process
#
#   $1 - process name
#   $2 - silent mode
#
#   returns - pid
#
check_status() {
    local status=`pgrep $1`
    pgrep $1 &> /dev/null
    rc=$?	
    [ "$2" -gt 0 ] && return $rc	
    if [ "$rc" -gt 0 ]; then
        echo $"$1 not running"
    else
        echo $"$1 running pid: $status"
    fi
    return $rc
}



#########################################
# Start process
#
#   $1 - process name
#   $2 - the user name by which the process should be started
#   $3 - options
#
#   returns - status
#
start() {
    local process=$1
    local user=$2
    shift; shift;
    local opt="$@"
    check_status $process 0
    RC=$?
    if [ "$RC" -gt 0 ]; then
       	echo -n $"Starting $process: "
       	start-stop-daemon -S -x $process -q -c $user -- $opt 
       	check_status $process 1
       	RETVAL=$?
       	if [ "$RETVAL" -eq 0 ]; then 
	   echo " Success"
	else
	   echo " Fault"
       	fi
    fi
    return $RETVAL
}


#########################################
# Stop process
#
#   $1 - process name
#
#   returns - status
#
stop() {
    local process=$1	
    check_status $process 0
    RC=$?
    if [ "$RC" -eq 0 ]; then
       echo -n $"Stopping $process: "
       start-stop-daemon -K -x $process -q
       usleep 500000
       check_status $process 1
       RETVAL=$?
       if [ "$RETVAL" -eq 0 ]; then 
           echo "Failed to stop daemon. Wait a little bit more"
           usleep 250000
           check_status $process 1
           RETVAL=$?                                        
           if [ "$RETVAL" -eq 0 ]; then
               echo "Failed to stop daemon. Trying again with SIGKILL"
               start-stop-daemon -K -x $process -q -s KILL
               check_status $process 1
               RETVAL=$?
               if [ "$RETVAL" -eq 0 ]; then 
	           echo " Fault"
                   RETVAL=1
               else 
                   echo " Success"
                   RETVAL=0
              fi
           else
               echo " Success"                          
               RETVAL=0  
           fi
	else
	   echo " Success"
           RETVAL=0
       	fi
    fi
    return $RETVAL
}


#########################################
# The Process start/stop/restart script 
# action
#
#   $1 - process name
#
#   returns - nothing
#
proc_action() {
     local prog=`find /etc -name [0-9][0-9]-$1`
     local dbus=`pgrep dbus-daemon`
     if [ "$2" = "stop" ]; then 
        if [ ! -z $prog ]; then
           $prog $2 
        fi    
     else
        if [ "$dbus" != "" ]; then 
           pkill -HUP dbus-daemon
           if [ ! -z $prog ]; then
              $prog $2 
           fi    
        fi
    fi      
}

#########################################
# Prepare/update file usb_gadget.conf
# with parameters passed to kernel
# object for g_ether driver
# action
#
#   $1 - MAC address to use
#
#   returns - nothing
#
create_usb_gadget_params() {
    # Prepare usb_gadget communication drivers parameters
    HWADDR=$HWADDR2

    # If MAC for usb0 is not set -> use "localized" version of MAC0
    if [ -z "$HWADDR" ]; then
       logger "Auto MAC for usb0 is enabled"
       HWADDR=$(cat /proc/mac0);
    fi

    if [ ! -d /etc/modprobe.d ]
    then
        mkdir -p /etc/modprobe.d
    fi

    product=`sed -e 's/phoenix/Luxe 8500i/' -e 's/griffin/Luxe 6200m/'</proc/device-tree/model`
    sn=""
    if [ -f /etc/platform/sys/SERIAL ]
    then
        sn=`sed -ne 's/^ENCLOSURE#//p'</etc/platform/sys/SERIAL`
    fi

    dev_addr=""
    host_addr=""

    if [ ! -z "$HWADDR" ]
    then
        # Form usb_gadget dev and host MAC addresses on the basis of $HWADDR
        # Both addresses will have a bit-sign of locally administered address. From each other, 
        # they will differ in the high bit of the first byte.
        # A locally administered MAC address is similar to a LAN MAC address. 
        # In this way, we can be sure that they  will not collide with any hardware on out network 
        # that use a factory burned-in MAC address. 

        oui=$(printf "%02X\n" $((0x`echo $HWADDR | awk -F ":" '{ print $1}'` ^ 0x80 | 0x02)))
        dev_addr=`echo $HWADDR | sed -e 's/^..:/'$oui':/'`
        oui=$(printf "%02X\n" $((0x`echo $HWADDR | awk -F ":" '{ print $1}'`|0x02)))
        host_addr=`echo $HWADDR | sed -e 's/^..:/'$oui':/'`
    fi
    if [ ! -z $dev_addr ] && [ ! -z $host_addr ]; then
        echo "options g_ether dev_addr=$dev_addr host_addr=$host_addr iManufacturer=\"Equinox Payments\" iProduct=\"$product\" iSerialNumber=$sn idProduct=0x4010 idVendor=0x04b3" > /etc/modprobe.d/usb_gadget.conf
    else
        echo "options g_ether iManufacturer=\"Equinox Payments\" iProduct=\"$product\" iSerialNumber=$sn idProduct=0x4010 idVendor=0x04b3" > /etc/modprobe.d/usb_gadget.conf
    fi

    echo "options g_serial iManufacturer=\"Equinox Payments\" iProduct=\"$product\"" >> /etc/modprobe.d/usb_gadget.conf
}
