chiark / gitweb /
debian/changelog: start -4~
[vtwm.git] / contrib / support / sysrc_add_apps.sh
1 #!/bin/sh
2 #
3 # Adds installed applications to the box-stock system.vtwmrc file.
4
5 syntax ()
6 {
7         # could be a "here-doc", but it'd break at least one shell
8         echo
9         echo "Usage: ${0##*/} [-i in_file] [-o out_file] [-m menu_decl] \\"
10         echo "       [-a app_list | -A app_file] [-f] [-h]"
11         echo
12         echo "Default in_file: $DEF_INPUT"
13         echo "Default out_file: $DEF_OUTPUT"
14         echo "Default menu_decl: $DEF_MENU_DECL"
15         echo "Default app_list: $DEF_LIST"
16         echo
17         echo "app_file is a list, each line containing one application specification"
18         echo "(or the keywords \"SEPARATOR\" or \"TITLE::string\"). The specification is"
19         echo "of the form \"name::string\", where \"name\" is the menu entry and \"string\""
20         echo "is the command to execute. Whitespace in either part is indicated with"
21         echo "a single colon."
22         echo
23
24         exit
25 }
26
27 check_replace ()
28 {
29         CWD=$PWD
30         cd `dirname "$INPUT"` && INDIR=$PWD && INNAME=`basename "$INPUT"`
31         cd `dirname "$OUTPUT"` && OUTDIR=$PWD && OUTNAME=`basename "$OUTPUT"`
32         cd $CWD
33         if [ "$INDIR" = "$OUTDIR" -a "$INNAME" = "$OUTNAME" ]; then
34                 if [ $NO_PROMPT -eq 1 ]; then
35                         echo "Replacing in_file with out_file."
36                 else
37                         echo -n "Replace in_file with out_file (y/n)? "
38                         read ANS
39                         [ "$ANS" != "y" -a "$ANS" != "Y" ] && \
40                             echo "Exiting." && exit
41                 fi
42
43                 OUTPUT=$TMPOUT
44         fi
45 }
46
47 check_X_path ()
48 {
49         ANS=`which xterm 2>/dev/null`
50         if [ -z "$ANS" ]; then
51                 if [ $NO_PROMPT -eq 1 ]; then
52                         echo "No path to X applications (xterm)."
53                 else
54                         echo "The path to X applications (xterm) isn't set."
55                         while true; do
56                                 echo -n "Enter full X path: "
57                                 read ANS
58                                 [ -z "$ANS" ] && echo "Exiting." && exit
59                                 [ -d "$ANS" -a -f "$ANS/xterm" -a \
60                                     -x "$ANS/xterm" ] && break
61                         done
62                         PATH=$PATH:$ANS
63                 fi
64         fi
65 }
66
67 check_proceed ()
68 {
69         echo
70         echo "in_file: $INPUT"
71         if [ "$OUTPUT" = "$TMPOUT" ]; then
72                 echo "out_file: $INPUT"
73         else
74                 echo "out_file: $OUTPUT"
75         fi
76         echo "menu_decl: $MENU_DECL"
77         echo "app_list: $APP_LIST"
78         echo "app_file: $APP_FILE"
79         echo "Search path: $PATH"
80         echo
81         if [ $NO_PROMPT -eq 1 ]; then
82                 echo "Proceeding..."
83         else
84                 echo -n "Proceed (y/n)? "
85                 read ANS
86                 [ "$ANS" != "y" -a "$ANS" != "Y" ] && echo "Exiting." && exit
87         fi
88 }
89
90 check_app ()
91 {
92         if [ "$1" = "SEPARATOR" ]; then
93                 printf "\t\"\"\t\tf.separator\n" >>$2
94         else
95                 CMD=${1%%::*}
96                 STR=${1##*::}
97
98                 if [ "$CMD" = "TITLE" ]; then
99                         printf "\t\"  %s  \"\t\tf.title\n" $STR \
100                             |sed -e 's/:/ /g' >>$2
101                 else
102                         [ -z "`which $CMD 2>/dev/null`" ] && return
103                         printf "\t\"%s\"\t\tf.exec \"%s &\"\n" $CMD $STR \
104                             |sed -e 's/:/ /g' >>$2
105                 fi
106         fi
107 }
108
109 DEF_INPUT=./system.vtwmrc
110 DEF_OUTPUT=./custom.vtwmrc
111
112 DEF_LIST="TITLE::Shells emu eterm mxterm rxvt xterm xvt"
113 DEF_LIST="$DEF_LIST midc::xterm:-e:midc top::xterm:-e:top"
114 DEF_LIST="$DEF_LIST TITLE::Editors nedit xcoral xvile"
115 DEF_LIST="$DEF_LIST vi::xterm:-e:vi vile::xterm:-e:vile vim::xterm:-e:vim"
116 DEF_LIST="$DEF_LIST SEPARATOR gimp xfig xpaint xv xbmbrowser bitmap pixmap"
117 DEF_LIST="$DEF_LIST TITLE::Desktop applix soffice abiword lyx ted::Ted"
118 DEF_LIST="$DEF_LIST gnumeric ghostview gv xcal xcalendar"
119 DEF_LIST="$DEF_LIST SEPARATOR tkman xman"
120 DEF_LIST="$DEF_LIST SEPARATOR gmplayer xine xmcd xmmix xmms"
121 DEF_LIST="$DEF_LIST SEPARATOR hexcalc xcalc editres"
122 DEF_LIST="$DEF_LIST xbiff xcb xev xeyes xload xmag"
123 DEF_LIST="$DEF_LIST SEPARATOR moonclock mouseclock oclock"
124 DEF_LIST="$DEF_LIST rclock sunclock t3d xarclock xclock xdaliclock"
125 DEF_LIST="$DEF_LIST TITLE::Network chimera ie mozilla netscape opera"
126 DEF_LIST="$DEF_LIST links::xterm:-e:links lynx::xterm:-e:lynx"
127 DEF_LIST="$DEF_LIST SEPARATOR exmh knews xdir"
128 DEF_LIST="$DEF_LIST ftp::xterm:-e:ftp telnet::xterm:-e:telnet"
129 DEF_LIST="$DEF_LIST elm::xterm:-e:elm mutt::xterm:-e:mutt"
130 DEF_LIST="$DEF_LIST pine::xterm:-e:pine tin::xterm:-e:tin"
131
132 DEF_MENU_DECL="menu \"apps\""
133
134 NO_PROMPT=0
135
136 TMPOUT=out.$$
137 LISTOUT=list.$$
138
139 while getopts "i:o:m:a:A:fh" OPT; do
140         case $OPT in
141                 i) INPUT=$OPTARG;;
142                 o) OUTPUT=$OPTARG;;
143                 m) MENU_DECL=$OPTARG;;
144                 a) APP_LIST=$OPTARG;;
145                 A) APP_FILE=$OPTARG;;
146                 f) NO_PROMPT=1;;
147                 *) syntax;;
148         esac
149 done
150
151 [ -z "$INPUT" ] && INPUT=$DEF_INPUT
152 [ -z "$OUTPUT" ] && OUTPUT=$DEF_OUTPUT
153 [ -z "$APP_LIST" ] && APP_LIST=$DEF_LIST
154 [ -n "$APP_FILE" ] && APP_LIST=
155 [ -z "$MENU_DECL" ] && MENU_DECL=$DEF_MENU_DECL
156
157 [ ! -f "$INPUT" ] && echo "$INPUT: File not found" && syntax
158
159 check_replace
160 check_X_path
161 check_proceed
162
163 if [ -n "$APP_LIST" ]; then
164         for APP in $APP_LIST; do
165                 check_app $APP $LISTOUT
166         done
167 else
168         if [ -f "$APP_FILE" ]; then
169                 while read APP; do
170                         check_app $APP $LISTOUT
171                 done <"$APP_FILE"
172         else
173                 echo "$APP_FILE: File not found"
174                 exit
175         fi
176 fi
177
178 [ ! -s $LISTOUT ] && echo "No applications added." && exit
179
180 uniq $LISTOUT $$.$$
181 mv $$.$$ $LISTOUT
182
183 cat "$INPUT" \
184     |awk -v list="$LISTOUT" -v menu="$MENU_DECL" \
185     'BEGIN { \
186         split(menu, menu); \
187     } { \
188         if ($1 == menu[1] && $2 == menu[2]) { \
189             print $0; \
190             do { \
191                 if ($1 == "{") print "{"; \
192                 a = getline; \
193             } while (a && $1 != "}"); \
194             if (a && $1 == "}") { \
195                 while (getline app <list) \
196                     print app; \
197                 print $0; \
198             } \
199         } else \
200             print $0; \
201     }' \
202     >"$OUTPUT"
203
204 [ -s $TMPOUT ] && mv $TMPOUT "$INPUT"
205 rm -f *.$$
206 echo "Done."