#!/bin/sh

chkpath() {
  DIRPATH=`/usr/bin/dirname $1`
  if [ ! -d "$DIRPATH" ] ; then 
    echo "Creating directory $DIRPATH"
    /bin/mkdir -m 755 -p $DIRPATH
  fi
}

mklink() {
  if [ ! -r "$1" ] ; then
    echo "$1 does not exist"
    return
  fi

  chkpath "$2"
  if [ -r "$2" -o -h "$2" ] ; then
    if [ -h "$2" ] ; then
      echo "symbolic link $2 currently exists.  Removing."
      /bin/rm -f $2
    else
      echo "$2 is a real file.  Can not create link"
      return
    fi
  fi
  /bin/ln -s $1 $2
}

/bin/echo "   -- Updating symbolic links"


if [ -z "/usr/local/lexmark" ] ; then
  CWD=`/bin/pwd`;
  BASEDIR=`/bin/dirname $0`
  cd /usr/local/lexmark
  BASEDIR=`/bin/pwd`;
  cd $CWD
fi

copy_overwrite() {
  chkpath "$2"
  if [ -r "$2" ] ; then
    /bin/rm -f $2
  fi
  cp $1 $2
}
copy_nooverwrite() {
  chkpath "$2"
  if [ -r "$2" ] ; then
    return
  fi

  cp $1 $2
}
RPM_INSTALL_PREFIX=$1;

if [ -z $RPM_INSTALL_PREFIX ] ; then
  RPM_INSTALL_PREFIX=`/usr/bin/dirname $0`
  cd $RPM_INSTALL_PREFIX
fi

mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/jre/lib/amd64/libjsig.so $RPM_INSTALL_PREFIX/unix_scan_drivers/jre/lib/amd64/server/libjsig.so
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/jre/man/ja_JP.UTF-8 $RPM_INSTALL_PREFIX/unix_scan_drivers/jre/man/ja
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers /usr/lexscan
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/lib/sane/libsane-lexmark_nscan.so.1.0.16 /usr/lib64/sane/libsane-lexmark_nscan.so
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/lib/sane/libsane-lexmark_nscan.so.1.0.16 /usr/lib64/sane/libsane-lexmark_nscan.so.1
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/lib/sane/libsane-lexmark_nscan.so.1.0.16 /usr/lib64/sane/libsane-lexmark_nscan.so.1.0.16
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/ar/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/ar/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/de/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/de/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/es/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/es/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/fr/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/fr/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/it/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/it/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/ja/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/ja/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/ko/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/ko/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/pl/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/pl/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/pt/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/pt/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/ru/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/ru/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/zh/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/zh/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/share/locale/zh_TW/LC_MESSAGES/sane-lexmark_nscan.mo /usr/share/locale/zh_TW/LC_MESSAGES/sane-lexmark_nscan.mo
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/etc/lexmark_nscan.conf /etc/sane.d/lexmark_nscan.conf
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/etc/scan13.conf /etc/sane.d/scan13.conf
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/etc/scan14.conf /etc/sane.d/scan14.conf
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/etc/scan16.conf /etc/sane.d/scan16.conf
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/etc/scan17.conf /etc/sane.d/scan17.conf
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/etc/scan6.conf /etc/sane.d/scan6.conf
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/etc/scan.conf /etc/sane.d/scan.conf
mklink $RPM_INSTALL_PREFIX/unix_scan_drivers/etc/udev/rules.d/99-lexmark.rules /etc/udev/rules.d/99-lexmark.rules
  
if [ -f "/sbin/udevadm" ] ; then
   udevadm_version=`/sbin/udevadm version`
   udevadm_new_version=126
   if [ $udevadm_version -gt $udevadm_new_version ] ; then
        /sbin/udevadm control --reload-rules
   else
        /sbin/udevadm control --reload_rules
   fi
fi

exit 0
