#!/bin/sh
##############################################################################
# generates a file.menu format for Enlightenment out of a KDE menu hierarchy #
#
# Copyright (C) 1999 Carsten Haitzler, Geoff Harrison and various contributors
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies of the Software, its documentation and marketing & publicity
# materials, and acknowledgment shall be given in the documentation, materials
# and software packages that this Software was used.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##############################################################################

# if we dont have enough arguments
if [ $# -lt 3 ]; then
  echo "usage:"
  echo "    "$0" base_name output_dir output_file.menu [kde_applnk_base_dir]"
  exit
fi
# setup variables
BASE="$1"
ODIR="$2"
OUT="$3"

set_IDIR() {
  # hunt around and see where all the kde graphics have gone
  IDIR=/usr/share/icons
  if [ ! -r "${IDIR}/mini/kcontrol.xpm" ]; then
    IDIR="/usr/local/share/icons"
  else
    return
  fi
  if [ ! -r "${IDIR}/mini/kcontrol.xpm" ]; then
    IDIR="/opt/kde/icons"
  else
    return
  fi
  if [ ! -r "${IDIR}/mini/kcontrol.xpm" ]; then
    IDIR="/opt/kde/share/icons"
  else
    return
  fi
  if [ ! -r "${IDIR}/mini/kcontrol.xpm" ]; then
    IDIR="/usr/local/kde/icons"
  else
    return
  fi
  if [ ! -r "${IDIR}/mini/kcontrol.xpm" ]; then
    IDIR="/usr/local/kde/share/icons"
  else
    return
  fi
  if [ ! -r "${IDIR}/mini/kcontrol.xpm" ]; then
    IDIR="/usr/kde/icons"
  else
    return
  fi
  if [ ! -r "${IDIR}/mini/kcontrol.xpm" ]; then
    IDIR="/usr/kde/share/icons"
  else
    return
  fi
  if [ ! -r "${IDIR}/mini/kcontrol.xpm" ]; then
    IDIR="/usr/share/kde/icons"
  else
    return
  fi
}

set_DIR (){
  DIR=/usr/share/applnk
  if [ ! -r "${DIR}//KControl.kdelnk" ]; then
    DIR="/usr/local/share/applnk"
  else
    return
  fi
  if [ ! -r "${DIR}//KControl.kdelnk" ]; then
    DIR="/opt/kde/applnk"
  else
    return
  fi
  if [ ! -r "${DIR}//KControl.kdelnk" ]; then
    DIR="/opt/kde/share/applnk"
  else
    return
  fi
  if [ ! -r "${DIR}//KControl.kdelnk" ]; then
    DIR="/usr/local/kde/applnk"
  else
    return
  fi
  if [ ! -r "${DIR}//KControl.kdelnk" ]; then
    DIR="/usr/local/kde/share/applnk"
  else
    return
  fi
  if [ ! -r "${DIR}//KControl.kdelnk" ]; then
    DIR="/usr/kde/applnk"
  else
    return
  fi
  if [ ! -r "${DIR}//KControl.kdelnk" ]; then
    DIR="/usr/kde/share/applnk"
  else
    return
  fi
  if [ ! -r "${DIR}//KControl.kdelnk" ]; then
    DIR="/usr/share/kde/applnk"
  else
    return
  fi
}


set_IDIR
# mini icons  are in the icons dir
MIDIR=$IDIR"/mini"

# hunt around and see where all the kde applnk stuff is
if [ $# -eq 4 ]; then
  DIR="$4"
else
  set_DIR
fi


# if the applnk dir doesn't exist in the end then exit
if [ ! -d "$DIR" ]; then
  exit
fi
# if the destination dir doesn't exist - create it
if [ ! -d "$ODIR" ]; then
  mkdir "$ODIR"
fi

# function to check its a KDE kdelnk file 
is_kdelnk() {
  VAL="`grep "\[KDE Desktop Entry\]" $1`"
  IS_KDELNK_RETURN=""
  if [ -n "$VAL" ]; then
    IS_KDELNK_RETURN="`awk -F= 'BEGIN { n="" } END { printf("%s", n) } $1 ~ "^Name\\\['$LANG'\\\]" {n=$2;exit} $1 ~ "^Name$" {n=$2}' $1`"
    if [ -n "$IS_KDELNK_RETURN" ]; then
      return 0
    fi
  fi
  return 1
}

# function to get the sortorder list -if there is one
get_sortorder() {
  GET_SORTORDER_RETURN="`awk -F= '$1 ~ "^SortOrder$" {printf("%s", $2);}' $1 | sed 's/,/ /g'`"
  if [ -z "$GET_SORTORDER_RETURN" ]; then
    return 1
  fi
  return 0
}

get_icon() {
  GET_ICON_RETURN="`awk -F= '$1 ~ "^Icon$" {printf("%s", $2); exit;}' $1`"
  if [ -z "$GET_ICON_RETURN" ]; then
    return 1
  fi
  return 0
}

get_miniicon() {
  GET_MINIICON_RETURN="`awk -F= '$1 ~ "^MiniIcon$" {printf("%s", $2); exit;}' $1`"
  if [ -z "$GET_MINIICON_RETURN" ]; then
    if ! get_icon $1; then
      return 1
    fi
    GET_MINIICON_RETURN=$GET_ICON_RETURN
  fi
  return 0
}

get_exec() {
  GET_EXEC_RETURN=`awk -F= '$1 ~ "^Exec$" {printf("%s", $2); exit;}' $1 | \
                     sed -e "s:\%c:$2:g"          -e "s:\%m:$3:g" \
		         -e "s:\%i:$4:g"          -e "s:\":\':g" \
			 -e "s:\%(u|k|n):NONE:g"  -e "s:\%f:blank:g" \
                         -e "s:\%d:$HOME:g"`
  if [ -z "$GET_EXEC_RETURN" ]; then
    return 1
  fi
  return 0
}

E_TITLE="$DIR Menu"

# order of the directory
DIRORDER=""
# if a .directory file exists - read it
F="${DIR}/.directory"
if [ -f $F ]; then
  is_kdelnk "$F"
  NAME="$IS_KDELNK_RETURN"
  if [ -n "$NAME" ]; then
    E_TITLE="$NAME"
  fi
  get_sortorder "$F"
  DIRORDER="$GET_SORTORDER_RETURN"
fi

# this is to build a complete orderered list
# from DIRORDER and all the sub-directories
# because DIRORDER is often not complete
build_complete_order() {
 DIRLIST=`/bin/ls -F $DIR | grep "/$" | sed "s:/::g"`
 BUILD_COMPLETE_ORDER_RETURN=""
 if [ -z "$DIRORDER" ]; then
   BUILD_COMPLETE_ORDER_RETURN="$DIRLIST"
 else
  BUILD_COMPLETE_ORDER_RETURN="$DIRORDER"
  for subdir in $DIRLIST; do
   # if not in DIRLIST
   pos="`echo $DIRORDER | egrep "(^$subdir | $subdir | $subdir$)"`"
   # case ^$subdir$ not possible because more than one sub-directory
   if [ -z "$pos" ]; then
    BUILD_COMPLETE_ORDER="${BUILD_COMPLETE_ORDER_RETURN} ${subdir}"
   fi
  done
 fi

 return 0
}

build_complete_order
ORDER="$BUILD_COMPLETE_ORDER_RETURN"

# print the menu title
echo "\"$E_TITLE\"" > $OUT

# for every subdir in the dir list or order - print it
for F in $ORDER; do
  FF="${DIR}/${F}"
  if [ -d "$FF" ]; then
    NAME="`echo "$F" | sed "s/_/ /g"`"
    FFF="${FF}/.directory"
    if [ -f "$FFF" ]; then
      get_icon "$FFF"
      ICON="$GET_ICON_RETURN"
    else
      ICON=""
    fi
    if [ -n "$ICON" ]; then
      ICON="\"${MIDIR}/${ICON}\""
    else
      ICON="NULL"
    fi
    FFF="${ODIR}/${BASE}"
    if [ ! -d "$FFF" ]; then
      mkdir "$FFF"
    fi
    MFILE="${FFF}/${F}.menu"
    "$0" "${BASE}/${F}" "$ODIR" "$MFILE" "${DIR}/${F}"
    echo "\"$NAME\" $ICON menu \"$MFILE\"" >> $OUT
  fi
done

# for all normal files...
FILES="`/bin/ls -F $DIR | grep -v "/$"`"
for F in $FILES; do 
  FF="${DIR}/${F}"
  if [ -f "$FF" ]; then
    is_kdelnk "$FF"
    NAME="$IS_KDELNK_RETURN"
    if [ -n "$NAME" ]; then
      get_miniicon "$FF"
      ICON="$GET_MINIICON_RETURN"
      get_icon "$FF"
      BICON="$GET_ICON_RETURN"
      if [ -n "$ICON" ]; then
	ICON="\"${MIDIR}/${ICON}\""
      else
	ICON="NULL"
      fi
      get_exec "$FF" "$NAME" "$ICON $BICON"
      EXE="$GET_EXEC_RETURN"
      echo "\"$NAME\" $ICON exec \"$EXE\"" >> $OUT
    fi
  fi
done
