#!/bin/sh

APPLICATION_DIR="/usr/mvp"
PRODUCT_NAME="mvp"
APPLICATION_NAME="Markvision Professional"
OEM=""
LINK_DIR="${OEM}_prt_utils"
DESKTOP_FILE="${PRODUCT_NAME}.desktop"

FOLDER_ICON="${APPLICATION_DIR}/etc/pixmap/icons/lxkapps.png"
APP_ICON="${APPLICATION_DIR}/etc/pixmap/icons/lxkmark.png"
APPLICATION="${APPLICATION_DIR}/bin/${PRODUCT_NAME}"

OLD_KDELNK=MarkVision-Pro.kdelnk

if [ -f /bin/id -a -x /bin/id ];then
   ID_CMD="/bin/id"
else
   ID_CMD="/usr/bin/id"
fi

########################################################
#
#  This function checks for user 'root'. 
#
#######################################################
check_for_root ()
{
   ${ID_CMD} | /bin/grep "uid=0" >/dev/null 2>&1
   if [ "${?}" != "0"  ]; then
      echo "You must be root to run this script."
      exit 1
   fi
}


#######################################################
#
#  This function looks for standard KDE Directories
#  and sets the KDE path or exits if KDE is not found.
#
#######################################################
check_for_kde ()
{
   if   [ -d /opt/kde3/share/applnk ]; then
        #-- SuSE KDE3 directory structure
        KDE_DIR=/opt/kde3/share/applnk
	SUSE_KDE_DIR="/etc/opt/kde3/share/applnk/SuSE"
        CATEGORY="Categories=Qt;KDE;Utility;System;X-SuSE-PrintingUtility"
        cat /etc/SuSE-release |grep "VERSION = 9" >/dev/null 2>&1
        if [ $? -eq 0 ];then
           MESSAGE="Utilities / Printing"
        else
           MESSAGE="${OEM} Printer Utilities"
        fi
   elif [ -d /opt/kde2/share/applnk ]; then
        #-- SuSE KDE2 directory structure
        KDE_DIR=/opt/kde2/share/applnk
	SUSE_KDE_DIR="/etc/opt/kde2/share/applnk/SuSE"
        CATEGORY="Categories=Application;System;X-Red-Hat-Base;"
        MESSAGE="${OEM} Printer Utilities"
   elif [ -d /usr/share/applnk ];then
        #-- RedHat < 8.0 KDE directory structure
        KDE_DIR=/usr/share/applnk
        CATEGORY="Categories=Application;System;X-Red-Hat-Base;"
        MESSAGE="${OEM} Printer Utilities"
   elif [ -d /usr/share/applnk-redhat ];then
        #-- RedHat 8.0 KDE directory structure
        KDE_DIR=/usr/share/applnk-redhat
        CATEGORY="Categories=Application;System;X-Red-Hat-Base;"
        MESSAGE="${OEM} Printer Utilities"
   else
 	#-- KDE Directories not found, exit
        echo
        echo "Error:  KDE Menu directories were not found."
        echo "        Either KDE is not installed or KDE was installed"
        echo "        in a non standard directory." 
        echo
        exit 1
   fi  
}


#######################################################
#
#  This function displays the KDE Utility Menu.
#
#######################################################
display_menu ()
{
   continue="no"

   echo
   echo "${OEM} KDE Menu Utility"
   echo 
   echo "  1. Install ${OEM} applications into the KDE Menu."
   echo "  2. Remove ${OEM} applications from the KDE Menu."
   echo "  3. Exit."
   echo
   printf "Enter your choice : [3] "
   while [ "${continue}" = "no" ]
   do
       read ans
       if   [ "${ans}" = "1" -o "${ans}" = "2" -o "${ans}" = "3" ];then 
            continue="yes"
       elif [ "${ans}" = "" ];then
            continue="yes"
       else
            echo 
            printf "Invalid choice. Enter your choice : "
       fi
   done

   if   [ "${ans}" = "1" ];then
        run_type=install
   elif [ "${ans}" = "2" ];then
        run_type=remove
   else
        echo
        exit 0
   fi
}

create_desktop_file ()
{

cat << EOT
[Desktop Entry]
Encoding=UTF-8
Name=${APPLICATION_NAME}

Icon=${APP_ICON}
Type=Application
Exec=${APPLICATION} -G
Terminal=0
X-Desktop-File-Install-Version=0.3
EOT
echo ${CATEGORY}

}

create_directory_entry ()
{

cat << EOT
[KDE Desktop Entry]
Name=${OEM} Printer Utilities
Comment=Printer Related Software
Icon=${FOLDER_ICON}
Type=Directory
Encoding=UTF-8
EOT

}

remove_old_links()
{
   if   [ -d /opt/kde/share/applnk ]; then
        # SuSE and Caldera Install Dir
        SYS_KDEDIR=/opt/kde/share/applnk/lxk_printer_utilities
	LAPP_FILE=${SYS_KDEDIR}/${OLD_KDELNK}
        SYS_KDEICONDIR=/opt/kde/share/icons/lxkapps.xpm
   elif [ -d  /usr/share/applnk ]; then
        # RedHat and TurboLinux Install Dir
        SYS_KDEDIR=/usr/share/applnk/lxk_printer_utilities
	LAPP_FILE=${SYS_KDEDIR}/${OLD_KDELNK}
        SYS_KDEICONDIR=/usr/share/icons/lxkapps.xpm
   else
        # Default to Standard Directory
        SYS_KDEDIR=/opt/kde/share/applnk/lxk_printer_utilities
	LAPP_FILE=${SYS_KDEDIR}/${OLD_KDELNK}
        SYS_KDEICONDIR=/opt/kde/share/icons/lxkapps.xpm
   fi

   #-- Check for KDE menu directory
   if [ ! -d "${SYS_KDEDIR}" -o ! -f "${LAPP_FILE}" ];then
      return
   fi
   desktop_files=`/bin/ls -1 ${SYS_KDEDIR} | egrep ".desktop|.kdelnk"`
   if [ "${?}" != "0" ];then
      #-- No desktop files found, just remove directory.
       /bin/rm -f ${SYS_KDEDIR}/.directory
       /bin/rmdir ${SYS_KDEDIR}
   else
      printf "${desktop_files}\n" | wc -l | egrep -e "^ *1 *$" >/dev/null 2>&1
      if [ "${?}" = "0" ];then
          /bin/rm -f ${LAPP_FILE}
          /bin/rm -f ${SYS_KDEDIR}/.directory
          /bin/rmdir ${SYS_KDEDIR}
         return
      else
         /bin/rm -f ${LAPP_FILE}
         return
      fi
   fi
   return
}

#-- SCRIPT STARTS HERE --#

#-- Check to see if the current user is root.
check_for_root

#-- Check to see if we can find KDE. 
check_for_kde

#-- Note: KDE_DIR is set in the check_for_kde function
#-- Note: KDE_DIR is set in the check_for_kde function
MENU_DIR="${KDE_DIR}/${LINK_DIR}"
APP_FILE="${MENU_DIR}/${DESKTOP_FILE}"

#-- Display KDE Utility Menu 
display_menu

if [ "${run_type}" = "install" ];then
   remove_old_links
   #-- Create Menu directory if it does not exist.
   if [ ! -d "${MENU_DIR}" ];then
      mkdir ${MENU_DIR}
      if [ "${?}" != "0" ];then
         echo
         echo "Error: KDE Menu directory could not be created."
         echo "       Directory: ${MENU_DIR}"
         exit 1
      fi
   fi

   #-- Update the Menu Directory File.
   if [ -f "${MENU_DIR}/.directory" ];then
      rm -f ${MENU_DIR}/.directory
      if [ "${?}" != "0" ];then
         echo
         echo "Error: KDE Menu directory file could not be removed."
         echo "       File: ${MENU_DIR}/.directory"
         exit 1
      fi
   fi 
   create_directory_entry > ${MENU_DIR}/.directory
   if [ "${?}" != "0" ];then
      echo
      echo "Error: KDE Menu directory file could not be created."
      echo "       File: ${MENU_DIR}/.directory"
      exit 1
   fi
   
   #--Create or Update application link 
   if [ -f "${APP_FILE}" ];then
      rm -f ${APP_FILE}
      if [ "${?}" != "0" ];then
         echo
         echo "Error: KDE Menu option could not be removed."
         echo "       File: ${APP_FILE}"
         exit 1
      fi
   fi
   create_desktop_file > ${APP_FILE}
   if [ "${?}" != "0" ];then
      echo
      echo "Error: KDE Menu option could not be created."
      echo "       File: ${APP_FILE}"
      exit 1
   fi

   if [ -d "${SUSE_KDE_DIR}" ]; then
        cp -rp ${MENU_DIR} ${SUSE_KDE_DIR}
   fi
   echo
   echo "Adding KDE MENU [ ${MESSAGE} ] was successful."
   echo "You may need to restart KDE for changes to take effect."
   exit 0
else
   #-- Remove KDE application links.
   remove_old_links

   #-- Check for KDE menu directory
   if [ ! -d "${MENU_DIR}" -o ! -f "${APP_FILE}" ];then
      echo "${OEM} KDE Menus are not installed."
      exit 1
   fi
   desktop_files=`/bin/ls -1 ${MENU_DIR} | grep ".desktop"`
   if [ "${?}" != "0" ];then 
      #-- No desktop files found, just remove directory.
      /bin/rm -f ${MENU_DIR}/.directory
      /bin/rmdir ${MENU_DIR}
   else
      printf "${desktop_files}\n" | wc -l | egrep -e "^ *1 *$" >/dev/null 2>&1
      if [ "${?}" = "0" ];then
         /bin/rm -f ${APP_FILE}
         /bin/rm -f ${MENU_DIR}/.directory
         /bin/rmdir ${MENU_DIR}
		 if [ -d "${SUSE_KDE_DIR}/${LINK_DIR}" ]; then
           /bin/rm -rf "${SUSE_KDE_DIR}/${LINK_DIR}"
         fi
         echo "Removing KDE Menu [ ${MESSAGE} ] was successful."
         exit 0
      else
         /bin/rm -f ${APP_FILE}
         echo "Removing KDE Menu Link was successful."
         exit 0
      fi
   fi
fi

