aboutsummaryrefslogtreecommitdiffstats
path: root/xenstore-editor-zh_TW.utf8.bash
blob: 12c451dcca12f3c91f0081f16cc2fa9d40b68c6b (plain) (blame)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/bin/bash
### 名稱: Simple XenStore Editor
### 版本: 1.2
### 發行日期: 2012-08-18

[ "`id -u`" != "0" ] && echo "這個 script 必須以 root 身份執行。" && exit 40

if [ "$1" ];then
    current="$1"
else
    current="/"
fi

should_exit=0
first_run=1
setdefault=0
prevdir="$current"
tmpfile=`mktemp`
scripttitle="               簡易 XenStore 編輯工具 版本 1.2               "
scriptshorttitle="簡易 XenStore 編輯工具"

if [ -z "$DIALOG" ]; then
    for i in dialog cdialog
    do
        DIALOG=`which $i 2> /dev/null`
        [ "$DIALOG" ] && break
    done
fi

[ -z "$DIALOG" ] && echo "在 PATH 中找不到必要的程式 dialog (可以嘗試用環境變數 DIALOG 來指定這個可執行檔的位置)" && exit 42

function getxenfullpath () {
    if [ "$1" = "/" ]
    then
        echo "/$2"
    else
        echo "$1/$2"
    fi
}

function getusernewvalue () {
    local initvalue="$3"
    while true
    do
        $DIALOG --title "$1" --extra-button --extra-label "檔案瀏覽器" --inputbox "$2" 0 0 "$initvalue" 2> "$tmpfile"
        local exitstat=$?
        case "$exitstat" in
            0|1)
                return "$exitstat"
                ;;
            3)
                local initdir="/"
                local tmpfile2="`mktemp`"
                local selectedfile="/"
                local nowvalue="`cat "$tmpfile"`"
                local parentdir="`dirname "$nowvalue"`"
                if [ "`echo "$nowvalue" | cut -c 1`" != "/" ] || [ '!' -e "$parentdir" ]
                then
                    $DIALOG --title "檔案瀏覽器" --msgbox "$parentdir 目錄不存在" 0 0
                    initdir="`pwd`/"
                else
                    initdir="$nowvalue"
                fi
                $DIALOG --title "請用空白鍵來複製游標所在位置的檔案路徑" --fselect "$initdir" 13 75 2> "$tmpfile2"
                if [ "$?" = "0" ]
                then
                    selectedfile="`cat "$tmpfile2"`"
                    initvalue="$selectedfile"
                else
                    initvalue="$nowvalue"
                fi
                rm -f "$tmpfile2"
                ;;
        esac
    done
}

while [ "$should_exit" = "0" ]
do
    unset dirlist
    unset valuelist
    unset i
    unset j
    xenstore-list "$current" > "$tmpfile"
    if [ "$?" != "0" ]; then
        $DIALOG --title "$scripttitle" --msgbox "無法從 $current 取得檔案清單" 0 0
        if [ "$first_run" != "0" ]
        then
            exit 1
        else
            current="$prevdir"
            continue
        fi
    fi
    first_run=0
    declare -a dirlist
    declare -a valuelist
    mapfile -t dirlist < "$tmpfile"
    declare -i i=0
    declare -i j=0
    if [ -z "${dirlist[0]}" ]
    then
        valuelist[0]="(Empty)"
        valuelist[1]=""
    else
        while [ "${dirlist[$i]}" ]
        do
            valuelist[$j]="${dirlist[$i]}"
            j=$j+1
            valuelist[$j]="`xenstore-read $current/${dirlist[$i]}`"
            j=$j+1
            i=$i+1
        done
    fi
    if [ "$setdefault" = "0" ]
    then
        $DIALOG --title "$scripttitle" --ok-label "切換目錄" --cancel-label "離開" --extra-button --extra-label "編輯" --menu "$current" 0 0 0 "${valuelist[@]}" 2> "$tmpfile"
    else
        $DIALOG --title "$scripttitle" --ok-label "切換目錄" --cancel-label "離開" --extra-button --extra-label "編輯" --default-item "$setdefaultvalue" --menu "$current" 0 0 0 "${valuelist[@]}" 2> "$tmpfile"
    fi
    dialogexit=$?
    dialogout="`cat "$tmpfile"`"
    setdefault=0
    case "$dialogexit" in
        0)
            unset valuepathvalid
            descending="`getxenfullpath "$current" "$dialogout"`"
            ascending="`dirname "$current"`"
            usevalue="`xenstore-read "$descending"`"
            if [ "$usevalue" ]; then
                xenstore-read "$usevalue" 2> /dev/null
                [ "$?" = "0" ] && valuepathvalid=1
            fi
            if [ "$dialogout" = "(Empty)" ]; then
                $DIALOG --title "$scriptshorttitle - 切換目錄" --menu "請從清單中選取" 0 0 0 "Back" "進入 $ascending" "Manual" "輸入 XenStore 路徑" 2> "$tmpfile"
                dialogexit=$?
            elif [ "$usevalue" ] &&  [ "$valuepathvalid" = "1" ]; then
                $DIALOG --title "$scriptshorttitle - 切換目錄" --menu "請從清單中選取" 0 0 0 "Enter" "進入 $descending" "Back" "進入 $ascending" "UseValue" "進入 $usevalue" "Manual" "輸入 XenStore 路徑" 2> "$tmpfile"
                dialogexit=$?
            else
                $DIALOG --title "$scriptshorttitle - 切換目錄" --menu "請從清單中選取" 0 0 0 "Enter" "進入 $descending" "Back" "進入 $ascending" "Manual" "輸入 XenStore 路徑" 2> "$tmpfile"
                dialogexit=$?
            fi
            if [ "$dialogexit" = "0" ]
            then
                dialogout2="`cat "$tmpfile"`"
                case "$dialogout2" in
                    "Enter")
                        prevdir="$current"
                        current="$descending"
                        ;;
                    "Back")
                        prevdir="$current"
                        current="$ascending"
                        ;;
                    "UseValue")
                        prevdir="$current"
                        current="$usevalue"
                        ;;
                    "Manual")
                        $DIALOG --title "$scriptshorttitle - 切換目錄 - Manual" --inputbox "XenStore 目錄名稱" 0 0 "$current" 2> "$tmpfile"
                        if [ "$?" = "0" ]
                        then
                            prevdir="$current"
                            current="`cat "$tmpfile"`"
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                        fi
                        ;;
                esac
            else
                setdefault=1
                setdefaultvalue="$dialogout"
            fi
            ;;
        1)
            if $DIALOG --title "$scriptshorttitle - 離開" --yesno "你確定要離開嗎?" 0 0
            then
                should_exit=1
            fi
            ;;
        3)
            if [ "$dialogout" = "(Empty)" ]
            then
                $DIALOG --title "$scriptshorttitle - 編輯" --menu "請從清單中選取" 0 0 0 "Add" "加入新值" 2> "$tmpfile"
                dialogexit=$?
                dialogout2="`cat "$tmpfile"`"
            else
                $DIALOG --title "$scriptshorttitle - 編輯" --menu "請從清單中選取" 0 0 0 "Modify" "修改此值" "Remove" "刪除此值" "Add" "加入新值" 2> "$tmpfile"
                dialogexit=$?
                dialogout2="`cat "$tmpfile"`"
            fi
            if [ "$dialogexit" = "0" ]
            then
                case "$dialogout2" in
                    "Add")
                        $DIALOG --title "$scriptshorttitle - 編輯 - Add" --inputbox "名稱" 0 0 2> "$tmpfile"
                        if [ "$?" = "0" ]
                        then
                            newname="`cat "$tmpfile"`"
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                            continue
                        fi
                        getusernewvalue "$scriptshorttitle - 編輯 - Add" "值" ""
                        if [ "$?" = "0" ]
                        then
                            newvalue="`cat "$tmpfile"`"
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                            continue
                        fi
                        fullpath="`getxenfullpath "$current" "$newname"`"
                        outmsg="`xenstore-write "$fullpath" "$newvalue" 2>&1`"
                        if [ "$?" != "0" ]
                        then
                            $DIALOG --title "$scriptshorttitle - 編輯 - Add" --msgbox "$outmsg" 0 0
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                        fi
                    ;;
                    "Modify")
                        fullpath="`getxenfullpath "$current" "$dialogout"`"                     
                        getusernewvalue "$scriptshorttitle - 編輯 - Modify" "值" "`xenstore-read "$fullpath"`"
                        if [ "$?" = "0" ]
                        then
                            dialogout3="`cat "$tmpfile"`"
                            outmsg="`xenstore-write "$fullpath" "$dialogout3" 2>&1`"
                            if [ "$?" != "0" ]
                            then
                                $DIALOG --title "$scriptshorttitle - 編輯 - Modify" --msgbox "$outmsg" 0 0
                            else
                                setdefault=1
                                setdefaultvalue="$dialogout"
                            fi
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                        fi
                    ;;
                    "Remove")
                        fullpath="`getxenfullpath "$current" "$dialogout"`"
                        if $DIALOG --title "$scriptshorttitle - 編輯 - Remove" --yesno "你確定要刪除 $dialogout 嗎?" 0 0
                        then
                            outmsg="`xenstore-rm "$fullpath" 2>&1`"
                            if [ "$?" != "0" ]
                            then
                                $DIALOG --title "$scriptshorttitle - 編輯 - Remove" --msgbox "$outmsg" 0 0
                            else
                                setdefault=1
                                setdefaultvalue="$dialogout"
                            fi
                        else
                            setdefault=1
                            setdefaultvalue="$dialogout"
                        fi
                    ;;
                esac
            else
                setdefault=1
                setdefaultvalue="$dialogout"
            fi
            ;;
    esac
done

rm -f "$tmpfile"