summaryrefslogtreecommitdiff
path: root/plugins/osdserver/file-browser.sh
blob: f5142afd94e3e04776e9c2788c6df22b6320a4db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
# file-browser.sh - Version 0.0.2
# A small file-browser for the vdr-osdserver

if [ "$*" != "0" ] && [ -x "$*" ] && [ -d "$*" ]; then
    directory=$(echo "$*" | sed s?//?/?g)
else
    directory="/"
fi
exit_cmd=
svdrpsend="svdrpsend.pl"

mkfifo --mode=700 /tmp/pipe-in$$ /tmp/pipe-out$$
exec 3<> /tmp/pipe-in$$
exec 4<> /tmp/pipe-out$$
rm /tmp/pipe-in$$ /tmp/pipe-out$$

{ netcat localhost 2010 ; echo 499 disconnected ; } <&3 >&4 &
pid=$!


function error() {
    SendCmd Quit

    exec 3>&-
    exec 4>&-

    kill $pid

    exit 1
}

function ReadReply() {
    reply2xx=()
    reply3xx=()
    reply4xx=()

    while read -r code line <&4 ; do
        echo "< $code $line"
        case $code in
            2*)     IFS=$' \t\n\r' reply2xx=($code "$line")
                    ;;
            3*)     IFS=$' \t\n\r' reply3xx=($code $line)
                    ;;
            4*)     IFS=$' \t\n\r' reply4xx=($code "$line")
                    ;;
        esac
        [ -n "${reply2xx[0]}" ] && break;
    done

    [ -n "${reply4xx[0]}" ] && return 1
    return 0
}

function SendCmd() {
    echo "> $*"
    echo "$*" >&3

    ReadReply
}

function FileBrowser() {
    n=1  # counter reset
    # menu
    SendCmd "menu=NewMenu \"File-Browser: $directory\"" || return 1
    if [ "$directory" != "/" ] ; then
        SendCmd "opt${n}=menu.AddOsdItem \".\"" || return 1
        ((n=n+1))
        SendCmd "opt${n}=menu.AddOsdItem \"..\"" || return 1
        ((n=n+1))
    fi
    files=$(ls -I $(basename $0) -B "$directory")
    if [ -n "$files" ] ; then
        for i in $files ; do
            SendCmd "opt$n=menu.AddOsdItem \"$i\"" || return 1
            ((n=n+1))
        done
    fi

    SendCmd 'menu.Show' || return 1
    SendCmd 'menu.EnableEvent keyOk close' || return 1
    SendCmd 'menu.SleepEvent' || return 1

    # reply
    if [ "${reply3xx[0]}" != "300" ] ; then return 1 ; fi

    if [ "${reply3xx[2]}" == "close" ] ; then
        if [ "$directory" != "/" ] ; then
            exit_cmd="$0 \"$(dirname "$directory")\""
        else
            SendCmd 'Message "Closing File-Browser ..."' || return 1
        fi

        SendCmd 'menu.SendState osEnd' || return 1
        return 0
    fi

    if [ "${reply3xx[2]}" != "keyOk" ] ; then return 1 ; fi

    SendCmd 'menu.GetCurrent' || return 1
    if [ "${reply3xx[0]}" != "302" ] ; then return 1 ; fi

    # options
    n=1
    if [ "$directory" != "/" ] ; then
        if [ "${reply3xx[2]}" == "opt${n}" ] ; then
            exit_cmd="$0 /"
            SendCmd 'menu.SendState osEnd' || return 1
            return 0
        fi
        ((n=n+1))

        if [ "${reply3xx[2]}" == "opt${n}" ] ; then
            exit_cmd="$0 \"$(dirname "$directory")\""
            SendCmd 'menu.SendState osEnd' || return 1
            return 0
        fi
        ((n=n+1))
    fi

    if [ -n "$files" ] ; then
        for i in $files ; do
            if [ "${reply3xx[2]}" == "opt${n}" ] ; then
                if [ -d "${directory}/${i}" ] ; then
                    exit_cmd="$0 \"${directory}/${i}\""
                    break
                elif [ -f "${directory}/${i}" ] && [ -x "${directory}/${i}" ] ; then
                    SendCmd "Message \"Executing $i ...\""
                    exit_cmd="${directory}/${i}"
                    break
                elif [ -f "${directory}/${i}" ] && [ -r "${directory}/${i}" ] ; then
                    SendCmd "Message \"Not supported: $i\""
#                    exit_cmd="${directory}/${i}"
                    break
                else
                    SendCmd "Message \"Not supported: $i\""
                    break
                fi
            else
                ((n=n+1))
            fi
        done
    fi

    SendCmd 'menu.SendState osEnd' || return 1
    return 0
}


ReadReply
SendCmd 'Version 0' || error

FileBrowser || error

SendCmd Quit

exec 3>&-
exec 4>&-

eval $exit_cmd