Submitted by Anonymous on Sat, 08/10/2002 - 08:59. General Questions
Hi all,
I wrote a shell script that finds and imports all Gnome`s menu links, converts them, and puts them into the .mwmrc (for the root menu post).
Anyone want like that kind of thing?
Just wondering,
John Hendrickson
#!/bin/sh
# --------------------------------------------------------------------
# Automagically import GNOME menu into a folder of mwm`s desktop menu.
# --------------------------------------------------------------------
# f1-f4 makes a directory structure with new menus then a file with all menus.
# f5,f6 updates .mwmrc
# --------------------------------------------------------------------
function echo_v () { if [ $DEBUG -gt 0 ]; then echo "$*" >> /dev/stderr ; fi ; }
# --------------------------------------------------------------------
# 1. Make a directory for each folder. A list holds all entries found.
function f1 () {
cd $HOME
# See below for why WRAP and IFS solve Long Filename woes in sh scripts.
for file in $(locate "*.desktop"); do
DESC="$(head "$file" | sed -n -e `/^Name=/{s/^Name=//;p;};`)"
TEXEC="$(tail "$file" | sed -n -e `/^TryExec=/{s/^TryExec=//;p;};`)"
EXEC="$(tail "$file" | sed -n -e `/^Exec=/{s/^Exec=//;p;};`)"
if [ c"$EXEC" == c"" ] ; then continue ; fi
if [ c"$TEXEC" == c"" ] ; then TEXEC="$EXEC" ; fi
# cull common directory prefixes; assume "*/gnome" and "*/share"
# (note sed is slow and can be avoided by supplying bases above)
DIR="$(dirname "$file")"
DIR="$(echo "$DIR" | sed -e `s/^.*gnome///`)"
DIR="$(echo "$DIR" | sed -e `s/^.*share///`)"
mkdir -p "$HOME/gnome-mwm/$DIR" 2>&1 >> /dev/null
# put it in the slot
echo -e ""$DESC" f.exec "$TEXEC &"" >> "$HOME/gnome-mwm/$DIR/mwm.list"
if [ "$DEBUG" -ge 0 ] ; then GROUP="$(basename $(dirname "$file"))" ; fi
echo_v "mkdir $HOME/gnome-mwm/$DIR"
echo_v "$GROUP "$DESC" f.exec "... &"" >> /dev/stderr
done
echo_v "--- F1 FINISHED ---"
}
# --------------------------------------------------------------------
# 2. Make any middlemen by adding directory names as menu titles to [the] list.
function f2 () {
cd $HOME/gnome-mwm
for dir in $(find -type d) ; do
if [ ! -z "$(find -type d -mindepth 0)" ] ; then
DESC="$(basename $dir)"
TEXEC="$(echo $dir | sed -e `s/.///;s///-/g`)"
echo -e ""$DESC" f.menu $TEXEC" >> "$dir/../mwm.list"
echo_v ""$DESC" f.menu $TEXEC"
fi
done
echo_v "--- F2 FINISHED ---"
}
#--------------------------------------------------
# 3. Make menus from all .list files.
function f3 () {
cd $HOME/gnome-mwm
for dir in $(find -type d) ; do
if [ -f "$dir/mwm.list" ]; then
TEXEC="$(echo $dir | sed -e `s/.///;s///-/g`)"
if [ "$TEXEC" == "" ] ; then
break ;
fi
if [ "$TEXEC" == "." ] ; then
TEXEC="gnome-mwm"
fi
echo -e "Menu $TEXEC
{" > "$dir/mwm.list.menu"
cat "$dir/mwm.list" | sort | uniq >> "$dir/mwm.list.menu"
echo "}" >> "$dir/mwm.list.menu"
echo_v "$dir/mwm.list.menu"
fi
done
echo_v "--- F3 FINISHED ---"
}
#--------------------------------------------------
# 4. group menus into file ; caveat - add marker
function f4 () {
cd $HOME/gnome-mwm
echo -e "!---Marker Do not edit between lines script can rewrite them." > marker
cat marker $(find -name "*.menu") marker > gnome-mwm.menu
echo_v "--- F4 FINISHED ---"
}
#--------------------------------------------------
# 5. caveat allow removal of old Gnome folder for updating.
function f5 () {
cd $HOME/gnome-mwm
cp -p ../.mwmrc gnome-mwm.mwmrc
if grep "gnome-mwm" gnome-mwm.mwmrc 2>&1 >> /dev/null ; then
# del first pair
if [ "$ALLOW" -eq 1 ] && [ "$(grep "!---Marker" gnome-mwm.mwmrc | wc -l)" -ge 2 ] ; then
if echo -e "r gnome-mwm.mwmrc
/!---Marker/
.,/!---Marker/d
w gnome-mwm.mwmrc.new" | ed 2>&1 >> /dev/null ; then
mv gnome-mwm.mwmrc.new gnome-mwm.mwmrc
else
echo "Oops Couldn`t remove old Gnome folder. Chech .mwmrc.new and see what happened." >> /dev/stderr
exit 1
fi ; fi
# adding partial gnome menu support to mwm would be easier
# than going any further
fi
echo_v "--- F5 FINISHED ---"
}
#--------------------------------------------------
# 6. caveat add one menu line to root menu if not there
function f6 () {
cd $HOME/gnome-mwm
# this ed`its .mwmrc.new to add one text line
if ! grep "f.menu.*gnome-mwm" gnome-mwm.mwmrc 2>&1 >> /dev/null ; then
if grep "Menu DefaultRootMenu" gnome-mwm.mwmrc 2>&1 >> /dev/null && which ed 2>&1 >> /dev/null ; then
if echo -e "r gnome-mwm.mwmrc
/Menu DefaultRootMenu/
+
.a
"Gnome Menus" f.menu gnome-mwm
.
w gnome-mwm.mwmrc.new
" | ed >> /dev/null 2>&1 ; then
mv gnome-mwm.mwmrc.new gnome-mwm.mwmrc
else
echo -e "Oops you must add ""Gnome Menus" f.menu gnome-mwm" to the DefaultRootMenu in $HOME/gnome-mwm/gnome-mwm.mwmrc or .mwmrc manually." >> /dev/stderr
fi ; fi ; fi
echo_v "--- F6 FINISHED ---"
}
#--------------------------------------------------
# 7. copy over the results
function f7 () {
cd $HOME/gnome-mwm
cat gnome-mwm.menu gnome-mwm.mwmrc > gnome-mwm.mwmrc.new
cp -p gnome-mwm.mwmrc.new ../.mwmrc
echo_v "modified $HOME/.mwmrc"
echo_v "--- F7 FINISHED ---"
}
#--------------------------------------------------
# standard arg handling and startup checks below
#
DEBUG=0 ; ALLOW=0 ; GO=0 ; GOALL=0 ; ERR=0
if [ -z $1 ] || [ $1 == "--help" ]; then
ERR=1
fi
if [ $ERR == 0 ]; then
while getopts "Ghdag" Option
do
case $Option in
h ) ERR=1 ;;
d ) DEBUG=1 ;;
G ) GOALL=1 ;;
g ) GO=$OPTARG ;;
a ) ALLOW=1 ;;
* ) ERR=1
break
;;
esac
done
shift $(($OPTIND - 1))
fi
cd $HOME
if [ ! -f .mwmrc ] ; then ERR=1 ; echo "Get [the default] $HOME/.mwmrc first." >> /dev/stderr ; fi
if ! which sort grep sed uniq locate 2>&1 >> /dev/null ; then
echo "Sorry, I need sort, grep, sed, uniq to finish." >> /dev/stderr
ERR=1 ; fi
if [ "$GO" -eq 0 ] && [ "$GOALL" -eq 0 ] ; then ERR=1 ; fi
if [ $ERR -gt 0 ]; then
echo "USAGE gnome-mwm [-G|-g num|-h]"
echo "DESCRIPTION Import gnome`s current menus to a folder in mwm root menu."
echo "$HOME/gnome-mwm gets menus; which are added into $HOME/.mwmrc"
echo "-G GO, do it all. You must restart mwm to see new folder."
echo "-a Allow removal of previously added "Gnome Menu" for updating." >> /dev/stderr
echo "-g 8 Only make menus in gnome-mwm/ but don`t touch .mwmrc"
echo "-g num Do just the indicated task. See script."
echo "-d show debug messages."
echo "-h This help message."
exit 1
fi
#--------------------------------------------------
#1 protect .mwmrc
if which ci 2>&1 >> /dev/null ; then
ci -m"nada" -q -l .mwmrc ; ci -m"nada" -q -l .mwmrc.orig
else if [ -f .mwmrc.orig ] ; then
echo "Will not clobber .mwmrc.orig - try ci" >> /dev/stderr
exit 1
else cp -p .mwmrc .mwmrc.orig ; fi
fi
mkdir $HOME/gnome-mwm 2>>/dev/null
# Here is a trick that took me a long time to find
# Long filename support COMPATIBLE with most old unix scripts using
# the iconic linux favorite sh (note csh does this one thing easier).
# Answer use arcane sh syntax for line mode. Wrap filenames in single quote.
#WRAP="sed -e "s/^/`/;s/$/`/""
IFS=$`
` # super secret $`` escape to set IFS correctly
if [ "$GO" -ge 1 ] ; then
case $GO in
0) ;;
1) f1 ;;
2) f2 ;;
3) f3 ;;
4) f4 ;;
5) f5 ;;
6) f6 ;;
7) f7 ;;
7) f1 ; f2 ; f3 ; f4 ;;
8) f5 ; f6 ; f7 ; f8 ;;
9) GOALL=1 ;;
*) echo "g[1,9] are selections - see help" >> /dev/stderr
exit 1
break;;
esac
exit
fi
if [ "$GOALL" -ge 1 ] ; then
f1 ; f2 ; f3 ; f4 ; f5 ; f6 ; f7
fi
#--------------------------------------------------
wow, neat! ill have to try it out.
copy, paste, save.....