#!/bin/sh
###############################################################################
# generates a file.menu format for Enlightenment out of a GNOME 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 [gnome_app_base_dir]"
  exit
fi
# setup variables
BASE=$1
ODIR=$2
OUT=$3


# NEW
find_app_base_dir() {
  ADIR=`gnome-config --prefix`"/share/gnome/apps"
  if [ ! -d "$ADIR" ]; then
    ADIR=/usr/share/gnome/apps
  fi
  if [ ! -d "$ADIR" ]; then
    ADIR=/usr/share/apps
  fi
  if [ ! -d "$ADIR" ]; then
    ADIR=/usr/local/share/apps
  fi
  if [ ! -d "$ADIR" ]; then
    ADIR=/usr/gnome/share/apps
  fi
  if [ ! -d "$ADIR" ]; then
    ADIR=/usr/local/gnome/share/apps
  fi
  if [ ! -d "$ADIR" ]; then
    ADIR=/opt/gnome/share/apps
  fi
  if [ ! -d "$ADIR" ]; then
    ADIR=/opt/gnome/share/gnome/apps
  fi
  if [ ! -d "$ADIR" ]; then
    ADIR=/usr/X11R6/share/gnome/apps
  fi
  if [ ! -d "$ADIR" ]; then
    ADIR=/opt/local/share/apps
  fi
}

find_icon_base_dir() {
  IDIR=`gnome-config --prefix`"/share/pixmaps"
  if [ ! -f $IDIR"/gnome-help.png" ]; then
    IDIR=/usr/share/pixmaps
  fi
  if [ ! -f $IDIR"/gnome-help.png" ]; then
    IDIR=/usr/share/gnome/pixmaps
  fi
  if [ ! -f $IDIR"/gnome-help.png" ]; then
    IDIR=/usr/local/share/pixmaps
  fi
  if [ ! -f $IDIR"/gnome-help.png" ]; then
    IDIR=/usr/gnome/share/pixmaps
  fi
  if [ ! -f $IDIR"/gnome-help.png" ]; then
    IDIR=/usr/local/gnome/share/pixmaps
  fi
  if [ ! -f $IDIR"/gnome-help.png" ]; then
    IDIR=/opt/gnome/share/pixmaps
  fi
  if [ ! -f $IDIR"/gnome-help.png" ]; then
    IDIR=/opt/gnome/share/gnome/pixmaps
  fi
  if [ ! -f $IDIR"/gnome-help.png" ]; then
    IDIR=/usr/X11R6/share/gnome/pixmaps
  fi
  if [ ! -f $IDIR"/gnome-help.png" ]; then
    IDIR=/opt/local/share/pixmaps
  fi
}

if [ $# -eq 4 ]; then
  DIR=$4
else
   find_app_base_dir
   DIR=$ADIR
fi

# In most cases I believe the icons can be found in the directory that is
# ../../pixmaps from $ADIR.
find_icon_base_dir
GICONDIR=$IDIR

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

# function to check its a GNOME desktop file 
is_desktop() {
  VAL=`grep "\[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() {
  if [ -f $1"/.order" ]; then
    VAL=`cat $1"/.order" | awk '{printf("%s ", $1);}' | sed "s/,/ /g"`
  else
    VAL=""
  fi
  FILES=`/bin/ls $1 | awk '{printf("%s ", $1);}'`
  VAL2=$VAL
  for I in $FILES; do
    IS_IN="n"
    for J in $VAL; do
      if [ "$J" = "$I" ]; then
        IS_IN="y"
      fi
    done
    if [ "$IS_IN" = "n" ]; then
      VAL2=$VAL2$I" "
    fi
  done
  echo $VAL2
  return 0
}

get_icon() {
  VAL=`grep '^Icon=' $1 | head -1`
  if [ -n "$VAL" ]; then
    VAL2=`echo $VAL | awk -F= '{printf("%s", $2);}'`
    # If we have "convert" and the icon havsn't already been scaled down, scale
    # it down now!

    EICONDIR="$ODIR/gnome_icons"
    if [ ! -d $EICONDIR ];then
      mkdir $EICONDIR
    fi

    # The "Icon" entery can contain an absolute path, if it does forget the
    # $GICONDIR
    VAL3=`echo $VAL2 | grep /`
    if [ "$VAL3" != "$VAL2" ];then
      if [ -n "$VAL2" ]; then
	GICON=$GICONDIR/$VAL2
	EICON=$EICONDIR/$VAL2
      fi
    else
      if [ -n "$VAL3" ]; then
	GICON=$VAL3
	EICON=$EICONDIR/"`basename "$VAL3"`"
      fi
    fi
    CONVERT=`which convert`
    if [ -n "$CONVERT" ]; then
      if [ -n "$GICON" ]; then
        if [ -f "$GICON" ]; then
          if [ ! -f "$EICON" ]; then
            $CONVERT $GICON -geometry 16x16 $EICON
          fi
        fi
      fi
    fi
    echo "$EICON" | tr -d '\n'
    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);}'`
    echo $VAL2 | tr -d '\n'
    return 0
  fi
  return 1
}

E_TITLE="GNOME Menu"

ORDER=""
# if a .directory file exists - read it
F=$DIR"/.directory"
if [ -f $F ]; then
  NAME=`is_desktop $F`
  if [ -n "$NAME" ]; then
    E_TITLE=$NAME
  fi
fi
ORDER=`get_sortorder $DIR`

# 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
    FFF=$FF"/.directory"
    if [ -f $FFF ]; then
      NAME=`is_desktop $FFF`
    else
      NAME=`echo $F | sed "s/_/ /g"`
    fi
    FFF=$ODIR"/"$BASE
    if [ ! -d "$FFF" ]; then
      mkdir $FFF
    fi
    MFILE=$FFF"/"$F.menu
    $0 $BASE"/"$F $ODIR $MFILE $DIR"/"$F
	ICO=`get_icon $FF"/.directory"`
    echo \"$NAME\" \"$ICO\" menu \"$MFILE\" >> $OUT
  else
    if [ -r "$FF" ]; then
      NAME=`is_desktop $FF`
      if [ -n "$NAME" ]; then
	EXE=`get_exec $FF`
	    ICO=`get_icon $FF`
	echo \"$NAME\" \"$ICO\" exec \"$EXE\" >> $OUT
      fi
    fi
  fi
done
