#!/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_GNOMELNK=markvision-pro.desktop

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
      echo "You must be root to run this script."
      exit 1
   fi
}

#######################################################
#
#  This function looks for standard GNOME Directories
#  and sets the GNOME path or exits if GNOME is not found.
#
#######################################################
check_for_gnome ()
{
   if   [ -d /opt/gnome/share/applications ]; then
        #-- SuSE GNOME directory structure
        GNOME_DIR=/opt/gnome/share/applications
        GNOME2="yes"
        MESSAGE="Utilities / Printing"
        CATEGORY="Categories=GNOME;Application;Utility;Utility;X-SuSE-PrintingUtility"
   elif [ -d /etc/opt/gnome/SuSE ]; then
        #-- SuSE GNOME directory structure
        GNOME_DIR=/etc/opt/gnome/SuSE
        GNOME2="no"
        CATEGORY="Categories=Application;System;X-Red-Hat-Base;"
   elif [ -d /opt/gnome/share/gnome/apps ]; then
        #-- SuSE GNOME directory structure
        GNOME_DIR=/opt/gnome/share/gnome/apps
        GNOME2="no"
        CATEGORY="Categories=Application;System;X-Red-Hat-Base;"
   elif [ -d  /usr/share/applications ]; then
        # RedHat 8.0 GNOME directory structure
        GNOME_DIR=/usr/share/applications
        GNOME2="yes"
        MESSAGE="System Tools"
        CATEGORY="Categories=Application;System;X-Red-Hat-Base;"
   elif [ -d  /usr/share/gnome/apps ]; then
        # RedHat < 8.0 GNOME directory structure
        GNOME_DIR=/usr/share/gnome/apps
        GNOME2="no"
        CATEGORY="Categories=Application;System;X-Red-Hat-Base;"
   else
        #-- GNOME Directories not found, exit
	echo
        echo "Error:  GNOME Menu directories were not found."
        echo "        Either GNOME is not installed or GNOME was installed"
        echo "        in a non standard directory."
        echo

        exit 1
   fi
}

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

   echo
   echo "${OEM} GNOME Menu Utility"
   echo 
   echo "  1. Install ${OEM} applications into the GNOME Menu."
   echo "  2. Remove ${OEM} applications from the GNOME 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
	remove_old_links
   elif [ "${ans}" = "2" ];then
        run_type=remove
	remove_old_links
   else
        echo
        exit 0
   fi
}

create_desktop_file ()
{

cat << EOT
[Desktop Entry]
Name=${APPLICATION_NAME}

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

}

create_directory_entry ()
{

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

}

remove_old_links ()
{
   if   [ -d /etc/opt/gnome/SuSE ]; then
        #-- SuSE GNOME directory structure
        SYS_GNOMEDIR=/etc/opt/gnome/SuSE/lxk_printer_utilities
        LAPP_FILE=${SYS_GNOMEDIR}/${OLD_GNOMELNK}
   elif   [ -d /opt/gnome/share/gnome/apps ]; then
        # SuSE and Caldera Install Dir
        SYS_GNOMEDIR=/opt/gnome/share/gnome/apps/lxk_printer_utilities
        LAPP_FILE=${SYS_GNOMEDIR}/${OLD_GNOMELNK}
   elif [ -d  /usr/share/gnome/apps ]; then
        # TurboLinux and RedHat Install Dir
        SYS_GNOMEDIR=/usr/share/gnome/apps/lxk_printer_utilities
        LAPP_FILE=${SYS_GNOMEDIR}/${OLD_GNOMELNK}
   else
        # Default to Standard Directory
        SYS_GNOMEDIR=/opt/gnome/share/gnome/apps/lxk_printer_utilities
        LAPP_FILE=${SYS_GNOMEDIR}/${OLD_GNOMELNK}
   fi

#-- Remove GNOME application links.

   if [ ! -d "${SYS_GNOMEDIR}" -o ! -f "${LAPP_FILE}" ];then
      return
   fi

   desktop_files=`/bin/ls -1 ${SYS_GNOMEDIR} | grep .desktop`
   if [ "${?}" != "0" ];then
      #-- No desktop files found, just remove directory.
      /bin/rm -f ${SYS_GNOMEDIR}/.directory
      /bin/rmdir ${SYS_GNOMEDIR}
   else
        if [ -f "${SUSE_GNOME_DIR}/${DESKTOP_FILE}" ];then
          /bin/rm -f "${SUSE_GNOME_DIR}/${DESKTOP_FILE}"
	fi
   fi
      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_GNOMEDIR}/.directory
          /bin/rmdir ${SYS_GNOMEDIR}
      else
          /bin/rm -f ${LAPP_FILE}
      fi
}

#-- SCRIPT STARTS HERE --#

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

#-- Check to see if we can find GNOME. 
check_for_gnome

#-- Note: GNOME_DIR is set in the check_for_gnome function
MENU_DIR="${GNOME_DIR}/${LINK_DIR}"
APP_FILE="${MENU_DIR}/${DESKTOP_FILE}"
GNOME2_APP_FILE="${GNOME_DIR}/${DESKTOP_FILE}"

#-- Display GNOME Utility Menu 
display_menu

if [ "${run_type}" = "install" ];then
   #-- Create Menu directory if it does not exist.
   if [ "${GNOME2}" = "yes" ];then
      if [ -f "${GNOME2_APP_FILE}" ];then
         rm -f ${GNOME2_APP_FILE}
         if [ "${?}" != "0" ];then
            echo
	    echo "Error: GNOME Menu option could not be removed."
            echo "       File: ${GNOME2_APP_FILE}"
            exit 1
         fi
      fi
      create_desktop_file > ${GNOME2_APP_FILE}
      if [ "${?}" != "0" ];then
         echo
	 echo "Error: GNOME Menu option could not be created."
         echo "       File: ${GNOME2_APP_FILE}"
         exit 1

      else
         echo
         echo "GNOME's Menu [ ${MESSAGE} ] has been updated."
         echo "You may need to restart GNOME for changes to take effect."
         exit 0
      fi
   fi


   #-- None RedHat 8 System 
   if [ ! -d "${MENU_DIR}" ];then
      mkdir ${MENU_DIR}
      if [ "${?}" != "0" ];then
         echo
	 echo "Error: GNOME 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: GNOME 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: GNOME 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: GNOME 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: GNOME Menu option could not be created."
      echo "       File: ${APP_FILE}"
      exit 1
   fi
		if [ -d "${SUSE_GNOME_DIR}" ];then
          cp ${APP_FILE} ${SUSE_GNOME_DIR}
        fi
   echo
   echo "Adding GNOME Menu Link was successful."
   echo "You may need to restart GNOME for changes to take effect."
   exit 0
else
   #-- Remove GNOME application links.

   if [ "${GNOME2}" = "yes" ];then 
      #-- Check for GNOME application link
      if [ -f "${GNOME2_APP_FILE}" ];then
         /bin/rm -f ${GNOME2_APP_FILE}
	 echo "Removing GNOME Menu Link was successful."
         exit 0
      else
         echo "${OEM} GNOME Menus are not installed."
         exit 1
      fi
   fi

   #-- Check for GNOME menu directory
   if [ ! -d "${MENU_DIR}" -o ! -f "${APP_FILE}" ];then
      echo "${OEM} GNOME 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 
	if [ -f "${SUSE_GNOME_DIR}/${DESKTOP_FILE}" ];then
          /bin/rm -f "${SUSE_GNOME_DIR}/${DESKTOP_FILE}"
         echo "Removing GNOME Application Link was successful."
    fi 
      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}
	 echo "Removing GNOME Menu Link was successful."
      else
         /bin/rm -f ${APP_FILE}
         echo "Removing GNOME Application Link was successful."
      fi
   fi
fi

exit 0
