#!/bin/sh 

#
# Script to configure PowerPath into initramfs
#

inst_hook pre-trigger 02 "${moddir}/emcp_pre-trigger.sh"
inst_simple "${moddir}/emcp_settled.sh"     /etc/opt/emcpower/emcp_settled.sh 

inst /bin/fgrep
inst /bin/awk
inst /bin/echo 
inst /sbin/lsmod
inst /sbin/modprobe

inst /sbin/pp_inq
inst /etc/opt/emcpower/emcpmgr /etc/opt/emcpower/emcpmgr

inst_rules  /etc/udev/rules.d/05-udev-pp.rules
inst_simple /lib/udev/pp_udev.sh

#
# When creating initramfs using dracut for the first time, 
# save the device config files into $dbbakdir.
# Every subsequent creation of initramfs will use the saved config files
#

dbbakdir=/etc/emc/bfs
if [ -d ${dbbakdir} ]; then
    # Not first time, so use the saved configuration files
    inst_simple ${dbbakdir}/emcp_devicesDB.idx /etc/emcp_devicesDB.idx
    inst_simple ${dbbakdir}/emcp_devicesDB.dat /etc/emcp_devicesDB.dat
else
    ## first-time creation of initramfs.
    inst_simple /etc/emcp_devicesDB.idx /etc/emcp_devicesDB.idx
    inst_simple /etc/emcp_devicesDB.dat /etc/emcp_devicesDB.dat

    # take backup of database when creating initramfs with PP
    # for the first time

    mkdir -p ${dbbakdir}
    if [ $? -ne 0 ]; then 
        echo "Unable to create backup directory $dbbakdir for database"
        exit 1
    fi

    cp -pf /etc/emcp_devicesDB.idx ${dbbakdir}/emcp_devicesDB.idx 
    if [ $? -ne 0 ]; then
        echo  "Unable to save backup of /etc/emcp_devicesDB.idx"
        exit 1
    fi
    cp -pf /etc/emcp_devicesDB.dat ${dbbakdir}/emcp_devicesDB.dat 
    if [ $? -ne 0 ]; then
        echo "Unable to save backup of /etc/emcp_devicesDB.dat"
        exit 1 
    fi
fi

inst_simple /etc/opt/emcpower/.pp_version 

pp_version=`cat /etc/opt/emcpower/.pp_version`
os_version=$(uname -r)
pp_install_dir=/etc/opt/emcpower/EMCpower.LINUX-${pp_version}

for i in $(ls ${pp_install_dir}/bin/.drivers_*  2>/dev/null); 
do
    inst_simple ${i}
done

for mods in $(ls /lib/modules/${os_version}/powerpath/*.ko 2>/dev/null); 
do
    inst ${mods}
done
