#! /bin/sh

LIBDIR=/usr/local/flashupdt/lib
ERR_FILE_COPY=1
ERR_CONF=2
ERR_SED=3
ERR_LDCONFIG=4
ERR_LN=5
ERR_USAGE=6

CopyFiles()
{
    echo flashupdt: Copying flashupdt libraries to $LIBDIR folder.
    mkdir -p $LIBDIR
    cp ./lib*.so* $LIBDIR/
    if [ $? -ne 0 ]
    then
      echo flashupdt: ERROR - Failed to copy OFU libraries from current working directory to $LIBDIR
      exit $ERR_FILE_COPY
    fi
}

ConfigureLib()
{
    echo =============== One-Boot Flash Update Utility Installation  ==================
    rm -f /etc/ld.so.conf.temp
    sed -e '/\/usr\/local\/flashupdt\/lib[[:space:]]*$/d' /etc/ld.so.conf > /etc/ld.so.conf.temp
    if [ $? -eq 0 ]
    then
      echo flashupdt: Saving /etc/ld.so.conf to /etc/ld.so.conf.bak.
      cp -f /etc/ld.so.conf /etc/ld.so.conf.bak
      echo flashupdt: Adding entry into /etc/ld.so.conf for the flashupdt libraries.
      echo $LIBDIR >> /etc/ld.so.conf.temp
      cp -f /etc/ld.so.conf.temp /etc/ld.so.conf
      if [ $? -ne 0 ]
      then 
        echo flashupdt: ERROR - Unable to modify ld.so.conf file.
        rm -f /etc/ld.so.conf.temp
        exit $ERR_CONF
      fi
      rm -f /etc/ld.so.conf.temp
    else
      echo flashupdt: ERROR - Unable to add entry into /etc/ld.so.conf for the flashupdt libraries.
      exit $ERR_SED
    fi

    echo flashupdt: Creating static links for flashupdt libraries.
    ldconfig
    if [ $? -ne 0 ]; then echo *ERROR* executing ldconfig.; exit $ERR_LDCONFIG; fi;
    rm -f $LIBDIR/lib*.so
    ln -s $LIBDIR/libbiosupdate.so.9 $LIBDIR/libbiosupdate.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libbud.so.9 $LIBDIR/libbud.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libsmbios.so.9 $LIBDIR/libsmbios.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libfwupdate.so.9 $LIBDIR/libfwupdate.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libfwdupdate.so.9 $LIBDIR/libfwdupdate.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libcfgfile.so.9 $LIBDIR/libcfgfile.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libsdrdata.so.9 $LIBDIR/libsdrdata.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libfrudata.so.9 $LIBDIR/libfrudata.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libRunTime.so.9 $LIBDIR/libRunTime.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libipmi.so.1 $LIBDIR/libipmi.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ldconfig
    if [ $? -ne 0 ]; then echo *ERROR* executing ldconfig.; exit $ERR_LDCONFIG; fi;

    echo flashupdt: Installation complete.

    if [ "true" != "$OFU_INSTALLME" ]; then

    echo
    echo The One-Boot Flash Update Utility is located in the /usr/local/flashupdt directory
    echo From that directory, run \"./flashupdt -h\" for information on how to use the utility.
    echo

    VERSION=`uname -r|sed "s/-/./"`
    

    fi
}

UnConfigureLib()
{
    echo flashupdt: Removing static links for flashupdt libraries.
    rm -f $LIBDIR/lib*.so
    rm -f $LIBDIR/lib*.so.9

    rm -f /etc/ld.so.conf.temp
    sed -e '/\/usr\/local\/flashupdt\/lib[[:space:]]*$/d' /etc/ld.so.conf > /etc/ld.so.conf.temp
    if [ $? -eq 0 ]
    then
      echo flashupdt: Saving /etc/ld.so.conf to /etc/ld.so.conf.bak.
      cp -f /etc/ld.so.conf /etc/ld.so.conf.bak
      echo flashupdt: Removing entry from /etc/ld.so.conf for the flashupdt libraries.
      cp -f /etc/ld.so.conf.temp /etc/ld.so.conf
      if [ $? -ne 0 ]
      then 
        echo flashupdt: ERROR - Unable to modify ld.so.conf file.
        rm -f /etc/ld.so.conf.temp
        exit $ERR_CONF
      fi
      rm -f /etc/ld.so.conf.temp
      ldconfig
    else
      echo flashupdt: ERROR - Unable to remove entry from /etc/ld.so.conf for the flashupdt libraries.
      exit $ERR_SED
    fi
}

case "$1" in
  start)
    ConfigureLib
    RETVAL=$?
    ;;
  stop)
    UnConfigureLib
    RETVAL=$?
    ;;
  test)
    CopyFiles
    ConfigureLib
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $0 {start|stop|test}"
    RETVAL=$ERR_USAGE
    ;;
esac

exit $RETVAL

