WPrefs: grouped the choices for Window Resize Display in a single place
[wmaker-crm.git] / util / wm-oldmenu2new
blob5ac801551857a224f828933651043d17b0d94f2b
1 #!/bin/sh
3 # wm-oldmenu2new: script to convert from old-style WindowMaker
4 # menu file to the new PropertyList style of the WMRootMenu.
6 # Note: ex in all the Linux systems I've used is badly
7 # broken, except for nex. perl can be relied on however.
8 # Re-written to use perl.
10 # Local dependencies:
11 # None.
13 # Authors: Luke Kendall, Toby J Sargeant
15 # Copyright waived; no warranty provided.
18 GLW=GNUstep/Library/WindowMaker
19 GD=GNUstep/Defaults
20 WLW=$HOME/$GLW
21 WD=$HOME/$GD
23 MYNAME=`basename $0`
24 USAGE="usage: $MYNAME [menu-file-specifier]
25 E.g. $MYNAME menu.pt
26 or $MYNAME pt
27 The default menu if no arguments are given is the English one, 'menu'."
30 # Process arguments - work out which language menu we're converting.
31 # Note that foreign language locales do *not* have the .lang suffix
32 # attached to the WMRootMenu name.
34 OLD_MENU=menu
35 NEW_MENU=WMRootMenu
36 if [ $# = 1 ]
37 then
38 if [ -s "$WLW/menu.$1" ]
39 then
40 OLD_MENU="menu.$1"
41 NEW_MENU="WMRootMenu"
42 elif [ -s "$WLW/$1" ]
43 then
44 OLD_MENU="$1"
45 x=`expr "$1" : "menu\.\(.*\)"`
46 [ "x$x" != "x" ] && NEW_MENU="WMRootMenu"
47 else
48 echo "$MYNAME: $WLW/$1 does not exist" >&2
49 exit 1
51 elif [ $# != 0 ]
52 then
53 echo "$USAGE" >&2
54 exit 1
58 # For working out what cc is installed
60 which1()
62 oldpath=$PATH
63 PATH=/bin:/usr/bin:/usr/local/bin
65 IFS=":"
66 for j in $oldpath
68 test -x $j/$1 && test ! -d $j/$1 && echo $j/$1 && return 0
69 done
70 IFS=" "
71 return 1
75 # Expand macros if necessary.
76 # Create a temp copy of the menu file to edit to turn into the new.
78 TD=$(/bin/mktemp -d /tmp/wmmenu.XXXXXX) || { echo "$0: can not create temporary file" >& 2; exit 1; }
79 T=$TD/wmmenu$$
81 echo "Converting $GLW/$OLD_MENU --> $GD/$NEW_MENU"
82 cd $WLW || exit 1
83 if [ ! -s "$OLD_MENU" ]
84 then
85 echo "$MYNAME: $WLW/$OLD_MENU does not exist" >&2
86 exit 1
89 # Always pre-process, to join lines split with \, and to strip comments.
90 # Not to mention the main purpose, include & process wmmacros if used.
92 set -e
93 CC=`which1 cc`
94 [ "x$CC" = "x" ] && CC=`which1 gcc`
95 [ "x$CC" = "x" ] && "$MYNAME: no cc, gcc found - can't preprocess" >&2 && exit 1
98 # Use the "parse as if it's C option" if cc is gcc, because
99 # newer versions apparently get confused. Apparently gcc -E does
100 # not simply run the preprocessor (that's sad).
102 strings "$CC" | grep -l gcc > /dev/null && GCC_FLAGS="-x c"
104 cp $OLD_MENU $T-c
106 # Given the set -e, the exit 1 shouldn't be needed. But it is, on my NeXT!
108 $CC -E -I. $GCC_FLAGS $T-c > $T+c || exit 1
109 sed '/^#/d;/^[ ]*$/d' $T+c > $T
110 rm $T-c $T+c
111 set +e
114 # This is the interesting bit. Edit the old style menu and
115 # convert into new style property-list menu.
117 perl - $T <<-'EOF' > $T-p
118 $v=chr(22);
119 for (<>) {
120 push @foo,$_;
122 for (@foo) {
123 s/\s*$//;
124 s/^(\s*)"*(Workspaces*)"*\s\s*(WORKSPACE_MENU)/\1(\2, \3),/;
125 s/^(\s*)("[^"]*")\s+MENU/\1($v\n\1\2,/;
126 push @foo2,split "\n";
128 @foo=();
129 for (@foo2) {
130 s/^(\s*)"([^"]*)"\s\s*END/\1),/;
131 s/^(\s*)"([^"]*)"\s\s*EXEC\s\s*(.*)$/\1($v\n\1"\2",$v\n\1EXEC,$v\n\1"\3"$v\n\1),/;
132 s/^(\s*)"([^"]*)"\s\s*SHEXEC\s\s*(.*)$/\1($v\n\1"\2",$v\n\1SHEXEC,$v\n\1"\3"$v\n\1),/;
133 push @foo,split "\n";
135 @foo2=();
136 for (@foo) {
138 s/^(\s*)"([^"]*)"\s\s*OPEN_MENU\s\s*(.*)$/\1($v\n\1"\2",$v\n\1OPEN_MENU,$v\n\1"\3"$v\n\1),/;
139 push @foo2,split "\n";
141 @foo=();
142 for (@foo2) {
143 s/^(\s*)([^ ]*)\s\s*MENU/\1($v\n\1"\2",/;
144 push @foo,split "\n";
146 @foo2=();
147 for (@foo) {
148 s/^(\s*)([^ ]*)\s\s*END/\1),/;
149 s/^(\s*)([^ ]*)\s\s*EXEC\s\s*(.*)$/\1($v\n\1"\2",$v\n\1EXEC,$v\n\1"\3"$v\n\1),/;
150 s/^(\s*)([^ ]*)\s\s*SHEXEC\s\s*(.*)$/\1($v\n\1"\2",$v\n\1SHEXEC,$v\n\1"\3"$v\n\1),/;
151 push @foo2,split "\n";
153 @foo=();
154 for (@foo2) {
155 s/^(\s*)([^
156 ]*)\s\s*OPEN_MENU\s\s*(.*)$/\1($v\n\1"\2",$v\n\1OPEN_MENU,$v\n\1"\3"$v\n\1),/;
157 push @foo,split "\n";
159 @foo2=();
160 for (@foo) {
161 s/ WITH / QQQjjQQQ /;
162 s/^(\s*)"([^"]*)"\s\s*([A-Z_][A-Z_]*)$/\1("\2", \3),/;
163 s/^(\s*)"([^"]*)"\s+([A-Z_][A-Z_]*)\s\s*(.*)$/\1("\2", \3, \4),/;
164 s/"(.*".*)"/JJJqqJJJ\1JJJqqJJJ/;
165 /JJJqqJJJ/ && s/"/\\"/g;
166 s/JJJqqJJJ/"/g;
167 s/ QQQjjQQQ / WITH /;
168 print "$_\n";
171 mv $T-p $T
174 # Now strip off spurious commas from lines like:
175 # ),
177 # since comma is a property separator, not terminator. Sigh.
178 # Also correct for another problem - Linux ex's require the CTRL-V
179 # above; a real vi/ex doesn't; so we have to strip out any spurious
180 # CTRL-V characters if we're using a real ex:
182 sed 's/\x16//g' $T | awk '
184 if (last_line != null)
186 if ((last_line ~ /,$/) && ($0 ~ /^[ ]*\)/))
187 print substr(last_line, 0, length(last_line)-1)
188 else
189 print last_line
191 last_line = $0
194 END {
195 if (last_line != null)
197 if (last_line ~ /,$/)
198 print substr(last_line, 0, length(last_line)-1)
199 else
200 print last_line
203 ' > $WD/$NEW_MENU.new || exit 1
205 rm -f $T
206 rm -fr $TD
209 # Now install it.
211 cd $WD
212 if [ -s $NEW_MENU ]
213 then
214 echo "Preserving $NEW_MENU as $NEW_MENU.sav in $WD"
215 mv $NEW_MENU $NEW_MENU.sav
217 mv $NEW_MENU.new $NEW_MENU && echo "Created new $WD/$NEW_MENU"