All pastes #914026 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-02-22 15:47 UTC
rendered paste body
#!/bin/bash
#
# Search .desktop files and extract information about program entries
# Use the information to write the menu file for JWM

if [ "$1" != "now" ];then
  # Check for X
  if [[ -z $DISPLAY ]];then
    echo "This will search for new programs and add them to the JWM menu file."
    echo "A copy of your existing ~/.jwm/menu file will be saved as ~/.jwm/menu-old"
    echo "Do you want to continue?  y/N"
    read ans    
    case $ans in
      Y|y) ;;
      [Yy][Ee][Ss]) ;;
      *)
        echo Aborting 
        exit
        esac
  else
    Xdialog --wrap --title "Refresh JWM menu?"\
     --yesno "This will search for new programs and add them to the JWM menu file.
     A copy of your existing ~/.jwm/menu file will be saved as ~/.jwm/menu-old
     Do you want to continue?" 0 0
    case $? in
      0)
    echo "Yes chosen."
        ;;
      1)
    echo "No chosen, exiting."
    exit
        ;;
      255)
    echo "Box closed, exiting"
    exit
        ;;
    esac
  fi
fi

### add symlinks for icon catagories
ln -s /usr/share/icons/Vista-Inspirate/scalable/apps/kdf.png /usr/share/icons/Vista-Inspirate/scalable/apps/other.png
ln -s /usr/share/icons/vasm.png /usr/share/icons/Vista-Inspirate/scalable/apps/system.png
ln -s /usr/share/icons/Vista-Inspirate/scalable/apps/advancedsettings.png /usr/share/icons/Vista-Inspirate/scalable/apps/utilities.png
ln -s /usr/share/icons/Vista-Inspirate/scalable/apps/ooo_draw.png /usr/share/icons/Vista-Inspirate/scalable/apps/graphics.png
ln -s /usr/share/icons/Vista-Inspirate/scalable/apps/ooo_writer.png /usr/share/icons/Vista-Inspirate/scalable/apps/office.png

# Make a backup of the original configuration file
cp ~/.jwm/menu ~/.jwm/menu-old

# Start looking for new applications
menufile="$HOME/.jwm/menu"
ls /usr/share/applications/*.desktop|while read file;do
  echo ==
  exec="$(cat "$file" | grep -i ^[Ee][Xx][Ee][Cc]= | sed s/[Ee][Xx][Ee][Cc]=//)"
  echo "Executable: $exec"
  if [ "$(echo $exec|cut -d " " -f1)" = "vsuper" ] || [ "$(echo $exec|cut -d " " -f1)" = "/sbin/vsuper" ];then
    exe="$(echo $exec|cut -d " " -f2)"
  else
    exe="$(echo $exec|cut -d " " -f1)"
  fi
  echo "$file:";
  name="$(cat "$file" | grep -i ^[Nn][Aa][Mm][Ee]= | sed s/[Nn][Aa][Mm][Ee]=// | sed 's/[ \t]*$//')"
  if [ -z "$name" ];then
    echo "No name found, Using $exe for name"
    name="$exe"
    echo "Name: $name"
  else
    echo "Name: $name"
  fi
  if [ -x $EXE ];then ##if its not executable no sense adding it.
    echo "$exec is Executable"
    ### This section finds/adds the category
    # The categories are: multimedia office graphics utilities network system games. Anything else goes in "other"
    category=$(cat "$file" | grep -i categories | grep [=\;][Mm][Uu][Ll][Tt][Ii][Mm][Ee][Dd][Di][Aa] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Aa][Uu][Dd][Ii][Oo] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Vv][Ii][Dd][Ee][Oo] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ "$category" = "audio" ] || [ "$category" = "video" ];then
      category=multimedia
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Oo][Ff][Ff][Ii][Cc][Ee] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Gg][Rr][Aa][Pp][Hh][Ii][Cc][Ss] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Uu][Tt][Ii][Ll][Ii][Tt][Ii][Ee][Ss] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Uu][Tt][Ii][Ll][Ii][Tt][Yy] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ "$category" = "utility" ];then
      category=utilities
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Nn][Ee][Tt][Ww][Oo][Rr][Kk] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Ii][Nn][Tt][Ee][Rr][Nn][Ee][Tt] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ "$category" = "internet" ];then
      category=network
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Ss][Yy][Ss][Tt][Ee][Mm] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Gg][Aa][Mm][Ee][Ss] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories | grep [=\;][Gg][Aa][Mm][Ee] -o | tr [:upper:] [:lower:] | sed 's/;//' | sed 's/=//')
    fi
    if [ "$category" = "game" ];then
      category=games
    fi
    if [ -z "$category" ];then
      category=$(cat "$file" | grep -i categories)
    fi
    if [ "$category" != "multimedia" ] && [ "$category" != "office" ] && [ "$category" != "graphics" ] && [ "$category" != "utilities" ] && [ "$category" != "network" ] && [ "$category" != "system" ] && [ "$category" != "games" ];then 
      category=other
    fi
      
    ### this capitalizes the category $Category = Capitalized $category = not capitalized
    #Since menu is captialized, make it so
    first=$(echo ${category:0:1} | tr 'a-z' 'A-Z')
    rest=$(echo ${category:1} | tr 'A-Z' 'a-z')
    echo $first$rest
    Category="$first$rest"
    echo Category: $category $Category
      
    ### find/set an icon for the app
    # Unless full icon path is specified, search for for a matching jpg, xpm or png file. 
    #If nothing found, use a default one.
    icon=$(cat "$file" | grep -i ^[Ii][Cc][Oo][Nn]= | sed 's/[Ii][Cc][Oo][Nn]=//')
    useicon=$icon
    if [ -z `echo $icon | grep \/` ];then
      useicon=
      themepath="/usr/share/icons/Vista-Inspirate/scalable"
      for icondir in "/usr/share/pixmaps" "$themepath/mimetypes" "$themepath/devices/" "$themepath/filesystems" "$themepath/apps";do
    cd $icondir
    if [ -z $(echo $icon | grep png) ] && [ -z $(echo $icon | grep jpg) ] && [ -z $(echo $icon | grep xpm) ];then
      for icontype in jpg xpm png;do
        iconfound=$(find -name "$icon.$icontype" | sed 's/.\//\//')
        if [ -n "$iconfound" ];then
          useicon=$icondir$iconfound
        fi
      done
    else 
      iconfound=$(find -name "$icon" | sed 's/.\//\//')
      if [ -n "$iconfound" ];then
        useicon=$icondir$iconfound
      fi
    fi
      done
    fi
    if [ -z "$useicon" ];then
      echo "No icon files found, using default"
      useicon="/usr/share/icons/Vista-Inspirate/scalable/apps/tux.png"
    fi
    echo "Icon: $useicon"

    ### add the app if its not in the menu
    # Search for application name and category in menu file,
    # if not there, insert appropriate category section and/or a program entry
    searchname=$(echo $name | sed 's/[ \t]*$//'| sed 's/ /\\\ /g')
    checkname=$(cat $menufile | grep "$exe")
    if [ -z "$checkname" ];then
      echo "Adding $exec"
      findcat=\<Menu\ label=\"\ $Category\"
      checkcat=`cat $menufile | grep "$findcat"`
      if [ -z "$checkcat" ];then
    menulabel=\ \ \ \ \<Menu\ label=\"\ $Category\"\ icon=\"/usr/share/icons/Vista-Inspirate/scalable/apps/$category.png\"\>
        sed -i "/<JWM>/ a\ $menulabel" $menufile
    sed -i "/$findcat/ a\ \ \ \ </Menu>" $menufile
      fi
      addprog=\ \ \ \ \ \ \<Program\ label=\"\ $name\"\ icon=\"$useicon\"\>$exec\<\/Program\>
      sed -i "/$findcat/ a\ $addprog" $menufile
    fi
  fi
done