~ubuntu-branches/debian/stretch/podget/stretch

« back to all changes in this revision

Viewing changes to SCRIPTS/podget-add-feed

  • Committer: Package Import Robot
  • Author(s): Dave Vehrs
  • Date: 2012-12-27 12:48:50 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20121227124850-deycdz086zbp7ce1
Tags: 0.6.9-1
Updated Debian version number to stay in sync with changes to the
source package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
 
3
# Copyright (C) 2009 Steven Black <yam655@gmail.com>
 
4
# Contributed to podget on 2009-11-06 to be released under the same license
 
5
#   as podget (currently GPL)
 
6
 
 
7
EMPTY_GROUP="Unknown"
 
8
EMPTY_TITLE="Unknown"
 
9
 
 
10
PODGET_DIR="$HOME/.podget"
 
11
config_serverlist=`sed -r -n -e 's/^[   ]*config_serverlist[    ]*=[    ]*(["]?)(.*)\1[         ]*$/\2/p' "$PODGET_DIR/podgetrc"`
 
12
dir_library=`sed -r -n -e 's/^[         ]*dir_library[  ]*=[    ]*(["]?)(.*)\1[         ]*$/\2/p' "$PODGET_DIR/podgetrc"`
 
13
case "$config_serverlist" in
 
14
    /*)
 
15
        SERVERLIST="$config_serverlist"
 
16
        ;;
 
17
    *)
 
18
        SERVERLIST="$PODGET_DIR/$config_serverlist"
 
19
        ;;
 
20
esac
 
21
case "$dir_library" in
 
22
    /*)
 
23
        LIBRARY_DIR="$dir_library"
 
24
        ;;
 
25
    *)
 
26
        LIBRARY_DIR="$PODGET_DIR/$dir_library"
 
27
        ;;
 
28
esac
 
29
 
 
30
if [ ! -f "$SERVERLIST" ] ; then
 
31
    exit 0
 
32
fi
 
33
 
 
34
CURL=`which curl`
 
35
 
 
36
fake_title() {
 
37
    # Originally we faked a title from the URL.
 
38
    # Now we only do that if we can't get one via curl and sed
 
39
    FAKE_TITLE=""
 
40
    if [ -x "$CURL" ] ; then
 
41
        FAKE_TITLE=`"$CURL" -A "Mozilla/4.0" -f -L -s "$1" | sed -n -e '/<title>/{
 
42
s|^.*<title>\(.*\)</title>.*$|\1|Mp
 
43
q
 
44
}'`
 
45
    fi
 
46
    if [ -z "$FAKE_TITLE" ] ; then
 
47
        FAKE_TITLE=`sed -r \
 
48
            -e 's;^https?://;;' -e 's;[.](com|org|net)/;/;' \
 
49
            -e 's;^(www|web|feeds?[0-9]?)?[.];;' \
 
50
            -e 's;/(bookfeeds|[sS]ite)/;/;g' -e 's;/[mM][pP]3_[fF]eed;;g' \
 
51
            -e 's;[.]libsyn;;' -e 's;^(me|feedburner|librivox)/;;' \
 
52
            -e 's;/feeds?/;/;' -e 's;[.](xml|html)$;;' \
 
53
            -e 's;/feed$;;' \
 
54
            -e 's;/[?]feed=[^/.]+;;' -e 's;/rss/?$;;' -e 's;/$;;' \
 
55
            -e 's;%20; ;g' -e 's;%[0-9a-fA-F][0-9a-fA-F];;' -e 'y;/;_;' \
 
56
                    <<< "$1"`
 
57
    fi
 
58
}
 
59
# fake_title "$1"
 
60
# echo $FAKE_TITLE
 
61
# exit 0
 
62
 
 
63
unsetcompetition() {
 
64
    KDIALOG=""
 
65
    XDIALOG=""
 
66
    GTKDIALOG=""
 
67
    WHIPTAIL=""
 
68
    TCDIALOG=""
 
69
    DIALOG=""
 
70
    AUTOMODE=0
 
71
}
 
72
 
 
73
checkdialog() {
 
74
    USRDIALOG="$1"
 
75
    unsetcompetition
 
76
    if [ "$USRDIALOG" = "auto" ] ; then
 
77
        AUTOMODE=1
 
78
        return
 
79
    fi
 
80
    if [ ! -z "$USRDIALOG" ] ; then
 
81
        case "$USRDIALOG" in
 
82
            */*) ;;
 
83
            *) USRDIALOG=`which $USRDIALOG` ;;
 
84
        esac
 
85
        if [ ! -x "$USRDIALOG" ] ; then USRDIALOG="" ; fi
 
86
    fi
 
87
    if [ ! -z "$USRDIALOG" ] ; then
 
88
        case "$USRDIALOG" in
 
89
            */kdialog*)
 
90
                KDIALOG="$USRDIALOG"
 
91
                ;;
 
92
            */[xX]dialog*)
 
93
                XDIALOG="$USRDIALOG"
 
94
                ;;
 
95
            */zenity*)
 
96
                ZENITY="$USRDIALOG"
 
97
                ;;
 
98
            */gtkdialog*)
 
99
                GTKDIALOG="$USRDIALOG"
 
100
                ;;
 
101
            */whiptail*)
 
102
                WHIPTAIL="$USRDIALOG"
 
103
                ;;
 
104
            */tcdialog*)
 
105
                TCDIALOG="$USRDIALOG"
 
106
                ;;
 
107
            */sh|*/bash*)
 
108
                DIALOG=""
 
109
                ;;
 
110
            *)
 
111
                # treat as 'dialog'
 
112
                if [ ! -z "$DISPLAY" ] ; then
 
113
                    if tty -s ; then
 
114
                        # Normally 'dialog' will only be used on a real TTY.
 
115
                        # They have a real TTY, so we're fine.
 
116
                        true
 
117
                        DIALOG="$USRDIALOG"
 
118
                    else
 
119
                        # Woah, no TTY. Perhaps we should treat this as Xdialog.
 
120
                        XDIALOG="$USRDIALOG"
 
121
                    fi
 
122
                else
 
123
                    DIALOG="$USRDIALOG"
 
124
                fi
 
125
                ;;
 
126
        esac
 
127
    else
 
128
        if [ ! -z "$DISPLAY" ] ; then
 
129
            KDIALOG=`which kdialog`
 
130
            XDIALOG=`which Xdialog`
 
131
            GTKDIALOG=`which gtkdialog`
 
132
            # Note: We *never* try to use 'zenity' unless the user specified
 
133
            # it. It just doesn't work the way we want -- at all.
 
134
        fi
 
135
        if tty -s ; then
 
136
            WHIPTAIL=`which whiptail`
 
137
            TCDIALOG=`which tcdialog`
 
138
            DIALOG=`which dialog`
 
139
        fi
 
140
    fi
 
141
}
 
142
 
 
143
dialogcheck() {
 
144
    local DIALOGS=0
 
145
    if [ "$AUTOMODE" = "1" ] ; then
 
146
        if [ ! -z "$1" ] ; then
 
147
            echo "Running in automatic mode."
 
148
        fi
 
149
        return 0
 
150
    fi
 
151
    if [ ! -z "$DISPLAY" ] ; then
 
152
        if [ ! -z "$1" ] ; then
 
153
            echo "X dialog services:"
 
154
        fi
 
155
    fi
 
156
    if [ -x "$KDIALOG" ] ; then
 
157
        if [ ! -z "$1" ] ; then
 
158
            echo "kdialog: good"
 
159
        fi
 
160
        DIALOGS=1
 
161
    fi
 
162
    if [ -x "$GTKDIALOG" ] ; then
 
163
        if [ ! -z "$1" ] ; then
 
164
            echo "gtkdialog: good"
 
165
        fi
 
166
        DIALOGS=1
 
167
    fi
 
168
    if [ -x "$XDIALOG" ] ; then
 
169
        if [ ! -z "$1" ] ; then
 
170
            echo "xdialog: okay"
 
171
            echo "---------- NOTICE regarding the use of Xdialog ----------"
 
172
            echo "Xdialog does not have the capacity to load in an icon or "
 
173
            echo "pixmap. This means it'll be uglier than the competition."
 
174
            echo "-----------------------------------------------------------"
 
175
            echo "For a prettier display, install kdialog or gtkdialog."
 
176
        fi
 
177
        DIALOGS=1
 
178
    fi
 
179
    if [ -x "$ZENITY" ] ; then
 
180
        DIALOGS=1
 
181
        echo "zenity: BAD (and UNSUPPORTED)"
 
182
        echo "---------- WARNING regarding the use of Zenity ----------"
 
183
        echo "'zenity' is unsupported. 'zenity' has no support for yes/no or"
 
184
        echo "yes/no/cancel dialogs. It has no capacity to add a button and"
 
185
        echo "change labels, allowing any sort of usable yes/no/cancel system."
 
186
        echo "zenity does not let me repurpose ok/cancel dialogs. The return"
 
187
        echo "code for cancel is the same as abort, so I can't even make do"
 
188
        echo "with changing the behavior there."
 
189
        echo
 
190
        echo "The dialogs will be confusing and missing features."
 
191
        echo 
 
192
        echo "This is why it is unsupported."
 
193
        echo
 
194
        echo "Please install kdialog, Xdialog, or gtkdialog or run it from"
 
195
        echo "a terminal."
 
196
        echo
 
197
        echo "Really, even the automatic mode works better than zenity mode."
 
198
        echo "---------------------------------------------------------"
 
199
        echo "zenity is unsupported. Use at your own risk."
 
200
        if [ -z "$1" ] ; then
 
201
            somedialog --error "'zenity' is unsupported. 'zenity' has no support for yes/no or yes/no/cancel dialogs. It has no capacity to add a button and change labels, allowing any sort of usable yes/no/cancel system. zenity does not let me repurpose ok/cancel dialogs. The return code for cancel is the same as abort, so I can't even make do with changing the behavior there.\n\nThe dialogs will be confusing and missing features.\n\nThis is why it is unsupported.\n\nPlease install kdialog, Xdialog, or gtkdialog or run it from a terminal.\n\nReally, even the automatic mode works better than zenity mode."
 
202
        fi
 
203
    fi
 
204
    if [ $DIALOGS = 0 ] ; then
 
205
        if [ ! -z "$1" ] ; then
 
206
            echo "none: okay (X dialogs unavailable)"
 
207
            echo "------------ NOTICE regarding automatic mode -------------"
 
208
            echo "Will be fully automatic unless run from a terminal."
 
209
            echo "----------------------------------------------------------"
 
210
            echo "Please consider installing kdialog, Xdialog, or gtkdialog."
 
211
            echo
 
212
        fi
 
213
    fi
 
214
    if tty -s ; then
 
215
        if [ ! -z "$1" ] ; then
 
216
            echo "TTY dialog services:"
 
217
        fi
 
218
    fi
 
219
    if [ -x "$DIALOG" ] ; then
 
220
        if [ ! -z "$1" ] ; then
 
221
            echo "dialog: good"
 
222
        fi
 
223
    fi
 
224
    if [ -x "$WHIPTAIL" ] ; then
 
225
        if [ ! -z "$1" ] ; then
 
226
            echo "whiptail: okay (not as pretty as dialog)"
 
227
            echo "--------- NOTICE regarding the use of whiptail ---------"
 
228
            echo "The line-wrapping in message dialogs is fairly crude."
 
229
            echo "This results in the message dialogs being uglier than"
 
230
            echo "dialog."
 
231
            echo "--------------------------------------------------------"
 
232
            echo "For a prettier display, install dialog."
 
233
            echo
 
234
        fi
 
235
    fi
 
236
    if [ -x "$TCDIALOG" ] ; then
 
237
        if [ ! -z "$1" ] ; then
 
238
            echo "tcdialog: BAD"
 
239
            echo "---------- WARNING regarding the use of tcdialog ----------"
 
240
            echo "tcdialog requires the row and column sizes be specified, so"
 
241
            echo "it produces some awful looking dialogs as we do not "
 
242
            echo "precompute these sizes."
 
243
            echo
 
244
            echo "We've also seen cases of the cursor being missing in the"
 
245
            echo "input dialog with tcdialog."
 
246
            echo "-----------------------------------------------------------"
 
247
            echo "Please consider installing dialog."
 
248
            echo
 
249
        fi
 
250
    fi
 
251
    if [ ! -z "$1" ] ; then
 
252
        echo "bash: okay (not as pretty as dialog)"
 
253
    fi
 
254
}
 
255
 
 
256
askquestions() {
 
257
    local MY_URL="$1"
 
258
    local MY_GROUP="${2:-$EMPTY_GROUP}"
 
259
    local MY_TITLE="$3"
 
260
    CUR_ABORT=0
 
261
    fake_title "$MY_URL"
 
262
    MY_TITLE="${MY_TITLE:-${FAKE_TITLE:-$EMPTY_TITLE}}"
 
263
 
 
264
    somedialog --input "Title for $MY_URL" "$MY_TITLE"
 
265
    if [ $? -eq 1 ] ; then CUR_ABORT=1 ; return 1 ; fi
 
266
    CUR_TITLE="${SOMEDIALOG:-${FAKE_TITLE:-$EMPTY_TITLE}}"
 
267
 
 
268
    somedialog --input "Genre or group for $CUR_TITLE" "$MY_GROUP"
 
269
    if [ $? -eq 1 ] ; then CUR_ABORT=1 ; return 1 ; fi
 
270
    CUR_GROUP="${SOMEDIALOG:-$EMPTY_GROUP}"
 
271
 
 
272
    somedialog --yesNOcancel "Activate <$MY_URL> in $CUR_GROUP as '$CUR_TITLE'?"
 
273
    CUR_ACTIVATE="$?"
 
274
    if [ $CUR_ACTIVATE -eq 2 ] ; then CUR_ABORT=1 ; return 1 ; fi
 
275
    if [ -z "$CUR_GROUP" ] ; then
 
276
        CUR_GROUP="$EMPTY_GROUP"
 
277
    fi
 
278
    if [ -z "$CUR_TITLE" ] ; then
 
279
        CUR_TITLE="${FAKE_TITLE:-$EMPTY_TITLE}"
 
280
    fi
 
281
    # Because of the backticks, the \\ needs to be escaped to \\\\
 
282
    CUR_GROUP=`sed -e 'y; /\\\\;___;' <<< "$CUR_GROUP"`
 
283
    CUR_TITLE=`sed -e 'y;/\\\\;__;' <<< "$CUR_TITLE"`
 
284
    return $CUR_ABORT
 
285
}
 
286
 
 
287
somedialog() {
 
288
    local TYPE="$1"
 
289
    shift
 
290
    local RET=0
 
291
    if [ -x "$KDIALOG" ] ; then
 
292
        if [ "$TYPE" = "--yesNOcancel" ] ; then TYPE="--yesnocancel" ; fi
 
293
        if [ "$TYPE" = "--error" ] ; then
 
294
            "$KDIALOG" --error "$*"
 
295
        elif [ "$TYPE" = "--sorry" ] ; then
 
296
            "$KDIALOG" --sorry "$*"
 
297
        elif [ "$TYPE" = "--message" ] ; then
 
298
            "$KDIALOG" --msgbox "$*"
 
299
        elif [ "$TYPE" = "--yesno" ] ; then
 
300
            "$KDIALOG" --yesno "$*"
 
301
            RET="$?"
 
302
        elif [ "$TYPE" = "--yesnocancel" ] ; then
 
303
            "$KDIALOG" --yesnocancel "$*"
 
304
            RET="$?"
 
305
        elif [ "$TYPE" = "--input" ] ; then
 
306
            SOMEDIALOG=`"$KDIALOG" --inputbox "$1" "$2"`
 
307
            RET="$?"
 
308
        fi
 
309
    elif [ -x "$XDIALOG" ] ; then
 
310
        if [ "$TYPE" = "--yesNOcancel" ] ; then TYPE="--yesnocancel" ; fi
 
311
        if [ "$TYPE" = "--error" ] ; then
 
312
            "$XDIALOG" --stdout --msgbox "Error: $*" 0 0
 
313
        elif [ "$TYPE" = "--sorry" ] ; then
 
314
            "$XDIALOG" --stdout --msgbox "Sorry: $*" 0 0
 
315
        elif [ "$TYPE" = "--message" ] ; then
 
316
            "$XDIALOG" --stdout --msgbox "$*" 0 0
 
317
        elif [ "$TYPE" = "--yesno" ] ; then
 
318
            "$XDIALOG" --stdout --yesno "$*" 0 0
 
319
            RET="$?"
 
320
            if [ $RET -eq 255 ] ; then RET=1 ; fi
 
321
        elif [ "$TYPE" = "--yesnocancel" ] ; then
 
322
            "$XDIALOG" --stdout --yesno "$*" 0 0
 
323
            RET="$?"
 
324
            if [ $RET -eq 255 ] ; then RET=2 ; fi
 
325
        elif [ "$TYPE" = "--input" ] ; then
 
326
            SOMEDIALOG=`"$XDIALOG" --stdout --inputbox "$1" 0 0 "$2"`
 
327
            RET="$?"
 
328
            if [ $RET -eq 255 ] ; then RET=1 ; fi
 
329
        fi
 
330
    elif [ -x "$GTKDIALOG" ] ; then
 
331
        if [ "$TYPE" = "--yesNOcancel" ] ; then TYPE="--yesnocancel" ; fi
 
332
        if [ "$TYPE" = "--error" ] ; then
 
333
            GTKMSG=`echo "$*" | sed -e 's/</[/g' -e 's/>/]/g'`
 
334
            export GTK_DIALOG="
 
335
<vbox>
 
336
    <hbox>
 
337
        <pixmap><input file stock=\"error\"></input></pixmap>
 
338
        <text><label>$GTKMSG</label></text>
 
339
    </hbox>
 
340
    <hbox>
 
341
        <button ok></button>
 
342
        <button cancel></button>
 
343
    </hbox>
 
344
</vbox>
 
345
"
 
346
            "$GTKDIALOG" --program GTK_DIALOG | grep -q 'EXIT="OK"'
 
347
        elif [ "$TYPE" = "--sorry" ] ; then
 
348
            GTKMSG=`echo "$*" | sed -e 's/</[/g' -e 's/>/]/g'`
 
349
            export GTK_DIALOG="
 
350
<vbox>
 
351
<hbox><pixmap><input file stock=\"sorry\"></input></pixmap>
 
352
<text><label>$GTKMSG</label></text></hbox>
 
353
<hbox><button ok></button><button cancel></button></hbox>
 
354
</vbox>
 
355
"
 
356
            "$GTKDIALOG" --program GTK_DIALOG | grep -q 'EXIT="OK"'
 
357
        elif [ "$TYPE" = "--message" ] ; then
 
358
            GTKMSG=`echo "$*" | sed -e 's/</[/g' -e 's/>/]/g'`
 
359
            export GTK_DIALOG="
 
360
<vbox>
 
361
<hbox><pixmap><input file stock=\"info\"></input></pixmap>
 
362
<text><label>$GTKMSG</label></text></hbox>
 
363
<hbox><button ok></button><button cancel></button></hbox>
 
364
</vbox>
 
365
"
 
366
            "$GTKDIALOG" --program GTK_DIALOG | grep -q 'EXIT="OK"'
 
367
        elif [ "$TYPE" = "--yesno" ] ; then
 
368
            GTKMSG=`echo "$*" | sed -e 's/</[/g' -e 's/>/]/g'`
 
369
            export GTK_DIALOG="
 
370
<vbox>
 
371
<text><label>$GTKMSG</label></text>
 
372
<hbox><button yes></button><button no></button></hbox>
 
373
</vbox>
 
374
"
 
375
            GTK_EXIT=`"$GTKDIALOG" --program GTK_DIALOG | sed -n -e 's/^EXIT="\([^"]*\)"/\1/p'`
 
376
            case "$GTK_EXIT" in
 
377
                Yes) RET=0 ;;
 
378
                No) RET=1 ;;
 
379
                *) RET=1 ;;
 
380
            esac
 
381
        elif [ "$TYPE" = "--yesnocancel" ] ; then
 
382
            GTKMSG=`echo "$*" | sed -e 's/</[/g' -e 's/>/]/g'`
 
383
            export GTK_DIALOG="
 
384
<vbox>
 
385
<text><label>$GTKMSG</label></text>
 
386
<hbox><button yes></button><button no></button><button cancel></button></hbox>
 
387
</vbox>
 
388
"
 
389
            GTK_EXIT=`"$GTKDIALOG" --program GTK_DIALOG | sed -n -e 's/^EXIT="\([^"]*\)"/\1/p'`
 
390
            case "$GTK_EXIT" in
 
391
                Yes) RET=0 ;;
 
392
                No) RET=1 ;;
 
393
                *) RET=2 ;;
 
394
            esac
 
395
        elif [ "$TYPE" = "--input" ] ; then
 
396
            GTKMSG=`echo "$1" | sed -e 's/</[/g' -e 's/>/]/g'`
 
397
            GTKDEFAULT=`echo "$2" | sed -e 's/</[/g' -e 's/>/]/g'`
 
398
            # This isn't what is done in gtkdialog examples, however gtkdialog
 
399
            # does *no escaping* of the input, so you can provide input such
 
400
            # as  '" ; rm /etc/passwd ; true "' and what you get back is:
 
401
            #   SOMEDIALOG="" ; rm /etc/passwd ; true ""
 
402
            export GTK_DIALOG="
 
403
<vbox><text><label>$GTKMSG</label></text>
 
404
<entry><default>$GTKDEFAULT</default><variable>SOMEDIALOG</variable></entry>
 
405
<hbox><button ok></button><button cancel></button></hbox>
 
406
</vbox>
 
407
"
 
408
            GTK_LINES=`"$GTKDIALOG" --program GTK_DIALOG`
 
409
            SOMEDIALOG=`sed -n -e 's/^SOMEDIALOG="\(.*\)"$/\1/p' <<< "$GTK_LINES"`
 
410
            GTK_EXIT=`sed -n -e 's/^EXIT="\([^"]*\)"/\1/p' <<< "$GTK_LINES"`
 
411
            case "$GTK_EXIT" in
 
412
                OK) RET=0 ;;
 
413
                *) SOMEDIALOG="" ; RET=1 ;;
 
414
            esac
 
415
        fi
 
416
    elif [ -x "$ZENITY" ] ; then
 
417
        # There is *NO CANCEL* option, so it is depricated
 
418
        # It also has no Yes/No, so the yesno questions come out confusing
 
419
        if [ "$TYPE" = "--yesNOcancel" ] ; then TYPE="--yesnocancel" ; fi
 
420
        if [ "$TYPE" = "--error" ] ; then
 
421
            GTKMSG=`echo "$*" | sed -e 's/</[/g' -e 's/>/]/g'`
 
422
            "$ZENITY" --error --text="$GTKMSG"
 
423
        elif [ "$TYPE" = "--sorry" ] ; then
 
424
            GTKMSG=`echo "$*" | sed -e 's/</[/g' -e 's/>/]/g'`
 
425
            "$ZENITY" --warning --text="$GTKMSG"
 
426
        elif [ "$TYPE" = "--message" ] ; then
 
427
            GTKMSG=`echo "$*" | sed -e 's/</[/g' -e 's/>/]/g'`
 
428
            "$ZENITY" --info --text="$GTKMSG"
 
429
        elif [ "$TYPE" = "--yesno" ] ; then
 
430
            GTKMSG=`echo "$*" | sed -e 's/</[/g' -e 's/>/]/g'`
 
431
            "$ZENITY" --question --text="$GTKMSG"
 
432
            RET="$?"
 
433
        elif [ "$TYPE" = "--yesnocancel" ] ; then
 
434
            GTKMSG=`echo "$*" | sed -e 's/</[/g' -e 's/>/]/g'`
 
435
            "$ZENITY" --question --text="$GTKMSG"
 
436
            RET="$?"
 
437
        elif [ "$TYPE" = "--input" ] ; then
 
438
            GTKMSG=`echo "$1" | sed -e 's/</[/g' -e 's/>/]/g'`
 
439
            GTKDEFAULT=`echo "$2" | sed -e 's/</[/g' -e 's/>/]/g'`
 
440
            SOMEDIALOG=`"$ZENITY" --entry --text="$GTKMSG" --entry-text="$GTKDEFAULT"`
 
441
            RET="$?"
 
442
        fi
 
443
    elif tty -s && [ $AUTOMODE = 0 ] ; then
 
444
        TTY_ABORT=0
 
445
        # trap TTY_ABORT=1 SIGINT
 
446
        if [ -x "$DIALOG" ] ; then
 
447
            if [ "$TYPE" = "--yesNOcancel" ] ; then TYPE="--yesnocancel" ; fi
 
448
            if [ "$TYPE" = "--error" ] ; then
 
449
                "$DIALOG" --msgbox "Error: $*" 0 0
 
450
            elif [ "$TYPE" = "--sorry " ] ; then
 
451
                "$DIALOG" --msgbox "Sorry: $*" 0 0
 
452
            elif [ "$TYPE" = "--message" ] ; then
 
453
                "$DIALOG" --msgbox "$*" 0 0
 
454
            elif [ "$TYPE" = "--yesno" ] ; then
 
455
                "$DIALOG" --yesno "$*" 0 0
 
456
                if [ $RET -eq 255 ] ; then RET=1 ; fi
 
457
            elif [ "$TYPE" = "--yesnocancel" ] ; then
 
458
                "$DIALOG" --extra-button --ok-label Yes --extra-label No --no-label Cancel --yesno "$*" 0 0
 
459
                if [ $RET -eq 1 ] ; then RET=2 
 
460
                elif [ $RET -eq 2 ] ; then RET=1
 
461
                elif [ $RET -eq 255 ] ; then RET=2 
 
462
                fi
 
463
            elif [ "$TYPE" = "--input" ] ; then
 
464
                "$DIALOG" --inputbox "$1" 0 0 "$2" 2>"$TMPFILE"
 
465
                RET="$?"
 
466
                SOMEDIALOG=`cat "$TMPFILE"`
 
467
                if [ $RET -eq 255 ] ; then RET=1 ; fi
 
468
            fi
 
469
        elif [ -x "$WHIPTAIL" ] ; then
 
470
            # whiptail doesn't handle 0 0 as nicely as dialog, so it
 
471
            # isn't preferred.
 
472
            if [ "$TYPE" = "--yesNOcancel" ] ; then TYPE="--yesnocancel" ; fi
 
473
            if [ "$TYPE" = "--error" ] ; then
 
474
                "$WHIPTAIL" --msgbox "Error: $*" 0 0
 
475
            elif [ "$TYPE" = "--sorry " ] ; then
 
476
                "$WHIPTAIL" --msgbox "Sorry: $*" 0 0
 
477
            elif [ "$TYPE" = "--message" ] ; then
 
478
                "$WHIPTAIL" --msgbox "$*" 0 0
 
479
            elif [ "$TYPE" = "--yesno" ] ; then
 
480
                "$WHIPTAIL" --yesno "$*" 0 0
 
481
                RET="$?"
 
482
                if [ $RET -eq 255 ] ; then RET=1 ; fi
 
483
            elif [ "$TYPE" = "--yesnocancel" ] ; then
 
484
                "$WHIPTAIL" --yesno "$* (ESC to cancel)" 0 0
 
485
                RET="$?"
 
486
                if [ $RET -eq 255 ] ; then RET=2 ; fi
 
487
            elif [ "$TYPE" = "--input" ] ; then
 
488
                "$WHIPTAIL" --inputbox "$1" 0 0 "$2" 2>"$TMPFILE"
 
489
                RET="$?"
 
490
                SOMEDIALOG=`cat "$TMPFILE"`
 
491
                if [ $RET -eq 255 ] ; then RET=1 ; fi
 
492
            fi
 
493
        elif [ -x "$TCDIALOG" ] ; then
 
494
            # tcdialog requires the height/width values
 
495
            # as such, it is depricated
 
496
            if [ "$TYPE" = "--yesNOcancel" ] ; then TYPE="--yesnocancel" ; fi
 
497
            if [ "$TYPE" = "--error" ] ; then
 
498
                "$TCDIALOG" --msgbox "Error: $*" 20 60
 
499
            elif [ "$TYPE" = "--sorry " ] ; then
 
500
                "$TCDIALOG" --msgbox "Sorry: $*" 20 60
 
501
            elif [ "$TYPE" = "--message" ] ; then
 
502
                "$TCDIALOG" --msgbox "$*" 20 60
 
503
            elif [ "$TYPE" = "--yesno" ] ; then
 
504
                "$TCDIALOG" --yesno "$*" 20 60
 
505
                RET="$?"
 
506
                if [ $RET -eq 255 ] ; then RET=1 ; fi
 
507
            elif [ "$TYPE" = "--yesnocancel" ] ; then
 
508
                "$TCDIALOG" --yesno "$* (ESC to cancel)" 20 60
 
509
                RET="$?"
 
510
                if [ $RET -eq 255 ] ; then RET=2 ; fi
 
511
            elif [ "$TYPE" = "--input" ] ; then
 
512
                "$TCDIALOG" --inputbox "$1" 20 60 "$2" 2>"$TMPFILE"
 
513
                RET="$?"
 
514
                SOMEDIALOG=`cat "$TMPFILE"`
 
515
                if [ $RET -eq 255 ] ; then RET=1 ; fi
 
516
            fi
 
517
        else
 
518
            if [ "$TYPE" = "--error" ] ; then
 
519
                echo "Error: $*"
 
520
                read -p "Press <Enter> to continue." RET
 
521
                RET=0
 
522
                if [ $TTY_ABORT -eq 1 ] ; then RET=1 ; fi
 
523
            elif [ "$TYPE" = "--sorry " ] ; then
 
524
                echo "Sorry: $*"
 
525
                read -p "Press <Enter> to continue." RET
 
526
                RET=0
 
527
                if [ $TTY_ABORT -eq 1 ] ; then RET=1 ; fi
 
528
            elif [ "$TYPE" = "--message" ] ; then
 
529
                echo "Note: $*"
 
530
                read -p "Press <Enter> to continue." RET
 
531
                RET=0
 
532
                if [ $TTY_ABORT -eq 1 ] ; then RET=1 ; fi
 
533
            elif [ "$TYPE" = "--yesno" ] ; then
 
534
                read -r -e -p "$* (y/N)?" RET
 
535
                case "$RET" in
 
536
                    [yY]|[yY][eE][sS]) RET=0 ;;
 
537
                    *) RET=1 ;;
 
538
                esac
 
539
                if [ $TTY_ABORT -eq 1 ] ; then RET=1 ; fi
 
540
            elif [ "$TYPE" = "--yesNOcancel" ] ; then
 
541
                read -r -e -p "$* (yes/NO/cancel):" RET
 
542
                if [ -z "$RET" ] ; then RET="n" ; fi
 
543
                case "$RET" in
 
544
                    [yY]|[yY][eE][sS]) RET=0 ;;
 
545
                    [nN]|[nN][oO]) RET=1 ;;
 
546
                    *) RET=2 ;;
 
547
                esac
 
548
                if [ $TTY_ABORT -eq 1 ] ; then RET=2 ; fi
 
549
            elif [ "$TYPE" = "--yesnocancel" ] ; then
 
550
                read -r -e -p "$* (yes/no/CANCEL):" RET
 
551
                case "$RET" in
 
552
                    [yY]|[yY][eE][sS]) RET=0 ;;
 
553
                    [nN]|[nN][oO]) RET=1 ;;
 
554
                    *) RET=2 ;;
 
555
                esac
 
556
                if [ $TTY_ABORT -eq 1 ] ; then RET=2 ; fi
 
557
            elif [ "$TYPE" = "--input" ] ; then
 
558
                read -r -e -p "$1 ($2): " SOMEDIALOG
 
559
                if [ -z "$SOMEDIALOG" ] ; then
 
560
                    SOMEDIALOG="$2"
 
561
                fi
 
562
                RET=$TTY_ABORT
 
563
            fi
 
564
        fi
 
565
        # trap - SIGINT
 
566
    else
 
567
        RET=0
 
568
        if [ "$TYPE" = "--error" ] ; then
 
569
            echo "Error: $*"
 
570
        elif [ "$TYPE" = "--sorry " ] ; then
 
571
            echo "Sorry: $*"
 
572
        elif [ "$TYPE" = "--message" ] ; then
 
573
            echo "Message: $*"
 
574
        elif [ "$TYPE" = "--yesno" ] ; then
 
575
            echo "Yes/No: $* (defaults to NO)"
 
576
            RET=1
 
577
        elif [ "$TYPE" = "--yesNOcancel" ] ; then
 
578
            echo "Yes/No/Cancel: $* (defaults to NO)"
 
579
            RET=1
 
580
        elif [ "$TYPE" = "--yesnocancel" ] ; then
 
581
            echo "Yes/No/Cancel: $* (defaults to CANCEL)"
 
582
            RET=2
 
583
        elif [ "$TYPE" = "--input" ] ; then
 
584
            echo "Input: $1 (defaults to $2)"
 
585
            SOMEDIALOG="$2"
 
586
        fi
 
587
    fi
 
588
    return $RET
 
589
}
 
590
 
 
591
showversion() {
 
592
    # Version history:
 
593
    # 0.1 - (yam655) first version
 
594
    # 0.1.1 (2009-05-08):
 
595
    #   fix issue with feed://somewhere/...
 
596
    #   genre/title dialog order swap (as seen in 0.2 line)
 
597
    # 0.2 - (mostly aborted) added ability to run podget afterwards
 
598
    # 0.3 - added inactivity tag support
 
599
    echo "podget-add-feed Version 0.3"
 
600
}
 
601
 
 
602
showhelp() {
 
603
    cat << FEOF
 
604
Usage: $0 {Options} <URL>
 
605
Options may include:
 
606
    -h | --help : show this help message
 
607
    -V | --version : show the version
 
608
    -c | --check : check dialog compatibility
 
609
    -a | --auto : run in non-interactive mode
 
610
    -d | --dialog <dialog> : use a specific dialog program
 
611
FEOF
 
612
}
 
613
 
 
614
checkdialog
 
615
TMPFILE=`tempfile -p paf.`
 
616
if [ -z "$TMPFILE" ] ; then TMPFILE=`mktemp -t addfeed.XXXXXXXXXX` ; fi
 
617
if [ -z "$TMPFILE" ] ; then TMPFILE="/tmp/podget-add-feed.$$.`hostname`" ; fi
 
618
 
 
619
while [ $# -gt 0 ] ; do
 
620
    case "$1" in
 
621
        --check|-c)
 
622
            dialogcheck -v
 
623
            exit 0
 
624
            ;;
 
625
        --help|-h)
 
626
            showhelp
 
627
            exit 0
 
628
            ;;
 
629
        --version|-V)
 
630
            showversion
 
631
            exit 0
 
632
            ;;
 
633
        --auto|-a)
 
634
            unset DISPLAY
 
635
            DIALOG="auto"
 
636
            shift
 
637
            continue
 
638
            ;;
 
639
        --dialog|-d)
 
640
            shift
 
641
            checkdialog "$1"
 
642
            dialogcheck
 
643
            shift
 
644
            continue
 
645
            ;;
 
646
    esac
 
647
    URL="$1"
 
648
    shift
 
649
    CUR_GROUP="$EMPTY_GROUP"
 
650
    CUR_TITLE=""
 
651
    CUR_ACTIVATE=0
 
652
 
 
653
    # Interestingly, the body of the single-quoted segments is getting
 
654
    # parsed.
 
655
    URL=`sed -e 's@^feed://@http://@' -e 's@^feed:\([a-z]\+:\)@\1@' \
 
656
             -e 's/ /%20/g' -e 's/|/%7C/g' -e 's/\\\\/%5C/g' \
 
657
             -e 's/\\$/%24/g' -e 's/"/%34/g' -e "s/'/%39/g" \
 
658
            <<< "$URL"`
 
659
    case "$URL" in
 
660
        http:*)
 
661
            ;;
 
662
        https:*)
 
663
            ;;
 
664
        *)
 
665
            somedialog --error "Unknown protocol in URL: $URL"
 
666
            continue
 
667
            ;;
 
668
    esac
 
669
    FOUNDURL=`grep -e "^\(#[a-zA-Z0-9]* \)\?$URL " "$SERVERLIST"`
 
670
    FURL=""
 
671
    FGROUP=""
 
672
    FTITLE=""
 
673
    FACTIVE="inactive"
 
674
    FREASON=""
 
675
    if [ ! -z "$FOUNDURL" ] ; then
 
676
        case "$FOUNDURL" in
 
677
            \#*)
 
678
                read FACTIVE FURL FGROUP FTITLE <<< "$FOUNDURL"
 
679
                if [ "$FACTIVE" != "#" ] ; then
 
680
                    FREASON=" (${FACTIVE:1})"
 
681
                fi
 
682
                FACTIVE="inactive"
 
683
                ;;
 
684
            *)
 
685
                read FURL FGROUP FTITLE <<< "$FOUNDURL"
 
686
                FACTIVE="active"
 
687
                ;;
 
688
        esac
 
689
        if ! somedialog --yesnocancel "Found <$FURL> in $FGROUP as \"$FTITLE\". It is $FACTIVE$FREASON. Change this?" ; then
 
690
            continue
 
691
        fi
 
692
    fi
 
693
    if ! askquestions "$URL" "$FGROUP" "$FTITLE" ; then
 
694
        continue
 
695
    fi
 
696
    ACTIVE=""
 
697
    if [ $CUR_ACTIVATE -ne 0 ] ; then
 
698
        ACTIVE='# '
 
699
    fi
 
700
    if [ -z "$FOUNDURL" ] ; then
 
701
        echo "$ACTIVE$URL $CUR_GROUP $CUR_TITLE" >> "$SERVERLIST"
 
702
    else
 
703
        OLIBDIR="$LIBRARY_DIR/$FGROUP/$FTITLE"
 
704
        NLIBDIR="$LIBRARY_DIR/$CUR_GROUP/$CUR_TITLE"
 
705
        sed -e 's|^\(# \)\?'"$URL"' .*$|'"$ACTIVE$URL $CUR_GROUP $CUR_TITLE"'|g' "$SERVERLIST" > "$SERVERLIST".new
 
706
        mv "$SERVERLIST" "$SERVERLIST~"
 
707
        mv "$SERVERLIST".new "$SERVERLIST"
 
708
        if [ -d "$OLIBDIR" ] ; then
 
709
            if [ ! -d "$LIBRARY_DIR/$CUR_GROUP" ] ; then
 
710
                mkdir -p "$LIBRARY_DIR/$CUR_GROUP"
 
711
            fi
 
712
            if [ -d "$NLIBDIR" ] ; then
 
713
                somedialog --sorry "Directory $NLIBDIR already existed. We could not move $OLIBDIR. You will need to move the files by hand."
 
714
            else
 
715
                mv -f "$OLIBDIR" "$NLIBDIR"
 
716
            fi
 
717
        fi
 
718
        # if [ -z "$ACTIVE" -a "$FACTIVE" != "active" ] ; then
 
719
            # if somedialog --yesno "Run podget now?" ; then
 
720
                # podget
 
721
            # fi
 
722
        # fi
 
723
    fi
 
724
done
 
725