~donadigo/power-installer/power-installer-legacy

« back to all changes in this revision

Viewing changes to scripts/user-input.sh

  • Committer: donadigo
  • Date: 2014-11-21 23:22:11 UTC
  • Revision ID: donadigos159@gmail.com-20141121232211-m6duy1qufzbuljg0
Initial, fix merged branches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!bin/bash
 
2
# part of the power installer application
 
3
 
 
4
# reset all variables
 
5
LINK=""
 
6
LINKSUCCESS=""
 
7
linesvalue=""
 
8
type=""
 
9
method=""
 
10
result=""
 
11
file=""
 
12
 
 
13
# input sent from vala
 
14
ppa="$1"
 
15
pkg="$2"
 
16
input="$3"
 
17
link="$4"
 
18
 
 
19
option="$5"
 
20
 
 
21
installed="$6"
 
22
 
 
23
# temporary name
 
24
tmpname="$(mktemp -d /dev/shm/.power-installer.XXXXXX)"
 
25
 
 
26
# if input equals something
 
27
if [ "$input$ppa$pkg" != "" ]; then
 
28
    cd $tmpname
 
29
 
 
30
    # open session
 
31
    echo -e "\nUSER SESSION: OPEN\n"
 
32
 
 
33
        # load all the functions from "functions.sh"
 
34
    source "/usr/share/power-installer/functions.sh"
 
35
 
 
36
    # launch all as sudo // this function was written origrinally by Luca Borrione
 
37
    exesudo() {
 
38
        local _funcname_="$1"
 
39
 
 
40
        params=("$@")
 
41
        tmpfile="/dev/shm/$RANDOM.sh"
 
42
        local filecontent
 
43
        local regex
 
44
        local func
 
45
 
 
46
        unset params[0]
 
47
 
 
48
        content="#!bin/bash
 
49
        tmpname='"$tmpname"'
 
50
            installed='"$installed"'
 
51
        sendfile="$tmpname/send-success.sh"
 
52
        params=(\n"
 
53
 
 
54
        regex="\s+"
 
55
        for param in "${params[@]}"; do
 
56
            if [[ $param =~ $regex ]]; then
 
57
                content="${content}\t\"${param}\"\n"
 
58
            else
 
59
                content="${content}\t${param}\n"
 
60
            fi
 
61
        done
 
62
 
 
63
        content="$content)\n"
 
64
        echo -e "$content" > "$tmpfile"
 
65
 
 
66
        echo "#$(type "$_funcname_")" >> "$tmpfile"
 
67
        echo -e "\n$_funcname_ \"\${params[@]}\"\n" >> "$tmpfile"
 
68
 
 
69
        # using "sudo -Si" prevents errors from any commands that cannot use sudo
 
70
        ep | sudo -Si bash "$tmpfile"
 
71
        ep | sudo -S rm -f "$tmpfile"
 
72
    }
 
73
 
 
74
 
 
75
    # create environment
 
76
    file="user-execute.sh"
 
77
    echo "#!bin/bash"                                                   >> $file
 
78
    echo "# generated by power installer"                               >> $file
 
79
 
 
80
    echo 'source "/usr/share/power-installer/functions.sh"'             >> $file
 
81
 
 
82
    echo  tmpname="'$tmpname'"                                          >> $file # without quotes, they have been breaking the string, transfer $tmpname to script
 
83
    echo 'sendfile="$tmpname"/send-success.sh'                          >> $file
 
84
 
 
85
    # start ppa_pkg
 
86
    echo "ppa_pkg() {"                                                  >> $file
 
87
    echo  ppa="'$ppa'"                                                  >> $file # without quotes, they have been breaking entire ppa list
 
88
    echo  pkg="'$pkg'"                                                  >> $file # without quotes, they have been breaking entire package list
 
89
    echo 'if [ "$ppa" != "" ]; then'                                    >> $file
 
90
 
 
91
    # another quotes around ppa variable to prevent fail from "deb ..." repositories
 
92
    echo  ppa='"$ppa"'                                                  >> $file
 
93
 
 
94
    echo 'for all in "$ppa"; do'                                        >> $file
 
95
        echo 'if [ "$installed" = "true" ]; then'                                                   >> $file
 
96
        echo 'ep | sudo -S add-apt-repository -y --remove "$all"'           >> $file
 
97
        echo "else"                                                                                                                 >> $file
 
98
    echo 'ep | sudo -S add-apt-repository -y "$all"'                    >> $file
 
99
    echo "fi"                                                           >> $file
 
100
    echo "done"                                                         >> $file
 
101
    echo 'ep | sudo -S apt-get update'                                  >> $file
 
102
    echo 'if [ "$?" -eq "0" ]; then'                                    >> $file
 
103
    echo 'echo "export PPASUCCESS=1" >> $sendfile'                      >> $file
 
104
    echo "else"                                                         >> $file
 
105
    echo 'echo "export PPASUCCESS=0" >> $sendfile'                      >> $file
 
106
    echo "fi"                                                           >> $file
 
107
    echo "fi"                                                           >> $file
 
108
 
 
109
    echo 'if [ "$pkg" != "" ]; then'                                    >> $file
 
110
        echo 'if [ "$installed" = "true" ]; then'                                                   >> $file
 
111
        echo 'ep | sudo -S apt-get remove -y $pkg'                                          >> $file
 
112
        echo "else"                                                                                                                 >> $file
 
113
    echo 'ep | sudo -S apt-get install -y $pkg'                         >> $file
 
114
        echo "fi"                                                                                                                   >> $file
 
115
    echo 'if [ "$?" -eq "0" ]; then'                                    >> $file
 
116
    echo 'echo "export PKGSUCCESS=1" >> $sendfile'                      >> $file
 
117
    echo "else"                                                         >> $file
 
118
    echo 'echo "export PKGSUCCESS=0" >> $sendfile'                      >> $file
 
119
    echo "fi"                                                           >> $file
 
120
    echo "fi"                                                           >> $file
 
121
    echo "}"                                                            >> $file
 
122
 
 
123
    # start execute
 
124
    echo "execute() {"                                                  >> $file
 
125
    echo 'if [ "$tmpname" = "" ]; then'                                 >> $file
 
126
    echo 'tmpname="/dev/null/"'                                         >> $file
 
127
    echo "fi"                                                           >> $file
 
128
    echo "input='$input'"                                               >> $file
 
129
    echo 'if [ "$input" = "" ]; then'                                   >> $file
 
130
    echo "exit"                                                         >> $file
 
131
    echo "fi"                                                           >> $file
 
132
    echo "link='$link'"                                                 >> $file
 
133
    echo 'if [ "$link" = "true" ]; then'                                >> $file
 
134
    echo 'cd $tmpname'                                                  >> $file
 
135
    echo 'echo "export LINK=1" >> $sendfile'                            >> $file
 
136
    echo "mkdir downloaded"                                             >> $file
 
137
    echo "cd downloaded"                                                >> $file
 
138
    echo 'ep | wget "$input"'                                           >> $file # the file needs to be downloaded as sudo, otherwise it downloads with ".1" extension
 
139
    echo "if ! ls * &>/dev/null; then"                                  >> $file
 
140
    echo "cd .."                                                        >> $file
 
141
    echo 'echo "export LINKSUCCESS=0" >> $sendfile'                     >> $file
 
142
    echo "cd downloaded"                                                >> $file
 
143
    echo "fi"                                                           >> $file
 
144
    echo "cd .."                                                        >> $file
 
145
    echo "exit"                                                         >> $file
 
146
    echo "else"                                                         >> $file
 
147
    echo 'echo "export LINK=0" >> $sendfile'                            >> $file
 
148
    echo 'cd $tmpname'                                                  >> $file
 
149
 
 
150
    # confirm: echo "y" every 2 seconds
 
151
    echo "confirm() {"                                                  >> $file
 
152
    echo "while true; do"                                               >> $file
 
153
    echo "sleep 2"                                                      >> $file
 
154
    echo 'echo "y"'                                                     >> $file
 
155
    echo "done"                                                         >> $file
 
156
    echo "}"                                                            >> $file
 
157
 
 
158
    # put commands in the file (function)
 
159
    # since we are on root account we need to export standard HOME and USER variables
 
160
    echo "cmd() {"                                                      >> $file
 
161
    echo "HOME=$HOME"                                                   >> $file
 
162
    echo 'cd $HOME/'                                                    >> $file
 
163
    echo "USER=$USER"                                                   >> $file
 
164
    echo "$input"                                                       >> $file
 
165
    echo "}"                                                            >> $file
 
166
 
 
167
    # echo "y" (yes) and execute commands
 
168
    echo "confirm | cmd"                                                >> $file
 
169
 
 
170
    echo 'if [ "$?" -eq "0" ]; then'                                    >> $file
 
171
    echo 'echo "export SUCCESS=1" >> $sendfile'                         >> $file
 
172
    echo "else"                                                         >> $file
 
173
    echo 'echo "export SUCCESS=0" >> $sendfile'                         >> $file
 
174
    echo "killall zenity >/dev/null 2>&1"                               >> $file
 
175
    echo "fi"                                                           >> $file
 
176
    echo "fi"                                                           >> $file
 
177
    echo "exit"                                                         >> $file
 
178
    echo "}"                                                            >> $file
 
179
 
 
180
    source $file # using source instead of bash can load all variables to this file
 
181
 
 
182
    # execute all functions
 
183
    main_pass "ep | exesudo ppa_pkg; ep | exesudo execute"
 
184
 
 
185
# workaround for recognizing files
 
186
if [ "$link" = "true" ]; then
 
187
    cd "$tmpname/downloaded/"
 
188
    dfile="$(ls)"
 
189
    fullpath="$tmpname/downloaded/$dfile"
 
190
    EXT="${dfile##*.}"
 
191
    general_path="/usr/share/power-installer/general.sh"
 
192
 
 
193
    if [ "$option" = "1" ]; then
 
194
        EXT="deb"
 
195
        option="0"
 
196
    elif [ "$option" = "2" ]; then
 
197
        bash $general_path "$fullpath" "true" "false" "false" "false"
 
198
        exit
 
199
    elif [ "$option" = "3" ]; then
 
200
        bash $general_path "$fullpath" "false" "true" "false" "false"
 
201
        exit
 
202
    elif [ "$option" = "4" ]; then
 
203
        bash $general_path "$fullpath" "false" "false" "true" "false"
 
204
        exit
 
205
    fi
 
206
 
 
207
    if [ "$option" = "0" ]; then
 
208
        check_support "$EXT"
 
209
 
 
210
        # deb file
 
211
        if [ "$EXT" == "deb" ]; then
 
212
            debexe
 
213
            debresult="$(echo '$?')"
 
214
            type="debian"
 
215
            method="installed"
 
216
 
 
217
            if [ "$debresult" = "0" ]; then
 
218
                LINKSUCCESS="1"
 
219
            else
 
220
                sudo -S apt-get install -y -f
 
221
                debexe
 
222
                if [ "$?" -eq "0" ]; then
 
223
                    LINKSUCCESS="1"
 
224
                else
 
225
                    LINKSUCCESS="0"
 
226
                fi
 
227
            fi
 
228
 
 
229
        # shell file
 
230
        elif [ "$EXT" == "sh" ]; then
 
231
            script_launch "bash $dfile"
 
232
 
 
233
            type="shell script"
 
234
            method="executed"
 
235
            shconstant="1"
 
236
 
 
237
        elif [ "$EXT" == "run" ]; then
 
238
            script_launch "sudo -S bash $dfile"
 
239
 
 
240
            type="run"
 
241
            method="executed"
 
242
            runconstant="1"
 
243
 
 
244
        # python file
 
245
        elif [ "$EXT" == "py" ]; then
 
246
            script_launch "python $dfile"
 
247
 
 
248
            type="python script"
 
249
            method="executed"
 
250
            pyconstant="1"
 
251
        fi
 
252
    fi
 
253
 
 
254
    allconst="$shconstant$runconstant$pyconstant"
 
255
    if [[ $allconst == *1* ]]; then
 
256
        LINKSUCCESS="1"
 
257
    fi
 
258
fi
 
259
 
 
260
# session is closed
 
261
echo -e "\nUSER SESSION: CLOSE\n"
 
262
 
 
263
# notify without sudo using external file
 
264
if [ -f "$sendfile" ]; then
 
265
    source "$sendfile"
 
266
 
 
267
        if [ "$installed" = "true" ]; then
 
268
                methodppa="removed"
 
269
                methodpkg="$methodppa"
 
270
        else
 
271
                methodppa="added"
 
272
                methodpkg="installed"
 
273
        fi
 
274
 
 
275
    if [ "$LINK" = "1" ]; then
 
276
        if [ "$LINKSUCCESS" = "1" ]; then
 
277
            notify-send "Power Installer" "The $type (linked) file has been $method successfully!" $endmsg
 
278
        else
 
279
            notify-send "Power Installer" "Something went wrong. Correct the link and try again." $errmsg
 
280
        fi
 
281
    elif [ "$input" != "" ]; then
 
282
        if [ "$SUCCESS" = "1" ]; then
 
283
            notify-send "Power Installer" "All commands have been executed successfully!" $endmsg
 
284
        else
 
285
            notify-send "Power Installer" "Something went wrong. Check the syntax of your commands and try again." $errmsg
 
286
        fi
 
287
    fi
 
288
 
 
289
    if [ "$ppa" != "" ]; then
 
290
        if [ "$PPASUCCESS" = "1" ]; then
 
291
            notify-send "Power Installer" "Succesfully $methodppa PPA's: $ppa!" $endmsg
 
292
        else
 
293
            notify-send "Power Installer" "Something went wrong. Check the syntax of repostiory and try again." $errmsg
 
294
        fi
 
295
    fi
 
296
 
 
297
    if [ "$pkg" != "" ]; then
 
298
        if [ "$PKGSUCCESS" = "1" ]; then
 
299
            notify-send "Power Installer" "Succesfully $methodpkg packages: $pkg!" $endmsg
 
300
        else
 
301
            notify-send "Power Installer" "Something went wrong. Check name of the package and try again." $errmsg
 
302
        fi
 
303
    fi
 
304
else
 
305
    notify-send "Power Installer" "Sorry, but I couldn't detect status of executed commands!" $errmsg
 
306
    fi
 
307
fi
 
308
 
 
309
# remove tmp directory
 
310
rm -rf "$tmpname"
 
311
 
 
312
exit