#!/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

# 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
fi
if [ ! -r $IDIR"/mini/kcontrol.xpm" ]; then
  IDIR=/opt/kde/icons
fi
if [ ! -r $IDIR"/mini/kcontrol.xpm" ]; then
  IDIR=/opt/kde/share/icons
fi
if [ ! -r $IDIR"/mini/kcontrol.xpm" ]; then
  IDIR=/usr/local/kde/icons
fi
if [ ! -r $IDIR"/mini/kcontrol.xpm" ]; then
  IDIR=/usr/local/kde/share/icons
fi
if [ ! -r $IDIR"/mini/kcontrol.xpm" ]; then
  IDIR=/usr/kde/icons
fi
if [ ! -r $IDIR"/mini/kcontrol.xpm" ]; then
  IDIR=/usr/kde/share/icons
fi
if [ ! -r $IDIR"/mini/kcontrol.xpm" ]; then
  IDIR=/usr/share/kde/icons
fi
# 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
  DIR=/usr/share/applnk
  if [ ! -r $DIR"/KControl.kdelnk" ]; then
    DIR=/usr/local/share/applnk
  fi
  if [ ! -r $DIR"/KControl.kdelnk" ]; then
    DIR=/opt/kde/applnk
  fi
  if [ ! -r $DIR"/KControl.kdelnk" ]; then
    DIR=/opt/kde/share/applnk
  fi
  if [ ! -r $DIR"/KControl.kdelnk" ]; then
    DIR=/usr/local/kde/applnk
  fi
  if [ ! -r $DIR"/KControl.kdelnk" ]; then
    DIR=/usr/local/kde/share/applnk
  fi
  if [ ! -r $DIR"/KControl.kdelnk" ]; then
    DIR=/usr/kde/applnk
  fi
  if [ ! -r $DIR"/KControl.kdelnk" ]; then
    DIR=/usr/kde/share/applnk
  fi
  if [ ! -r $DIR"/KControl.kdelnk" ]; then
    DIR=/usr/share/kde/applnk
  fi
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`
  if [ -n "$VAL" ]; then
    VAL=`grep "^Name\[$LANG\]=" $1 | head -1`
    if [ ! -n "$VAL" ]; then
      VAL=`grep '^Name=' $1 | head -1`
    fi
    if [ -n "$VAL" ]; then
      NAME=`echo $VAL | awk -F= '{printf("%s", $2);}'`
      echo $NAME | tr -d '\n'
      return 0
    fi
  fi
  return 1
}

# function to get the sortorder list -if there is one
get_sortorder() {
  VAL=`grep '^SortOrder=' $1 | head -1`
  if [ -n "$VAL" ]; then
    VAL2=`echo $VAL | awk -F= '{printf("%s", $2);}' | sed "s/,/ /g"`
    echo $VAL2 | tr -d '\n'
    return 0
  fi
  return 1
}

get_icon() {
  VAL=`grep '^Icon=' $1 | head -1`
  if [ -n "$VAL" ]; then
    VAL2=`echo $VAL | awk -F= '{printf("%s", $2);}'`
    echo $VAL2 | tr -d '\n'
    return 0
  fi
  return 1
}

get_miniicon() {
  VAL=`grep '^MiniIcon=' $1 | head -1`
  if [ -n "$VAL" ]; then
    VAL2=`echo $VAL | awk -F= '{printf("%s", $2);}'`
    echo $VAL2 | tr -d '\n'
    return 0
  else
    get_icon $1
    return 0
  fi
  return 1
}

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

E_TITLE="$DIR Menu"

# order of the directory
DIRORDER=""
# if a .directory file exists - read it
F=$DIR"/.directory"
if [ -f $F ]; then
  NAME=`is_kdelnk $F`
  if [ -n "$NAME" ]; then
    E_TITLE=$NAME
  fi
  DIRORDER=`get_sortorder $F`
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"`
 if [ -z "$DIRORDER" ]; then
  echo $DIRLIST
 else
  echo $DIRORDER
  for subdir in $DIRLIST; do
   # if not in DIRLIST
   pos1=`echo $DIRORDER | grep "^$subdir "`
   pos2=`echo $DIRORDER | grep " $subdir "`
   pos3=`echo $DIRORDER | grep " $subdir$"`
   # case ^$subdir$ not possible because more than one sub-directory
   if [ -z "$pos2" -a -z "$pos1" -a -z "$pos3" ]; then
    echo $subdir
   fi
  done
 fi
}

ORDER=`build_complete_order`

# 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
      ICON=`get_icon "$FFF"`
    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
    NAME=`is_kdelnk $FF`
    if [ -n "$NAME" ]; then
      ICON=`get_miniicon "$FF"`
      BICON=`get_icon $FF`
      if [ -n "$ICON" ]; then
	ICON=\"$MIDIR"/"$ICON\"
      else
	ICON=NULL
      fi
      EXE=`get_exec $FF $NAME $ICON $BICON`
      echo \"$NAME\" $ICON exec \"$EXE\" >> $OUT
    fi
  fi
done
