#! /bin/sh

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

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

ConfigureLib()
{
    echo =============== System Configuration Utility - SysCfg Installation  ==================
    rm -f /etc/ld.so.conf.temp
    sed -e '/\/usr\/local\/syscfg\/lib[[:space:]]*$/d' /etc/ld.so.conf > /etc/ld.so.conf.temp
    if [ $? -eq 0 ]
    then
      echo syscfg: Saving /etc/ld.so.conf to /etc/ld.so.conf.bak.
      cp -f /etc/ld.so.conf /etc/ld.so.conf.bak
      echo syscfg: Adding entry into /etc/ld.so.conf for the syscfg libraries.
      echo $LIBDIR >> /etc/ld.so.conf.temp
      cp -f /etc/ld.so.conf.temp /etc/ld.so.conf
      if [ $? -ne 0 ]
      then 
        echo syscfg: 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 syscfg: ERROR - Unable to add entry into /etc/ld.so.conf for the syscfg libraries.
      exit $ERR_SED
    fi

    echo syscfg: Creating static links for syscfg libraries.
    ldconfig
    if [ $? -ne 0 ]; then echo *ERROR* executing ldconfig.; exit $ERR_LDCONFIG; fi;
    rm -f $LIBDIR/lib*.so
    ln -s $LIBDIR/liblancfg.so.1 $LIBDIR/liblancfg.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libsolcfg.so.1 $LIBDIR/libsolcfg.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libpowercfg.so.1 $LIBDIR/libpowercfg.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libpefcfg.so.1.0 $LIBDIR/libpefcfg.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libusercfg.so.1.0 $LIBDIR/libusercfg.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libsercfg.so.1.0 $LIBDIR/libsercfg.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libfwadvcfg.so.1.0 $LIBDIR/libfwadvcfg.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libsystemcfg.so.1.0 $LIBDIR/libsystemcfg.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libbootdevice.so.1.0 $LIBDIR/libbootdevice.so
    if [ $? -ne 0 ]; then echo *ERROR* creating shared library static link.; exit $ERR_LN; fi;
    ln -s $LIBDIR/libbiosconfig.so.1.0 $LIBDIR/libbiosconfig.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 syscfg: Installation complete.

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

    echo
    echo The System Configuration Utility is located in the /usr/local/syscfg directory
    echo From that directory, run \"./syscfg -h\" for information on how to use the utility.
    echo

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

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

    rm -f /etc/ld.so.conf.temp
    sed -e '/\/usr\/local\/syscfg\/lib[[:space:]]*$/d' /etc/ld.so.conf > /etc/ld.so.conf.temp
    if [ $? -eq 0 ]
    then
      echo syscfg: Saving /etc/ld.so.conf to /etc/ld.so.conf.bak.
      cp -f /etc/ld.so.conf /etc/ld.so.conf.bak
      echo syscfg: Removing entry from /etc/ld.so.conf for the syscfg libraries.
      cp -f /etc/ld.so.conf.temp /etc/ld.so.conf
      if [ $? -ne 0 ]
      then 
        echo syscfg: 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 syscfg: ERROR - Unable to remove entry from /etc/ld.so.conf for the syscfg 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

