Internet radio browser GUI for music/video streams from various directory services.

βŒˆβŒ‹ βŽ‡ branch:  streamtuner2


Check-in [94e2f12883]

Overview
Comment:Merged in new changes, made some defaults into script options. New funcs: Display-Logo, Warn-NonElevated, Ask-First, Console-MaxHeight, Create-UninstallScript, Any-Key, Check-Package. Some colorization fixes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:94e2f128837687ddd38762acb0ab6ac619e29d8d
User & Date: mario on 2016-11-24 02:07:54
Other Links: manifest | tags
Context
2016-11-25
00:03
Add uninstall.cmd / minor changes to win install scripts. check-in: 8740bd6fa0 user: mario tags: trunk
2016-11-24
02:07
Merged in new changes, made some defaults into script options. New funcs: Display-Logo, Warn-NonElevated, Ask-First, Console-MaxHeight, Create-UninstallScript, Any-Key, Check-Package. Some colorization fixes. check-in: 94e2f12883 user: mario tags: trunk
2016-11-23
00:58
Revert yelp-"fixes", as adding required <section>s actually broke the layout. check-in: a0a24d1a99 user: mario tags: trunk
Changes

Modified dev/install_python_gtk.ps1 from [c1e7d6826c] to [33023cddd1].

6
7
8
9
10
11
12


13
14
15
16
17

18
19
20
21
22



23
24
25
26
27
28
29
..
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
..
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
...
297
298
299
300
301
302
303
304
305
 #  - some python libraries
 #  - and runs their respective installers
 #  - crafts streamtuner2 desktop shortcut
 #  - crafts streamtuner2 shortcuts in Start menu
 #
 #>



[CmdletBinding()]            
Param()            


# default paths

$TEMP = $env:TEMP
$PYTHON = "C:\Python27"
$OutputEncoding = [System.Text.Encoding]::UTF8
$StartMenu = "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs"
$UsrFolder = $MyInvocation.MyCommand.Path -replace "([\\/][^\\/]+){4}$",""



$regPathCU = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
$regPathLM = "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
if(!(Test-Path $regPathLM)) { $regPathLM = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" }


#-- what and how to install
#   each row is a list of (title,url,cmd,msi args,regkey,reghive)
................................................................................
    "",
    ""
  ),
  @(
    "Python 2.7.12",                                                  # title
    "https://www.python.org/ftp/python/2.7.12/python-2.7.12.msi",     # url
    "",                                                               # custom cmd
    "TARGETDIR=C:\Python27 /qb",                                      # msi args
    "$regPathLM\{9DA28CE5-0AA5-429E-86D8-686ED898C665}",              # registry
    "$regPathLM\{9DA28CE5-0AA5-429E-86D8-686ED898C665}"               # installed check
  ),
  @(
    "PyGtk 2.24.2",
    "http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/2.24/pygtk-all-in-one-2.24.2.win32-py2.7.msi",
    "",
    "TARGETDIR=C:\Python27 ADDLOCAL=ALL REMOVE=PythonExtensionModulePyGtkSourceview2,PythonExtensionModulePyGoocanvas,PythonExtensionModulePyRsvg,DevelopmentTools /qb",
    "$regPathLM\{09F82967-D26B-48AC-830E-33191EC177C8}",
    "$regPathLM\{09F82967-D26B-48AC-830E-33191EC177C8}"
  ),
  @(
    "Python requests 2.12.1",
    "requests", # no download url, pip handles this
    "easy_install",
    "",
    "",
    "$PYTHON\Lib\site-packages\requests-2.12.1-py2.7.egg"
  ),
  @(
................................................................................
    "$regPathCU\lxml-py2.7",
    "$PYTHON\Lib\site-packages\lxml-2.3-py2.7.egg-info"
  ),
  @(
    "PyQuery 1.2.1",
    "https://pypi.python.org/packages/62/71/8ac1f5c0251e51714d20e4b102710d5eee1683c916616129552b0a025ba5/pyquery-1.2.17-py2.py3-none-any.whl",
    "pip",
    "",
    "",
    "$PYTHON\Lib\site-packages\pyquery-1.2.17.dist-info"
  ),
  @(
    "PIL 1.1.7",
    "http://effbot.org/downloads/PIL-1.1.7.win32-py2.7.exe",
    "",
    "",
    "$regPathCU\PIL-py2.7",
    "$PYTHON\Lib\site-packages\PIL"
  ),
  @(
    "ST2 relocatable start script patch",
    "",
    'Rewrite-Startscript',
    "",
    "",
    "$UsrFolder\bin\streamtuner2.bak"








  ),
  @(
    "Desktop shortcut",
    "",
    'Make-Shortcut -dir $Home\Desktop -name Streamtuner2.lnk -target $PYTHON\pythonw.exe -arg $UsrFolder\bin\streamtuner2',
    "",
    "",
    "$Home\Desktop\Streamtuner2.lnk"
  ),
  @(
    "Startmenu shortcut",
    "",
    'Make-Shortcut -dir "$StartMenu\Streamtuner2" -name Streamtuner2.lnk -target $PYTHON\pythonw.exe -arg $UsrFolder\bin\streamtuner2',
    "",
    "",
    ""
  ),
  @(
    "Startmenu help.chm",
    "",
    'Make-Shortcut -dir "$StartMenu\Streamtuner2" -name Help.lnk -target $UsrFolder\share\streamtuner2\help\help.chm -arg $false',
    "",
    "",
    ""
  ),
  @(
    "FINISHED", "", '$true', "", "", ""
  )
)






























































#-- create Desktop/Startmenu shortcuts
function Make-Shortcut {
    [CmdletBinding()]
    param($dir, $name, $target, $arg)
    if (!(Test-Path -Path $dir)) {
        New-Item -Path $dir -ItemType directory
    }
    $wsh = New-Object -ComObject WScript.Shell
    $lnk = $wsh.CreateShortcut("$dir\$name")
    $lnk.TargetPath = $target
    if ($arg) {
        $lnk.Arguments = """"$arg""""
        $lnk.IconLocation = "$UsrFolder\share\pixmaps\streamtuner2.ico"
        $lnk.WorkingDirectory = "$UsrFolder\bin"
    }
    $lnk.Save()
}





































#-- modify ST2 wrapper script
function Rewrite-Startscript {
    [CmdletBinding()]
    param()
    $WrapperScript = Get-Content -Path "$UsrFolder\bin\streamtuner2"
    for ($i=0; $i -lt $WrapperScript.Length ; $i++) {
        $WrapperScript[$i] = $WrapperScript[$i].Replace("sys.path.insert(0, " + """" + "/usr/","sys.path.insert(0, " + """" + $UsrFolder)
        Out-File -FilePath "$UsrFolder\bin\streamtuner2.new" -Encoding ascii -Append -InputObject $WrapperScript[$i]
    }

    if (Test-Path -Path "$UsrFolder\bin\streamtuner2.bak") {
        Remove-Item -Path "$UsrFolder\bin\streamtuner2.bak" -Force
    }
    Rename-Item -NewName "streamtuner2.bak" -Path "$UsrFolder\bin\streamtuner2" #save old script
    Rename-Item -NewName "streamtuner2" -Path "$UsrFolder\bin\streamtuner2.new"
    Set-Acl "$UsrFolder\bin\streamtuner2" (Get-Acl "$UsrFolder\bin\streamtuner2.bak") # set file permissions 
}

#-- admin check
function Test-IsElevated {
    [CmdletBinding()]
    param()
    [Security.Principal.WindowsPrincipal] $Identity = [Security.Principal.WindowsIdentity]::GetCurrent()
    $Identity.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)













}

#-- check prereqs installation
function Check-Prerequisites {
    [CmdletBinding()]
    param()
    Check-Package
    $result = 1
    ForEach ($task in $tasks) {
        $title, $url, $cmd, $args, $regkey, $check = $task;
        if ($regkey) { 
            if (!(Test-Path -Path $regkey)) {     # avoid runtime error if not existent (not working in PS1)
                Write-Host "   - $title not found"
                $result = 0
            }
            else {

                Write-Host "   + $title found"
            }
        }
        elseif ($title -match "requests|PyQuery") {
            if (!(Test-Path -Path $check)) {
                write-Host "   - $title not found"
                $result = 0
................................................................................
            }
            else {
                Write-Host "   + $title found"
            }
        }
    }
    if ($result) {
        Write-Host -f Green "`nAll required Python components are already installed. You can use 'none' or 'skip' or 'S' to skip them. Or just press ENTER on each following prompt. If you want to reinstall them though, use 'all' or 'reinstall' or 'R'.`n"
    }
}

#-- ensure ST2 startup script exists in relative path to this install script
function Check-Package {
    if (!(Test-Path -Path("$UsrFolder\bin\streamtuner2"))) {
        Write-Host -b DarkRed -f White @"
The Streamtuner2 start script could not be found. The installation cannot continue.
Please do not change the folder structure of the Streamtuner2 package!

[Press any key to exit ...]
"@
        $null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
        exit;
    }
}



#-- ask before running
Write-Host -f White -b Blue @"
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Do you want to install Python 2.7 and Gtk dependencies now?
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
"@

if(!(Test-IsElevated)) {
    Write-Host -f red @"
/---------------------------------------------------------------------------\
| (!)  Since you run this script in non-elevated mode you will not be able  |
|      to uninstall Python and Gtk using the control panelsΒ΄ software list. |
\---------------------------------------------------------------------------/
"@
}
if ((Read-Host "[Y/n]") -notmatch "[yYi123ar]|^$") {
    exit;
}



#-- process
$reinstall = "ask"
ForEach ($task in $tasks) {
    $title, $url, $cmd, $args, $regkey, $testpath = $task;

    # print step
    if ($title -match "\d+\.\d+") { $title = "Installing $title" }
    Write-Host -b DarkBlue "`n $title `n"
    chdir($TEMP);
    
    # test if element (file path or registry key) already exists:
    if ($reinstall -eq "all") {
    }
    elseif ($testpath -AND (Test-Path -Path $testpath)) {
        echo " β†’ Is already present."
        if ($reinstall -eq "none") { continue }
        $y = Read-Host "   Reinstall [y/N/all/none]?"
        if ($y -match "^all|always|re|^A") { $reinstall = "all" }
        elseif ($y -match "never|none|skip|^S") { $reinstall = "none"; continue }
        elseif ($y -match "^y|yes|1|go|^R") { } # YES
        else { continue } # everything else counts as NO
    }

    # get "filename" part from url
    $file = [regex]::match($url, "/([^/]+?)([\?\#]|$)").Groups[1].Value;

    # download
    if ($url -match "https?://.+") {
        Write-Host -f DarkGreen  " ← $url"
        $wget = New-Object System.Net.WebClient
        $wget.DownloadFile($url, "$TEMP\$file");
    }

    # run shorthand or custom command
    if ($cmd) {
        if (Test-Path $PYTHON) { chdir($PYTHON) }
        if ($cmd -eq "pip") {
            $cmd = "& $PYTHON\Scripts\pip.exe install $TEMP\$file"
        }
        elseif ($cmd -match "^(easy|easy_install|silent)$") {
             $cmd = "& $PYTHON\Scripts\easy_install.exe $url"
        }
        Write-Host -f DarkYellow   " β†’ $cmd"
        Invoke-Expression "$cmd"
    }
    # msi
    elseif ($file -match ".+.msi$") {
        write-host -f DarkYellow " β†’ msiexec /i $file $args"
        Start-Process -Wait msiexec -ArgumentList /i,"$TEMP\$file",$args
        if ($regkey) {
            Set-ItemProperty -Path "$regkey" -Name "WindowsInstaller"  -Value "0" -ErrorAction SilentlyContinue
        }
    }
    # exe
    elseif ($file -match ".+.exe$") {
................................................................................
    }
    # other files
    elseif ($file) {
      echo " β†’ $file"
      & "$TEMP\$file"
    }
}









>
>

|
<
<
|
>
|
|
<
|
|
>
>
>







 







|







|




|







 







|











|
|
|
|
|
|
|
>
>
>
>
>
>
>
>




|


|




|







|





|



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|







|






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







|
|







|




<
<

|
>
>
>
>
>
>
>
>
>
>
>
>
>





|

<








>







 







|
<
<
<
<
<
<
<
<
<
<
<
<
<
<




>

<
<
<
<
<
>
|
<
<
<
<
<
<
<
<
<
<
>



<












|

|










|









|









|







 








<
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
..
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
..
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
...
308
309
310
311
312
313
314
315














316
317
318
319
320
321





322
323










324
325
326
327

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
...
388
389
390
391
392
393
394
395

 #  - some python libraries
 #  - and runs their respective installers
 #  - crafts streamtuner2 desktop shortcut
 #  - crafts streamtuner2 shortcuts in Start menu
 #
 #>


#-- defaults / parameters
[CmdletBinding()]            
Param(


  [string]$reinstall = "ask",
  [switch]$keepdownloads = $false,
  [string]$TEMP = $env:TEMP,
  [string]$PYTHON = "C:\Python27",

  [string]$StartMenu = "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs",
  [string]$UsrFolder = $MyInvocation.MyCommand.Path -replace ("([\\/][^\\/]+){4}$","")
)            
$OutputEncoding = [System.Text.Encoding]::UTF8
$OSVersion = $env:OSVersion.Version
$regPathCU = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall"
$regPathLM = "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
if(!(Test-Path $regPathLM)) { $regPathLM = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall" }


#-- what and how to install
#   each row is a list of (title,url,cmd,msi args,regkey,reghive)
................................................................................
    "",
    ""
  ),
  @(
    "Python 2.7.12",                                                  # title
    "https://www.python.org/ftp/python/2.7.12/python-2.7.12.msi",     # url
    "",                                                               # custom cmd
    "TARGETDIR=C:\Python27 /qb-!",                                    # msi args
    "$regPathLM\{9DA28CE5-0AA5-429E-86D8-686ED898C665}",              # registry
    "$regPathLM\{9DA28CE5-0AA5-429E-86D8-686ED898C665}"               # installed check
  ),
  @(
    "PyGtk 2.24.2",
    "http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/2.24/pygtk-all-in-one-2.24.2.win32-py2.7.msi",
    "",
    "TARGETDIR=C:\Python27 ADDLOCAL=ALL REMOVE=PythonExtensionModulePyGtkSourceview2,PythonExtensionModulePyGoocanvas,PythonExtensionModulePyRsvg,DevelopmentTools /qb-!",
    "$regPathLM\{09F82967-D26B-48AC-830E-33191EC177C8}",
    "$regPathLM\{09F82967-D26B-48AC-830E-33191EC177C8}"
  ),
  @(
    "Python requests",
    "requests", # no download url, pip handles this
    "easy_install",
    "",
    "",
    "$PYTHON\Lib\site-packages\requests-2.12.1-py2.7.egg"
  ),
  @(
................................................................................
    "$regPathCU\lxml-py2.7",
    "$PYTHON\Lib\site-packages\lxml-2.3-py2.7.egg-info"
  ),
  @(
    "PyQuery 1.2.1",
    "https://pypi.python.org/packages/62/71/8ac1f5c0251e51714d20e4b102710d5eee1683c916616129552b0a025ba5/pyquery-1.2.17-py2.py3-none-any.whl",
    "pip",
    "--disable-pip-version-check",
    "",
    "$PYTHON\Lib\site-packages\pyquery-1.2.17.dist-info"
  ),
  @(
    "PIL 1.1.7",
    "http://effbot.org/downloads/PIL-1.1.7.win32-py2.7.exe",
    "",
    "",
    "$regPathCU\PIL-py2.7",
    "$PYTHON\Lib\site-packages\PIL"
  ),
  #@(
  #  "ST2 invocation (relocatability)",
  #  "",
  #  'Rewrite-Startscript',
  #  "",
  #  "",
  #  "$UsrFolder\bin\streamtuner2.bak"
  #),
  @(
    "Uninstall script",
    "",
    'Create-Uninstallscript',
    "",
    "",
    ""
  ),
  @(
    "Desktop shortcut",
    "",
    'Make-Shortcut -dir $Home\Desktop -name Streamtuner2.lnk -target $PYTHON\pythonw.exe -arg "$UsrFolder\bin\streamtuner2"',
    "",
    "",
    "" # always overwrite $Home\Desktop\Streamtuner2.lnk
  ),
  @(
    "Startmenu shortcut",
    "",
    'Make-Shortcut -dir "$StartMenu\Streamtuner2" -name Streamtuner2.lnk -target $PYTHON\pythonw.exe -arg "$UsrFolder\bin\streamtuner2"',
    "",
    "",
    ""
  ),
  @(
    "Startmenu help.chm",
    "",
    'Make-Shortcut -dir "$StartMenu\Streamtuner2" -name Help.lnk -target "$UsrFolder\share\streamtuner2\help\help.chm"',
    "",
    "",
    ""
  ),
  @(
    "FINISHED", "", 'Any-Key Green', "", "", ""
  )
)


#-- startup messages
function Display-Logo {
    Clear-Host
    Console-MaxHeight
    Write-Host -b DarkBlue @"
 _____________________________________________________________________________ 
|                                                                             |
|       _____/\\\\\\\\\\\____/\\\\\\\\\\\\\\\____/\\\\\\\\\_____              |
|        ___/\\\/////////\\\_\///////\\\/////___/\\\///////\\\___             |
|         __\//\\\______\///________\/\\\_______\///______\//\\\__            |
|          ___\////\\\_______________\/\\\_________________/\\\/___           |
|           ______\////\\\____________\/\\\______________/\\\//_____          |
|            _________\////\\\_________\/\\\___________/\\\//________         |
|             __/\\\______\//\\\________\/\\\_________/\\\/___________        |
|              _\///\\\\\\\\\\\/_________\/\\\________/\\\\\\\\\\\\\\\_       |
|               ___\///////////___________\///________\///////////////__      |
|                                                                             |
|                                                                             |
|    Streamtuner2 for Windows                               Version 2.2.0     |
|                                                                             |
|    Installer for Python 2.7.12 & Gtk 2.24.2                                 |
 ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– 
"@
}
function Warn-NonElevated {
    if (Test-IsElevated) {
        return
    }
    Write-Host ""
    Write-Host -f red " ___________________________________________________________________________"
    Write-Host -f red "|   If you run this script in non-elevated mode you will not be able to     |"
    Write-Host -f red "|               uninstall Streamtuner2, Python and Gtk                      |" 
    Write-Host -f red "|              using the control panelsΒ΄ software list.                     |"
    Write-Host -f red " ___________________________________________________________________________"
}
function Ask-First {
    Write-Host ""
    Write-Host -f Yellow "Do you want to install Python 2.7 and Gtk dependencies now?"
    Write-Host -f Yellow "–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––"
    Write-Host ""
    if ((Read-Host "[Y/n]") -notmatch "[yYi123ar]|^$") {
        exit;
        # or $tasks = $tasks[7..($tasks.length-1)]
    }
}
function Console-MaxHeight {
  if ($Host.Name -match "console") {
    $MaxHeight = $host.UI.RawUI.MaxPhysicalWindowSize.Height
    $MaxWidth = $host.UI.RawUI.MaxPhysicalWindowSize.Width
    $MyBuffer = $Host.UI.RawUI.BufferSize
    $MyWindow = $Host.UI.RawUI.WindowSize
    $MyWindow.Height = ($MaxHeight)
    #$MyWindow.Width = ($Maxwidth-2)
    $MyBuffer.Height = (9999)
    #$MyBuffer.Width = ($Maxwidth-2)
    $host.UI.RawUI.set_bufferSize($MyBuffer)
    $host.UI.RawUI.set_windowSize($MyWindow)
  }
}

#-- create Desktop/Startmenu shortcuts
function Make-Shortcut {
    [CmdletBinding()]
    param($dir, $name, $target, $arg=$false)
    if (!(Test-Path -Path $dir)) {
        New-Item -Path $dir -ItemType directory
    }
    $wsh = New-Object -ComObject WScript.Shell
    $lnk = $wsh.CreateShortcut("$dir\$name")
    $lnk.TargetPath = $target
    if ($arg) {
        $lnk.Arguments = '"'+$arg+'"'
        $lnk.IconLocation = "$UsrFolder\share\pixmaps\streamtuner2.ico"
        $lnk.WorkingDirectory = "$UsrFolder\bin"
    }
    $lnk.Save()
}

#-- create uninstall script and registry key
function Create-Uninstallscript {
    [CmdletBinding()]
    param()
    Write-Host " β†’ Creating uninstall script"
    $UninstallScriptPath = "$UsrFolder\share\streamtuner2\dev\uninstall.cmd"
    $installFolder = $usrFolder.substring(0,$usrFolder.LastIndexOf("\")) #"
    $UninstallScript = Get-Content -Path $UninstallScriptPath
    Out-File -FilePath $UninstallScriptPath -Encoding ascii -InputObject "@set installFolder=$installFolder"
    Out-File -FilePath $UninstallScriptPath -Encoding ascii -Append -InputObject "@set usrFolder=$usrFolder"
    Out-File -FilePath $UninstallScriptPath -Encoding ascii -Append -InputObject "@set Python=$PYTHON"
    for ($i=3; $i -lt $UninstallScript.Length ; $i++) {
        Out-File -FilePath $UninstallScriptPath -Encoding ascii -Append -InputObject $UninstallScript[$i]
    }
    Out-File -FilePath "$UsrFolder\share\streamtuner2\dev\Y" -Encoding ascii -InputObject "Y"
    if (Test-IsElevated) {
        Write-Host "   Writing registry"
        if (Test-Path $regPathLM\Streamtuner2) {
            Remove-Item -Path $regPathLM\Streamtuner2 | out-null        
        }
        New-Item $regPathLM -Name "Streamtuner2" | out-null
        Set-Location -Path $regPathLM\Streamtuner2
        New-ItemProperty -Path . -Name DisplayName -PropertyType String -Value "Streamtuner2" | out-null
        New-ItemProperty -Path . -Name DisplayVersion -PropertyType String -Value "2.2.0" | out-null
        New-ItemProperty -Path . -Name DisplayIcon -PropertyType String -Value "$UsrFolder\share\pixmaps\streamtuner2.ico" | out-null
        New-ItemProperty -Path . -Name UninstallString -PropertyType String -Value "$UsrFolder\share\streamtuner2\dev\uninstall.cmd" | out-null
        New-ItemProperty -Path . -Name URLInfoAbout -PropertyType String -Value "http://freshcode.club/projects/streamtuner2" | out-null
        New-ItemProperty -Path . -Name Publisher -PropertyType String -Value "Mario Salzer" | out-null
        New-ItemProperty -Path . -Name NoModify -PropertyType DWord -Value 1 | out-null
        New-ItemProperty -Path . -Name NoRepair -PropertyType DWord -Value 1 | out-null
    }
    if (!(([environment]::OSVersion.Version.Major -match 10) -AND ($name -match "Uninstall.lnk"))) {  # no Uninstall.lnk on Windows 10
        Make-Shortcut -dir "$StartMenu\Streamtuner2" -name Uninstall.lnk -target "$UsrFolder\share\streamtuner2\dev\uninstall.cmd"
    }    
}

#-- modify ST2 wrapper script
function Rewrite-Startscript {
    [CmdletBinding()]
    param()
    $WrapperScript = Get-Content -Path "$UsrFolder\bin\streamtuner2"
    for ($i=0; $i -lt $WrapperScript.Length ; $i++) {
        $WrapperScript[$i] = $WrapperScript[$i].Replace("sys.path.insert(0, " + """" + "/usr/","sys.path.insert(0, " + """" + $UsrFolder + "\\")
        Out-File -FilePath $UsrFolder"\bin\streamtuner2.new" -Encoding ascii -Append -InputObject $WrapperScript[$i]
    }

    if (Test-Path -Path "$UsrFolder\bin\streamtuner2.bak") {
        Remove-Item -Path "$UsrFolder\bin\streamtuner2.bak" -Force
    }
    Rename-Item -NewName "streamtuner2.bak" -Path "$UsrFolder\bin\streamtuner2" #save old script
    Rename-Item -NewName "streamtuner2" -Path "$UsrFolder\bin\streamtuner2.new"
    Set-Acl $UsrFolder"\bin\streamtuner2" (Get-Acl "$UsrFolder\bin\streamtuner2.bak") # set file permissions 
}

#-- admin check
function Test-IsElevated {


    [Security.Principal.WindowsPrincipal] $Identity = [Security.Principal.WindowsIdentity]::GetCurrent()
    return $Identity.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

function Any-Key($color) {
    Write-Host -f $color "[Press any key]"
    $null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

#-- ensure ST2 startup script exists in relative path to this install script
function Check-Package {
    if (!(Test-Path -Path("$UsrFolder\bin\streamtuner2"))) {
        Write-Host -b DarkRed -f White "The bin\streamtuner2 start script could not be found. The installation cannot continue. Do not change the folder structure of the Streamtuner2 package! If you want to run the install_python_gtk.ps1 script post-install, please use the -UsrFolder parameter."
        Any-Key Red ; exit
    }
}

#-- check prereqs installation
function Check-Prerequisites {
    [CmdletBinding()]
    param($result = 1)
    Check-Package

    ForEach ($task in $tasks) {
        $title, $url, $cmd, $args, $regkey, $check = $task;
        if ($regkey) { 
            if (!(Test-Path -Path $regkey)) {     # avoid runtime error if not existent (not working in PS1)
                Write-Host "   - $title not found"
                $result = 0
            }
            else {
               #Write-Host (Get-ItemProperty -Path $regkey -Name "DisplayName".DisplayName).DisplayName " found"
                Write-Host "   + $title found"
            }
        }
        elseif ($title -match "requests|PyQuery") {
            if (!(Test-Path -Path $check)) {
                write-Host "   - $title not found"
                $result = 0
................................................................................
            }
            else {
                Write-Host "   + $title found"
            }
        }
    }
    if ($result) {
        Write-Host -f Yellow "`nAll required Python components are already installed. You can use 'none' or 'skip' or 'S' to skip them. Or just press ENTER on each following prompt. If you want to reinstall them though, use 'all' or 'reinstall' or 'R'.`n"














    }
}



#-- ask before running





Display-Logo
Warn-NonElevated










Ask-First


#-- process

ForEach ($task in $tasks) {
    $title, $url, $cmd, $args, $regkey, $testpath = $task;

    # print step
    if ($title -match "\d+\.\d+") { $title = "Installing $title" }
    Write-Host -b DarkBlue "`n $title `n"
    chdir($TEMP);
    
    # test if element (file path or registry key) already exists:
    if ($reinstall -eq "all") {
    }
    elseif ($testpath -AND (Test-Path -Path $testpath)) {
        echo -f Yellow " β†’ Is already present."
        if ($reinstall -eq "none") { continue }
        Write-Host -f Yellow -NoNewline "   Reinstall [y/N/all/none]? " ; $y = Read-Host ; Write-Host ""
        if ($y -match "^all|always|re|^A") { $reinstall = "all" }
        elseif ($y -match "never|none|skip|^S") { $reinstall = "none"; continue }
        elseif ($y -match "^y|yes|1|go|^R") { } # YES
        else { continue } # everything else counts as NO
    }

    # get "filename" part from url
    $file = [regex]::match($url, "/([^/]+?)([\?\#]|$)").Groups[1].Value;

    # download
    if (($url -match "https?://.+") -AND ($keepdownloads -OR !(Test-Path "$TEMP\$file"))) {
        Write-Host -f DarkGreen  " ← $url"
        $wget = New-Object System.Net.WebClient
        $wget.DownloadFile($url, "$TEMP\$file");
    }

    # run shorthand or custom command
    if ($cmd) {
        if (Test-Path $PYTHON) { chdir($PYTHON) }
        if ($cmd -eq "pip") {
            $cmd = "& $PYTHON\Scripts\pip.exe install $TEMP\$file", $args
        }
        elseif ($cmd -match "^(easy|easy_install|silent)$") {
             $cmd = "& $PYTHON\Scripts\easy_install.exe $url"
        }
        Write-Host -f DarkYellow   " β†’ $cmd"
        Invoke-Expression "$cmd"
    }
    # msi
    elseif ($file -match ".+.msi$") {
        Write-Host -f DarkYellow " β†’ msiexec /i $file $args"
        Start-Process -Wait msiexec -ArgumentList /i,"$TEMP\$file",$args
        if ($regkey) {
            Set-ItemProperty -Path "$regkey" -Name "WindowsInstaller"  -Value "0" -ErrorAction SilentlyContinue
        }
    }
    # exe
    elseif ($file -match ".+.exe$") {
................................................................................
    }
    # other files
    elseif ($file) {
      echo " β†’ $file"
      & "$TEMP\$file"
    }
}