Updating to version 0.20.2
[wmaker-crm.git] / util / wm-oldmenu2new
blob61f32f8585728241cf3f7c9049f548896cde597d
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 # I am *assuming* that foreign language locales have the .lang suffix
32 # attached to the WMRootMenu name. I hope that's right!
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.$1"
42 elif [ -s "$WLW/$1" ]
43 then
44 OLD_MENU="$1"
45 x=`expr "$1" : "menu\.\(.*\)"`
46 [ "x$x" != "x" ] && NEW_MENU="WMRootMenu.$x"
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 T=/tmp/wmmenu$$
79 echo "Converting $GLW/$OLD_MENU --> $GD/$NEW_MENU"
80 cd $WLW || exit 1
81 if [ ! -s "$OLD_MENU" ]
82 then
83 echo "$MYNAME: $WLW/$OLD_MENU does not exist" >&2
84 exit 1
87 # Always pre-process, to join lines split with \, and to strip comments.
88 # Not to mention the main purpose, include & process wmmacros if used.
90 set -e
91 CC=`which1 cc`
92 [ "x$CC" = "x" ] && CC=`which1 gcc`
93 [ "x$CC" = "x" ] && "$MYNAME: no cc, gcc found - can't preprocess" >&2 && exit 1
94 cp $OLD_MENU $T-c
96 # Given the set -e, the exit 1 shouldn't be needed. But it is, on my NeXT!
98 $CC -E -I. $T-c > $T+c || exit 1
99 sed '/^#/d;/^[ ]*$/d' $T+c > $T
100 rm $T-c $T+c
101 set +e
104 # This is the interesting bit. Edit the old style menu and
105 # convert into new style property-list menu.
107 perl - $T <<-'EOF' > $T-p
108 $v=chr(22);
109 for (<>) {
110 push @foo,$_;
112 for (@foo) {
113 s/\s*$//;
114 s/^(\s*)"*(Workspaces*)"*\s\s*(WORKSPACE_MENU)/\1(\2, \3),/;
115 s/^(\s*)("[^"]*")\s+MENU/\1($v\n\1\2,/;
116 push @foo2,split "\n";
118 @foo=();
119 for (@foo2) {
120 s/^(\s*)"([^"]*)"\s\s*END/\1),/;
121 s/^(\s*)"([^"]*)"\s\s*EXEC\s\s*(.*)$/\1($v\n\1"\2",$v\n\1EXEC,$v\n\1"\3"$v\n\1),/;
122 push @foo,split "\n";
124 @foo2=();
125 for (@foo) {
126 s/^(\s*)"([^"]*)"\s\s*OPEN_MENU\s\s*(.*)$/\1($v\n\1"\2",$v\n\1OPEN_MENU,$v\n\1"\3"$v\n\1),/;
127 push @foo2,split "\n";
129 @foo=();
130 for (@foo2) {
131 s/^(\s*)([^ ]*)\s\s*MENU/\1($v\n\1"\2",/;
132 push @foo,split "\n";
134 @foo2=();
135 for (@foo) {
136 s/^(\s*)([^ ]*)\s\s*END/\1),/;
137 s/^(\s*)([^ ]*)\s\s*EXEC\s\s*(.*)$/\1($v\n\1"\2",$v\n\1EXEC,$v\n\1"\3"$v\n\1),/;
138 push @foo2,split "\n";
140 @foo=();
141 for (@foo2) {
142 s/^(\s*)([^ ]*)\s\s*OPEN_MENU\s\s*(.*)$/\1($v\n\1"\2",$v\n\1OPEN_MENU,$v\n\1"\3"$v\n\1),/;
143 push @foo,split "\n";
145 @foo2=();
146 for (@foo) {
147 s/ WITH / QQQjjQQQ /;
148 s/^(\s*)"([^"]*)"\s\s*([A-Z_][A-Z_]*)$/\1("\2", \3),/;
149 s/^(\s*)"([^"]*)"\s+([A-Z_][A-Z_]*)\s\s*(.*)$/\1("\2", \3, \4),/;
150 s/"(.*".*)"/JJJqqJJJ\1JJJqqJJJ/;
151 /JJJqqJJJ/ && s/"/\\"/g;
152 s/JJJqqJJJ/"/g;
153 s/ QQQjjQQQ / WITH /;
154 print "$_\n";
157 mv $T-p $T
160 # Now strip off spurious commas from lines like:
161 # ),
163 # since comma is a property separator, not terminator. Sigh.
164 # Also correct for another problem - Linux ex's require the CTRL-V
165 # above; a real vi/ex doesn't; so we have to strip out any spurious
166 # CTRL-V characters if we're using a real ex:
168 sed 's/\x16//g' $T | awk '
170 if (last_line != null)
172 if ((last_line ~ /,$/) && ($0 ~ /^[ ]*\)/))
173 print substr(last_line, 0, length(last_line)-1)
174 else
175 print last_line
177 last_line = $0
180 END {
181 if (last_line != null)
183 if (last_line ~ /,$/)
184 print substr(last_line, 0, length(last_line)-1)
185 else
186 print last_line
189 ' > $WD/$NEW_MENU.new || exit 1
191 rm $T
194 # Now install it.
196 cd $WD
197 if [ -s $NEW_MENU ]
198 then
199 echo "Preserving $NEW_MENU as $NEW_MENU.sav in $WD"
200 mv $NEW_MENU $NEW_MENU.sav
202 mv $NEW_MENU.new $NEW_MENU && echo "Created new $WD/$NEW_MENU"