ytdlWrapper.psm1

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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
# Create module-wide variables.
$script:ModuleRoot = $PSScriptRoot
$script:ModuleVersion = (Import-PowerShellDataFile -Path "$ModuleRoot\ytdlWrapper.psd1").ModuleVersion
$script:Folder = "$env:APPDATA\Powershell\ytdlWrapper"
$script:TemplateData = "$env:APPDATA\Powershell\ytdlWrapper\template-database.$ModuleVersion.xml"
$script:JobData = "$env:APPDATA\Powershell\ytdlWrapper\job-database.$ModuleVersion.xml"

# For the debug output to be displayed, $DebugPreference must be set
# to 'Continue' within the current session.
Write-Debug "`e[4mMODULE-WIDE VARIABLES`e[0m"
Write-Debug "Module root folder: $ModuleRoot"
Write-Debug "Module version: $ModuleVersion"
Write-Debug "Template Database file: $TemplateData"
Write-Debug "Job Database file: $JobData"
Write-Debug "Data Folder: $Folder"

# Create the module data-storage folder if it doesn't exist.
if (-not (Test-Path -Path "$env:APPDATA\Powershell\ytdlWrapper" -ErrorAction Ignore))
{
    New-Item -ItemType Directory -Path "$env:APPDATA" -Name "Powershell\ytdlWrapper" -Force -ErrorAction Stop -WhatIf:$false -Confirm:$false
}
if (-not (Test-Path -Path "$Folder\Templates" -ErrorAction Ignore))
{
    New-Item -ItemType Directory -Path "$Folder" -Name "Templates" -Force -ErrorAction Stop -WhatIf:$false -Confirm:$false
}
if (-not (Test-Path -Path "$Folder\Jobs" -ErrorAction Ignore))
{
    New-Item -ItemType Directory -Path "$Folder" -Name "Jobs" -Force -ErrorAction Stop -WhatIf:$false -Confirm:$false
}
Write-Debug "Created database folders!"

if ($null -eq (Get-Command youtube-dl.exe -ErrorAction SilentlyContinue))
{
    Write-Error "The 'youtube-dl.exe' binary could not be found! Make sure the %PATH% variable has the location of the binary."
}

# Potentially force this module script to dot-source the files, rather than
# load them in an alternative method.
$doDotSource = $global:ModuleDebugDotSource
$doDotSource = $true # Needed to make code coverage tests work

function Resolve-Path_i
{
    <#
    .SYNOPSIS
        Resolves a path, gracefully handling a non-existent path.
         
    .DESCRIPTION
        Resolves a path into the full path. If the path is invalid,
        an empty string will be returned instead.
         
    .PARAMETER Path
        The path to resolve.
         
    .EXAMPLE
        PS C:\> Resolve-Path_i -Path "~\Desktop"
         
        Returns 'C:\Users\...\Desktop"
 
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [string]
        $Path
    )
    
    # Run the command, silencing errors.
    $resolvedPath = Resolve-Path -Path $Path -ErrorAction Ignore
    
    # If NULL, then just return an empty string.
    if ($null -eq $resolvedPath)
    {
        $resolvedPath = ""
    }
    
    Write-Output $resolvedPath
}
function Import-ModuleFile {
    <#
    .SYNOPSIS
        Loads files into the module on module import.
        Only used in the project development environment.
        In built module, compiled code is within this module file.
         
    .DESCRIPTION
        This helper function is used during module initialization.
        It should always be dot-sourced itself, in order to properly function.
         
    .PARAMETER Path
        The path to the file to load.
         
    .EXAMPLE
        PS C:\> . Import-ModuleFile -File $function.FullName
         
        Imports the code stored in the file $function according to import policy.
         
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]
        $Path
    )
    
    # Get the resolved path to avoid any cross-OS issues.
    $resolvedPath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($Path).ProviderPath
    
    if ($doDotSource)
    {
        # Load the file through dot-sourcing.
        . $resolvedPath    
        Write-Debug "Dot-sourcing file: $resolvedPath"
    }
    else
    {
        # Load the file through different method (unknown atm?).
        $ExecutionContext.InvokeCommand.InvokeScript($false, ([scriptblock]::Create([io.file]::ReadAllText($resolvedPath))), $null, $null) 
        Write-Debug "Importing file: $resolvedPath"
    }
}

# ISSUE WITH BUILT MODULE FILE
# ----------------------------
# If this module file contains the compiled code below, as this is a "packaged"
# build, then that code *must* be loaded, and you cannot individually import
# and of the code files, even if they are there.
#
#
# If this module file is built, then it contains the class definitions below,
# and on Import-Module, this file is AST analysed and those class definitions
# are read-in and loaded.
#
# It's only once a command is run that this module file is executed, and if at
# that point this file starts to individually import the project files, it will
# end up re-defining the classes, and apparently that seems to cause issues
# later down the line.
#
#
# Therefore to prevent this issue, if this module file has been built and it
# contains the compile code below, that code will be used, and nothing else.
#
# The build script should also not package the individual files, so that the
# *only* possibility is to load the compiled code below and there is no way
# the individual files can be imported, as they don't exist.


# If this module file contains the compiled code, import that, but if it
# doesn't, then import the individual files instead.
$importIndividualFiles = $false
if ("<was built>" -eq '<was not built>')
{
    $importIndividualFiles = $true
    Write-Debug "Module not built! Importing individual files."
}

Write-Debug "`e[4mIMPORT DECISION`e[0m"
Write-Debug "Dot-sourcing: $doDotSource"
Write-Debug "Importing individual files: $importIndividualFiles"

# If importing code as individual files, perform the importing.
# Otherwise, the compiled code below will be loaded.
if ($importIndividualFiles)
{
    Write-Debug "!IMPORTING INDIVIDUAL FILES!"
    
    # Execute Pre-import actions.
    . Import-ModuleFile -Path "$ModuleRoot\internal\preimport.ps1"
    
    # Import all internal functions.
    foreach ($file in (Get-ChildItem "$ModuleRoot\internal\functions" -Filter "*.ps1" -Recurse -ErrorAction Ignore))
    {
        . Import-ModuleFile -Path $file.FullName
    }
    
    # Import all public functions.
    foreach ($file in (Get-ChildItem "$ModuleRoot\functions" -Filter "*.ps1" -Recurse -ErrorAction Ignore))
    {    
        . Import-ModuleFile -Path $file.FullName
    }
    
    # Execute Post-import actions.
    . Import-ModuleFile -Path "$ModuleRoot\internal\postimport.ps1"
}
else
{
    Write-Debug "!LOADING COMPILED CODE!"
    
    #region Load compiled code
    enum JobState
{
    Valid
    InvalidPath
    MismatchedVariables
    UninitialisedVariables
    HasInputs
}

class YoutubeDlJob
{
    [string]$Name
    [string]$Path
    [hashtable]$_Variables
    [nullable[datetime]]$_lastExecutionTime
    [nullable[boolean]]$_lastExecutionSuccess
    
    # Constructor.
    YoutubeDlJob ([string]$name, [string]$path, [hashtable]$variableValues, [nullable[datetime]]$lastExecutionTime, 
        [nullable[boolean]]$lastExecutionSuccess)
    {
        $this.Name = $name
        $this.Path = $path
        $this._Variables = $variableValues
        $this._lastExecutionTime = $lastExecutionTime
        $this._lastExecutionSuccess = $lastExecutionSuccess
    }
        
    [JobState] GetState()
    {
        # Check through all the invalid states for a job.
        if ($this.HasInvalidPath())
        {
            return [JobState]::InvalidPath
        }
        if ($this.HasInputs())
        {
            return [JobState]::HasInputs
        }
        if ($this.HasMismatchedVariables())
        {
            return [JobState]::MismatchedVariables
        }
        if ($this.HasUninitialisedVariables())
        {
            return [JobState]::UninitialisedVariables
        }
        return [JobState]::Valid
    }
    
    [boolean] HasInvalidPath()
    {
        return [youtubeDlJob]::HasInvalidPath($this.Path)
    }
    static [boolean] HasInvalidPath([string]$path)
    {
        # Check whether the file path is valid.
        if (Test-Path -Path $path)
        {
            return $false
        }
        return $true
    }
    
    [boolean] HasInputs()
    {
        return [YoutubeDlJob]::HasInputs($this.Path)
    }
    static [boolean] HasInputs([string]$path)
    {
        # Check whether there are input definitions.
        if ((Read-ConfigDefinitions -Path $path -InputDefinitions).Count -eq 0)
        {
            return $false
        }
        return $true
    }
    
    [boolean] HasMismatchedVariables()
    {
        $configVariables =  Read-ConfigDefinitions -Path $this.Path -VariableDefinitions
        if (-not($configVariables.Count -eq 0))
        {
            $differenceA = $configVariables | Where-Object { $this._Variables.Keys -notcontains $_ }
            $differenceB = $this._Variables.Keys | Where-Object { $configVariables -notcontains $_ }
            if (($null -ne $differenceA) -or ($null -ne $differenceB))
            {
                return $true
            }
        }
        return $false
    }
    
    [boolean] HasUninitialisedVariables()
    {
        # Check that each variable has a value, i.e. is not uninitialised.
        foreach ($value in $this._Variables.Values)
        {
            if (($null -eq $value) -or [system.string]::IsNullOrWhiteSpace($value))
            {
                return $true
            }
        }
        return $false
    }
    
    
    [System.Collections.Generic.List[string]] GetVariables()
    {
        # Get the definitions within the file.
        return Read-ConfigDefinitions -Path $this.Path -VariableDefinitions
    }
    
    [System.Collections.Generic.List[string]] GetStoredVariables()
    {
        # Get the variable names defined in this object.
        $returnList = New-Object -TypeName System.Collections.Generic.List[string]
        foreach ($key in $this._Variables.Keys)
        {
            $returnList.Add($key)
        }
        
        return $returnList
    }
    
    [System.Collections.Generic.List[string]] GetMissingVariables()
    {
        # Get the variables which are missing in the object but present in
        # the configuration file.
        $configVariables =  Read-ConfigDefinitions -Path $this.Path -VariableDefinitions
        return $configVariables | Where-Object { $this._Variables.Keys -notcontains $_ }
    }
    
    [System.Collections.Generic.List[string]] GetUnnecessaryVariables()
    {
        # Get the variables which are present in the object but missing in
        # the configuration file.
        $configVariables =  Read-ConfigDefinitions -Path $this.Path -VariableDefinitions
        return $this._Variables.Keys | Where-Object { $configVariables -notcontains $_ }
    }
    
    [System.Collections.Generic.List[string]] GetNullVariables()
    {
        # Get any variable names defined in this object which don't have a value.
        $returnList = New-Object -TypeName System.Collections.Generic.List[string]
        foreach ($key in $this._Variables.Keys)
        {
            if (($null -eq $this._Variables[$key]) -or [system.string]::IsNullOrWhiteSpace($this._Variables[$key]))
            {
                $returnList.Add($key)
            }
        }
        
        return $returnList
    }
    
    [hashtable] GetScriptblocks()
    {
        # Get the scriptblock hashtable.
        return Read-ConfigDefinitions -Path $this.Path -VariableScriptblocks
    }
    
    [string] GetCompletedConfigFile()
    {
        # Go through all variable definitions and substitute the stored variable
        # value, before returning the modified file content string.
        $configFilestream = Get-Content -Path $this.Path -Raw
        foreach ($key in $this._Variables.Keys)
        {
            $configFilestream = $configFilestream -replace "v@{$key}{start{(?s)(.*?)}end}", $this._Variables[$key]
        }
        
        return $configFilestream
    }
}

enum TemplateState
{
    Valid
    InvalidPath
    NoInputs
}

class YoutubeDlTemplate
{
    [string]$Name
    [string]$Path
    
    # Constructor.
    YoutubeDlTemplate([string]$name, [string]$path)
    {
        $this.Name = $name
        $this.Path = $path
    }
    
    [TemplateState] GetState()
    {
        # Check through all the invalid states for a template.
        if ($this.HasInvalidPath())
        {
            return [TemplateState]::InvalidPath
        }
        if ($this.HasNoInput())
        {
            return [TemplateState]::NoInputs
        }
        return [TemplateState]::Valid
    }
    
    [boolean] HasInvalidPath()
    {
        return [YoutubeDlTemplate]::HasInvalidPath($this.Path)
    }
    static [boolean] HasInvalidPath([string]$path)
    {
        # Check whether the file path is valid.
        if (Test-Path -Path $path)
        {
            return $false
        }
        return $true
    }
    
    [boolean] HasNoInput()
    {
        return [YoutubeDlTemplate]::HasNoInput($this.Path)
    }
    static [boolean] HasNoInput([string]$path)
    {
        # Check whether the template has no inputs.
        if ((Read-ConfigDefinitions -Path $path -InputDefinitions).Count -gt 0)
        {
            return $false
        }
        return $true
    }
    
    
    [System.Collections.Generic.List[string]] GetInputs()
    {
        # Get the definitions within the file.
        return Read-ConfigDefinitions -Path $this.Path -InputDefinitions
    }
    
    [string] GetCompletedConfigFile([hashtable]$inputs)
    {
        # Go through all input definitions and substitute the user provided
        # value, before returning the modified file content string.
        $configFilestream = Get-Content -Path $this.Path -Raw
        foreach ($key in $inputs.Keys)
        {
            $configFilestream = $configFilestream -replace "i@{$key}", $inputs[$key]
        }
        
        return $configFilestream
    }
    
}


<#
.SYNOPSIS
    Starts the youtube-dl process and waits for it to finish.
 
.DESCRIPTION
    Starts the youtube-dl process and waits for it to finish.
     
.EXAMPLE
    PS C:\> Invoke-Process -Path $path
     
    Starts youtube-dl specifying the configuration file at the $path location.
     
.PARAMETER Path
    Path of the location of the configuration file to execute.
     
.INPUTS
    None
     
.OUTPUTS
    None
     
.NOTES
     
#>

function Invoke-Process
{
    
    [CmdletBinding()]
    Param
    (
        
        [Parameter(Position = 0, Mandatory = $true)]
        [string]
        $Path
        
    )
    
    # Define youtube-dl process information.
    $processStartupInfo = New-Object System.Diagnostics.ProcessStartInfo -Property @{
        FileName = "youtube-dl"
        Arguments = "--config-location `"$Path`""
        UseShellExecute = $false
    }
    
    # Start and wait for youtube-dl to finish.
    $process = New-Object System.Diagnostics.Process
    $process.StartInfo = $processStartupInfo
    $process.Start() | Out-Null
    $process.WaitForExit()
    $process.Dispose()
    
}

<#
.SYNOPSIS
    Reads all the definitions from a configuration file. Can specify between
    input definitions, variable definitions, or variable scriptblocks.
     
.DESCRIPTION
    Reads all the definitions from a configuration file. Can specify between
    input definitions, variable definitions, or variable scriptblocks.
     
.PARAMETER Path
    Path of the location of the configuration file.
     
.PARAMETER InputDefinitions
    Get the input definitions names.
     
.PARAMETER VariableDefinitions
    Get the variable definition names.
     
.PARAMETER VariableScriptblocks
    Get the variable scriptblock strings.
     
.EXAMPLE
    PS C:\> Read-ConfigDefinitions -Path ~\conf.txt -InputDefinitions
     
    Reads in and generates a list of all input definitions.
     
.INPUTS
    None
     
.OUTPUTS
    System.Collections.Generic.List[string]
    Hashtable[string, scriptblock]
     
.NOTES
     
#>

function Read-ConfigDefinitions
{
    
    [CmdletBinding()]
    param
    (
        
        [Parameter(Position = 0, Mandatory = $true)]
        [String]
        $Path,
        
        [Parameter()]
        [switch]
        $InputDefinitions,
        
        [Parameter()]
        [switch]
        $VariableDefinitions,
        
        [Parameter()]
        [switch]
        $VariableScriptblocks
        
    )
    
    # If the file doesn't exist, quit early.
    if (-not (Test-Path -Path $Path))
    {
        return $null
    }
    
    # Read in the config file as a single string.
    $configFilestream = Get-Content -Path $Path -Raw
    $definitionList = New-Object -TypeName System.Collections.Generic.List[string]
    $hashList = @{}
    
    if ($InputDefinitions -eq $true)
    {
        # Find all matches to:
        # 1. --some-parameter i@{name} : full parameter definition
        # 1. -s i@{name} : shorthand parameter definition
        # 2. 'i@{Url}' : special case for url, since it doesn't have a flag
        # Also matches even if multiple parameter definitions are on the same line.
        $regex = [regex]::Matches($configFilestream, "(-(\S+)\s'?i@{(\w+)}'?)\s*")
        $url = [regex]::Match($configFilestream, "'i@{url}'", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)
        
        # Add the definition name fields to the list.
        foreach ($match in $regex)
        {
            # .Group[1] is the whole match
            # .Group[2] is the 'some-parameter' or 's' match
            # .Group[3] is the 'name' match
            $definitionList.Add($match.Groups[3].Value)
        }
        # If a url input is detected, add that too.
        if ($url.Success)
        {
            $definitionList.Add("Url")
        }
    }
    else
    {
        # Find all matches to:
        # 1. --some-parameter v@{name}{start{scriptblock}end} : full parameter definition
        # 1. -s v@{name}{start{scritpblock}end} : shorthand parameter definition
        # Also matches even if multiple parameter definitions are on the same line.
        $regex = [regex]::Matches($configFilestream, "(-(\S+)\s'?v@{(\w+)}{start{(?s)(.*?)}end}'?)\s+")
        
        # Add the descriptor fields to the list.
        foreach ($match in $regex)
        {
            # .Group[1] is the whole match
            # .Group[2] is the 'some-parameter' or 's' match
            # .Group[3] is the 'name' match
            # .Group[4] is the 'scriptblock' match
            if ($VariableDefinitions -eq $true)
            {
                $definitionList.Add($match.Groups[3].Value)
            }
            elseif ($VariableScriptblocks -eq $true)
            {
                $hashList[$match.Groups[3].Value] = $match.Groups[4].Value
            }
        }
    }
    
    if ($VariableScriptblocks)
    {
        Write-Output $hashList
    }
    else
    {
        # Return the list as a List object, rather than as an array (by default).
        Write-Output $definitionList -NoEnumerate
    }
}

<#
.SYNOPSIS
    Reads all of the defined job objects.
     
.DESCRIPTION
    Reads all of the defined job objects.
     
.EXAMPLE
    PS C:\> $list = Read-Jobs
     
    Reads all of the job objects into a variable, for later manipulation.
     
.INPUTS
    None
     
.OUTPUTS
    System.Collections.Generic.List[YoutubeDlJob]
     
.NOTES
     
#>

function Read-Jobs
{
    # Create an empty list.
    $jobList = New-Object -TypeName System.Collections.Generic.List[YoutubeDlJob]
    
    # If the file doesn't exist, skip any importing.
    if (Test-Path -Path $script:JobData -ErrorAction SilentlyContinue)
    {
        # Read the xml data in.
        $xmlData = Import-Clixml -Path $script:JobData
        
        # Iterate through all the objects.
        foreach ($item in $xmlData)
        {
            # Rather than extracting the deserialised objects, which would
            # create a mess of serialised and non-serialised objects, create
            # new identical copies from scratch.
            if ($item.pstypenames[0] -eq "Deserialized.YoutubeDlJob")
            {
                $job = [YoutubeDlJob]::new($item.Name, $item.Path, $item._Variables, $item._lastExecutionTime, $item._lastExecutionSuccess)
                $jobList.Add($job)
            }
        }
    }
    
    # Return the list as a <List> object, rather than as an array,
    # (ps converts by default).
    Write-Output $jobList -NoEnumerate
}


<#
.SYNOPSIS
    Reads all of the defined template objects.
     
.DESCRIPTION
    Reads all of the defined template objects.
     
.EXAMPLE
    PS C:\> $list = Read-Templates
     
    Reads all of the template objects into a variable, for later manipulation.
     
.INPUTS
    None
     
.OUTPUTS
    System.Collections.Generic.List[YoutubeDlTemplate]
     
.NOTES
     
#>

function Read-Templates
{
    # Create an empty list.
    $templateList = New-Object -TypeName System.Collections.Generic.List[YoutubeDlTemplate]
    
    # If the file doesn't exist, skip any importing.
    if (Test-Path -Path $script:TemplateData -ErrorAction SilentlyContinue)
    {
        # Read the xml data in.
        $xmlData = Import-Clixml -Path $script:TemplateData
        
        # Iterate through all the objects.
        foreach ($item in $xmlData)
        {
            # Rather than extracting the deserialised objects, which would
            # create a mess of serialised and non-serialised objects, create
            # new identical copies from scratch.
            if ($item.pstypenames[0] -eq "Deserialized.YoutubeDlTemplate")
            {
                $template = [YoutubeDlTemplate]::new($item.Name, $item.Path)
                $templateList.Add($template)
            }
        }
    }
    
    # Return the list as a <List> object, rather than as an array,
    # (ps converts by default).
    Write-Output $templateList -NoEnumerate
}


<#
.SYNOPSIS
    Gets the specified youtube-dl item(s).
     
.DESCRIPTION
    The `Get-Item` cmdlet gets one or more youtube-dl templates or jobs,
    specified by their name(s).
     
.PARAMETER Template
    Indicates that this cmdlet will be retrieving youtube-dl template(s).
     
.PARAMETER Job
    Indicates that this cmdlet will be retrieving youtube-dl job(s).
     
.PARAMETER Names
    Specifies the name(s) of the items to get.
     
 [!]Once you specify the '-Template'/'-Job' switch, this parameter will
    autocomplete to valid names for the respective item type.
     
.PARAMETER All
    Specifies to get all items of the respective item type.
     
.INPUTS
    System.String[]
        You can pipe one or more strings containing the names of the items
        to get.
     
.OUTPUTS
    YoutubeDlTemplate
    YoutubeDlJob
     
.NOTES
    This cmdlet is aliased by default to 'gydl'.
     
.EXAMPLE
    PS C:\> Get-YoutubeDlItem -Template -Names "music","video"
     
    Gets the youtube-dl template definitions which are named "music" and
    "video", and pipes them out to the screen, by default formatted in a list.
     
.EXAMPLE
    PS C:\> Get-YoutubeDlItem -Job -All
     
    Gets all youtube-dl job definitions, and pipes them out to the screen,
    by default formatted in a list.
     
.EXAMPLE
    PS C:\> Get-YoutubeDlItem -Job "music" | Invoke-YoutubeDl -Job
     
    Gets the youtube-dl job named "music", and then invokes youtube-dl to
    run it automatically.
     
.LINK
    New-YoutubeDlItem
    Set-YoutubeDlItem
    Remove-YoutubeDlItem
    Invoke-YoutubeDl
    about_ytdlWrapper
     
#>

function Get-YoutubeDlItem
{
    [Alias("gydl")]
    
    [CmdletBinding()]
    param
    (
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Template-All")]
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Template-Specific")]
        [switch]
        $Template,
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Job-All")]
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Job-Specific")]
        [switch]
        $Job,
        
        [Parameter(Position = 1, Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Template-Specific")]
        [Parameter(Position = 1, Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Job-Specific")]
        [Alias("Name")]
        [string[]]
        $Names,
        
        [Parameter(Position = 1, Mandatory = $true, ParameterSetName = "Template-All")]
        [Parameter(Position = 1, Mandatory = $true, ParameterSetName = "Job-All")]
        [switch]
        $All
        
    )
    
    begin
    {
        # Store the retrieved items, to in one go at the end of execution.
        $outputList = if ($Template)
        {
            New-Object -TypeName System.Collections.Generic.List[YoutubeDlTemplate]
        }
        elseif ($Job)
        {
            New-Object -TypeName System.Collections.Generic.List[YoutubeDlJob]
        }
        
        # Read in the correct list of templates or jobs.
        $objectList = if ($Template)
        {
            Read-Templates
        }
        elseif ($Job)
        {
            Read-Jobs
        }
    }
    
    process
    {
        if (-not $All)
        {
            # Iterate through all the passed in names.
            foreach ($name in $Names)
            {
                # If the object doesn't exist, warn the user.
                $existingObject = $objectList | Where-Object { $_.Name -eq $name }
                if ($null -eq $existingObject)
                {
                    Write-Warning "There is no $(if($Template){`"template`"}else{`"job`"}) named: '$name'."
                    continue
                }
                
                # Add the object for outputting.
                $outputList.Add($existingObject) | Out-Null
            }
        }
        else
        {
            # Output every object.
            $outputList = $objectList
        }
    }
    
    end
    {
        # By default, this outputs in List formatting.
        $outputList | Sort-Object -Property Name
    }
}

<#
.SYNOPSIS
    Runs youtube-dl.
     
.DESCRIPTION
    The `Invoke-YoutubeDl` cmdlet runs youtube-dl.exe using the specified
    method.
     
    This cmdlet can be used to run youtube-dl, giving it a fully completed
    configuration file which matches the youtube-dl config specification.
     
    This cmdlet can be used to run a youtube-dl template, giving it the
    required input parameters.
     
    This cmdlet can be used to run a youtube-dl job, which happens without
    user input.
     
.PARAMETER Template
    Indicates that this cmdlet will be running a youtube-dl template.
     
.PARAMETER Job
    Indicates that this cmdlet will be running a youtube-dl job.
     
.PARAMETER Path
    Specifies the path of the location of the configuration file to use.
     
.PARAMETER Names
    Specifies the name(s) of the items to run.
     
 [!]Once you specify the '-Template'/'-Job' switch, this parameter will
    autocomplete to valid names for the respective item type.
     
    If specifying the '-Template' switch, you can only pass in one name.
     
    If specifying the '-Job' switch, you can pass in multiple names.
 
.PARAMETER WhatIf
    Shows what would happen if the cmdlet runs. The cmdlet does not run.
     
.PARAMETER Confirm
    Prompts you for confirmation before running any state-altering actions
    in this cmdlet.
     
.EXAMPLE
    PS C:\> Invoke-YoutubeDl -Path ~\download.conf
     
    Runs youtube-dl, giving it the "download.conf" configuration file to parse.
    The configuration file must fully align to the youtube-dl config
    specification.
     
.EXAMPLE
    Assuming the template 'music' has the input named "Url".
     
    PS C:\> Invoke-YoutubeDl -Template -Name "music" -Url "https:\\some\url"
     
    Runs the "music" template, which takes in the '-Url' parameter to complete
    the configuration file, before giving it to youtube-dl.
     
.EXAMPLE
    PS C:\> Invoke-YoutubeDl -Job -Name "archive"
     
    Runs the "archive" job, which uses the stored variables to complete the
    configuration file and pass it to youtube-dl. Afterwards, the scriptblocks
    responsible for each variable run to generate the new variable values to
    be used for the next run.
     
.INPUTS
    System.String[]
        You can pipe one or more strings containing the names of the items
        to run.
     
.OUTPUTS
    None
     
.NOTES
    When executing a template using the '-Template' switch, a dynamic parameter
    corresponding to each input definition, found within the configuration
    file, will be generated. The parameter sets the value of the input to make
    the template ready for execution.
     
    For detailed help regarding running a template, see the
    "INVOKING A TEMPLATE" section in the help at:
    'about_ytdlWrapper_templates'.
 
    This cmdlet is aliased by default to 'iydl'.
     
.LINK
    New-YoutubeDlItem
    Get-YoutubeDlItem
    Set-YoutubeDlItem
    Remove-YoutubeDlItem
    about_ytdlWrapper
     
#>

function Invoke-YoutubeDl
{
    [Alias("iydl")]
    
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Template")]
        [switch]
        $Template,
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Job")]
        [switch]
        $Job,
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Config")]
        [Alias("ConfigurationFilePath")]
        [string]
        $Path,
        
        [Parameter(Position = 1, Mandatory = $true, ParameterSetName = "Template")]
        [Parameter(Position = 1, Mandatory = $true, ParameterSetName = "Job", ValueFromPipelineByPropertyName = $true)]
        [Alias("Name")]
        [string[]]
        $Names
        
    )
    
    dynamicparam
    {
        # Only run the input detection logic if a template is given, and only
        # one template is given, and the template exists, and the template
        # has a valid configuration file path.
        if (-not $Template) { return }
        if ($null -eq $Names) { return }
        $name = $Names[0]
        if ([system.string]::IsNullOrWhiteSpace($name)) { return }
        $templateList = Read-Templates
        $templateObject = $templateList | Where-Object { $_.Name -eq $name }
        if ($null -eq $templateObject) { return }
        if ($templateObject.GetState() -eq "InvalidPath") { return }
        
        # Retrieve all instances of input definitions in the config file.
        $inputNames = $templateObject.GetInputs()
        
        # Define the dynamic parameter dictionary to hold new parameters.
        $parameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        # Now that a list of all input definitions is found, create a
        # dynamic parameter for each one.
        foreach ($input in $inputNames)
        {
            # Set up the necessary objects for a parameter.
            $paramAttribute = New-Object System.Management.Automation.ParameterAttribute
            $paramAttribute.Mandatory = $true
            $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($paramAttribute)                
            $param = New-Object System.Management.Automation.RuntimeDefinedParameter($input, [String], $attributeCollection)
            
            $parameterDictionary.Add($input, $param)
        }
        
        return $parameterDictionary
    }
    
    process
    {
        if ($PSCmdlet.ParameterSetName -eq "Config")
        {
            # Validate that the path is valid.
            if (-not (Test-Path -Path $Path))
            {
                Write-Error "The configuration file path: '$Path' is invalid!"
                return
            }
            
            if ($PSCmdlet.ShouldProcess("Starting youtube-dl.exe.", "Are you sure you want to start youtube-dl.exe?", "Start Process Prompt"))
            {
                Invoke-Process -Path "$script:Folder\$hash.conf"
            }
        }
        elseif ($PSCmdlet.ParameterSetName -eq "Template")
        {
            # Only accept one template at a time.
            if ($Names.Length -gt 1)
            {
                Write-Error "Cannot specify more than one template per invocation of this cmdlet!"
                return
            }
            
            $name = $Names[0]
            
            # Retrieve the template and check that it exists.
            $templateList = Read-Templates
            $templateObject = $templateList | Where-Object { $_.Name -eq $name }
            Write-Verbose "Validating parameters and the configuration file."
            if ($null -eq $templateObject)
            {
                Write-Error "There is no template named: '$name'."
                return
            }
            
            # Validate that the template can be used.
            if ($templateObject.HasInvalidPath())
            {
                Write-Error "The template: '$name' has a configuration file path: '$($templateObject.Path)' which is invalid!"
                    return
            }
            if ($templateObject.HasNoInput())
            {
                Write-Error "The template: '$name' has a configuration file with no input definitions!`nFor help regarding the configuration file, see the `"SETTING UP A CONFIGURATION FILE`" section in the help at: `'about_ytdlWrapper_templates`'."
                    return
            }
            
            # Get the necessary inputs for this template, and assign each the
            # user provided value. Quit if the user has failed to give in a
            # certain value.
            $inputNames = $templateObject.GetInputs()
            $inputs = @{}
            foreach ($input in $inputNames)
            {
                if ($PSBoundParameters.ContainsKey($input))
                {
                    $inputs[$input] = $PSBoundParameters[$input]
                }
                else
                {
                    Write-Error "The template: '$name' requires the input: '$input' which has been not provided!"
                    return
                }
            }
            
            $completedTemplateContent = $templateObject.GetCompletedConfigFile($inputs)
            
            # Write modified config file (with substituted user inputs) to a
            # temporary file. This is done because it is easier to use the
            # --config-location flag for youtube-dl than to edit the whole
            # string to use proper escape sequences.
            $stream = [System.IO.MemoryStream]::new([byte[]][char[]]$completedTemplateContent)
            $hash = (Get-FileHash -InputStream $stream -Algorithm SHA256).hash
            if ($PSCmdlet.ShouldProcess("Creating temporary configuration file at: '$script:Folder\$hash.conf'.", "Are you sure you want to create a temporary configuration file at: '$script:Folder\$hash.conf'?", "Create File Prompt"))
            {
                Out-File -FilePath "$script:Folder\$hash.conf" -Force -InputObject $completedTemplateContent `
                    -ErrorAction Stop
            }
            
            if ($PSCmdlet.ShouldProcess("Starting youtube-dl.exe.", "Are you sure you want to start youtube-dl.exe?", "Start Process Prompt"))
            {
                Invoke-Process -Path "$script:Folder\$hash.conf"
            }
            
            # Clean up the temporary file.
            if ($PSCmdlet.ShouldProcess("Clean-up temporary configuration file from: '$script:Folder\$hash.conf'.", "Are you sure you want to clean-up the temporary configuration file from: '$script:Folder\$hash.conf'?", "Delete File Prompt"))
            {
                Remove-Item -Path "$script:Folder\$hash.conf" -Force
            }
        }
        elseif ($PSCmdlet.ParameterSetName -eq "Job")
        {
            foreach ($name in $Names)
            {
                # Retrieve the template and check that it exists.
                $jobList = Read-Jobs
                $jobObject = $jobList | Where-Object { $_.Name -eq $name }
                Write-Verbose "Validating parameters and the configuration file."
                if ($null -eq $jobObject)
                {
                    Write-Error "There is no job named: '$name'."
                    return
                }
                
                # Validate that the job can be used.
                if ($jobObject.HasInvalidPath())
                {
                    Write-Error "The configuration file path: '$Path' is invalid."
                    return
                }
                if ($jobObject.HasInputs())
                {
                    Write-Error "The configuration file at: '$Path' has input definitions, which a job cannot have.`nFor help regarding the configuration file, see the `"SETTING UP A CONFIGURATION FILE`" section in the help at: `'about_ytdlWrapper_jobs`'."
                    return
                }
                if ($jobObject.HasMismatchedVariables())
                {
                    Write-Error "The job: '$name' has a mismatch between the variables stored in the database and the variable definitions within the configuration file!`nRun the `Set-YoutubeDlItem` cmdlet with the '-Update' switch to fix the issue."
                    return
                }
                if ($jobObject.HasUninitialisedVariables())
                {
                    Write-Error "The job: '$name' has uninitialised variables and cannot run!`nRun the `Set-YoutubeDlItem` cmdlet with the '-Update' switch to fix the issue."
                    return
                }
                
                $completedJobContent = $jobObject.GetCompletedConfigFile()
                
                # Write modified config file (with substituted variable values) to a
                # temporary file. This is done because it is easier to use the
                # --config-location flag for youtube-dl than to edit the whole
                # string to use proper escape sequences.
                $stream = [System.IO.MemoryStream]::new([byte[]][char[]]$completedJobContent)
                $hash = (Get-FileHash -InputStream $stream -Algorithm SHA256).hash
                if ($PSCmdlet.ShouldProcess("Creating temporary configuration file at: '$script:Folder\$hash.conf'.", "Are you sure you want to create a temporary configuration file at: '$script:Folder\$hash.conf'?", "Create File Prompt"))
                {
                    Out-File -FilePath "$script:Folder\$hash.conf" -Force -InputObject $completedJobContent `
                        -ErrorAction Stop
                }
                
                if ($PSCmdlet.ShouldProcess("Starting youtube-dl.exe.", "Are you sure you want to start youtube-dl.exe?", "Start Process Prompt"))
                {
                    Invoke-Process -Path "$script:Folder\$hash.conf"
                }
                # Set the appropriate execution information.
                if ($LASTEXITCODE -eq 0)
                {
                    $jobObject._lastExecutionSuccess = $true
                }
                else
                {
                    $jobObject._lastExecutionSuccess = $false
                }
                $jobObject._lastExecutionTime = Get-Date
                
                # Clean up the temporary file.
                if ($PSCmdlet.ShouldProcess("Clean-up temporary configuration file from: '$script:Folder\$hash.conf'.", "Are you sure you want to clean-up the temporary configuration file from: '$script:Folder\$hash.conf'?", "Delete File Prompt"))
                {
                    Remove-Item -Path "$script:Folder\$hash.conf" -Force
                }
                
                # If a scriptblock didn't return a value, warn the user.
                Write-Verbose "Updating variable values for the job."
                $scriptblocks = $jobObject.GetScriptblocks()
                foreach ($key in $scriptblocks.Keys)
                {
                    $scriptblock = [scriptblock]::Create($scriptblocks[$key])
                    $returnResult = Invoke-Command -ScriptBlock $scriptblock
                    # If no value is returned, return the variable name to the invocation
                    # cmdlet to warn the user.
                    if ($null -eq $returnResult)
                    {
                        Write-Error "The job: '$name' has a scriptblock definition named: '$key' which did not return a value!`nFor help regarding the configuration file, see the `"SETTING UP A CONFIGURATION FILE`" section in the help at: `'about_ytdlWrapper_jobs`'."
                        $jobObject._lastExecutionSuccess = $false
                        $jobObject._Variables[$key] = $null
                        continue
                    }
                    
                    $jobObject._Variables[$key] = $returnResult
                }
                
                if ($PSCmdlet.ShouldProcess("Updating database at '$script:JobData' with the changes.", "Are you sure you want to update the database at '$script:JobData' with the changes?", "Save File Prompt"))
                {
                    Export-Clixml -Path $script:JobData -InputObject $jobList -WhatIf:$false -Confirm:$false `
                        | Out-Null
                }
            }
        }
    }
}

<#
.SYNOPSIS
    Creates a new youtube-dl item.
     
.DESCRIPTION
    The `New-YoutubeDlItem` cmdlet creates a new youtube-dl template or job,
    and sets its values in accordance to the given configuration
    file.
     
    This cmdlet can be used to create a youtube-dl template, which takes in
    a configuration file with input definitions. Alternatively, this cmdlet
    can be used to create a youtube-dl job, which takes in a configuration
    file with variable definitions.
     
    This cmdlet can optionally keep the configuration files in their original
    location if desired.
     
.PARAMETER Template
    Indicates that this cmdlet will be creating a youtube-dl template.
     
.PARAMETER Job
    Indicates that this cmdlet will be creating a youtube-dl job.
     
.PARAMETER Name
    Specifies the name of the item to be created; must be unique.
     
.PARAMETER Path
    Specifies the path of the location of the configuration file to use.
     
.PARAMETER DontMoveConfigurationFile
    Prevents the configuration file from being moved from its original location
    to a new location in the module appdata folder.
     
.PARAMETER WhatIf
    Shows what would happen if the cmdlet runs. The cmdlet does not run.
     
.PARAMETER Confirm
    Prompts you for confirmation before running any state-altering actions
    in this cmdlet.
     
.PARAMETER Force
    Forces this cmdlet to create an item that writes over an existing item.
    Even using this parameter, if the filesystem denies access to the
    necessary files, this cmdlet will fail.
     
.INPUTS
    System.String
        You can pipe a string containing a path to the location of the
        configuration file.
     
.OUTPUTS
    YoutubeDlTemplate
    YoutubeDlJob
     
.NOTES
    When creating a job using the '-Job' switch, a dynamic parameter
    corresponding to each variable definition, found within the configuration
    file, will be generated. The parameter sets the initial value of the
    variable to make the job ready for first-time execution.
     
    For detailed help regarding the configuration file, see the
    "SETTING UP A CONFIGURATION FILE" section in the help at:
    'about_ytdlWrapper_jobs'.
     
    This cmdlet is aliased by default to 'nydl'.
     
.EXAMPLE
    PS C:\> New-YoutubeDlItem -Template -Name "music" -Path ~\music.conf
     
    Creates a new youtube-dl template named "music", and moves the configuration
    file to the module appdata folder.
     
.EXAMPLE
    PS C:\> New-YoutubeDlItem -Template -Name "music" -Path ~\music.conf
                -DontMoveConfigurationFile
                 
    Creates a new youtube-dl template named "music", but doesn't move the
    configuration file from the existing location. If this file is ever moved
    manually, this template will cease working until the path is updated to
    the new location of the configuration file.
     
.EXAMPLE
    Assuming 'music.conf' has an input definition named "Url".
     
    PS C:\> New-YoutubeDlItem -Template -Name "music" -Path ~\music.conf |
                Invoke-YoutubeDl -Template -Url "https:\\some\youtube\url"
     
    Creates a new youtube-dl template named "music", and then invokes
    youtube-dl to run it, giving in the required inputs (Url) in the process.
     
.EXAMPLE
    Assuming 'archive.conf' has a variable definition named "Autonumber".
     
    PS C:\> New-YoutubeDlJob -Job -Name "archive" -Path ~\archive.conf
                -Autonumber "5"
                 
    Creates a new youtube-dl job named "archive", and moves the configuration
    file from the home directory to the module appdata foler. Also sets
    the 'Autonumber' variable within this configuration file to an initial
    value of "5".
     
.LINK
    Get-YoutubeDlItem
    Set-YoutubeDlItem
    Remove-YoutubeDlItem
    about_ytdlWrapper
     
#>

function New-YoutubeDlItem
{
    [Alias("nydl")]
    
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Template")]
        [switch]
        $Template,
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Job")]
        [switch]
        $Job,
        
        [Parameter(Position = 1, Mandatory = $true)]
        [string]
        $Name,
        
        [Parameter(Position = 2, Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("ConfigurationFilePath")]
        [string]
        $Path,
        
        [Parameter(Position = 3)]
        [switch]
        $DontMoveConfigurationFile,
        
        [Parameter()]
        [switch]
        $Force
        
    )
    
    dynamicparam
    {
        # Only run the variable detection logic if creating a new job,
        # and a valid configuration file path has been given in.
        if ($Job -and ($null -ne $Path) -and (Test-Path -Path $Path))
        {
            # Retrieve all instances of variable definitions in the config file.
            $definitionList = Read-ConfigDefinitions -Path $Path -VariableDefinitions
            
            # Define the dynamic parameter dictionary to hold new parameters.
            $parameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
            
            # Create a parameter for each variable definition found.
            foreach ($definition in $definitionList)
            {
                # Set up the necessary objects for a parameter.
                $paramAttribute = New-Object System.Management.Automation.ParameterAttribute
                $paramAttribute.Mandatory = $true
                $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
                $attributeCollection.Add($paramAttribute)                
                $param = New-Object System.Management.Automation.RuntimeDefinedParameter($definition, [String], `
                    $attributeCollection)
                
                $parameterDictionary.Add($definition, $param)
            }
            
            return $parameterDictionary
        }
    }
    
    begin
    {
        # Validate that '-WhatIf'/'-Confirm' isn't used together with '-Force'.
        # This is ambiguous, so warn the user instead.
        Write-Debug "`$WhatIfPreference: $WhatIfPreference"
        Write-Debug "`$ConfirmPreference: $ConfirmPreference"
        if ($WhatIfPreference -and $Force)
        {
            Write-Error "You cannot specify both '-WhatIf' and '-Force' in the invocation for this cmdlet!"
            return
        }
        if (($ConfirmPreference -eq "Low") -and $Force)
        {
            Write-Error "You cannot specify both '-Confirm' and '-Force' in the invocation for this cmdlet!"
            return
        }
    }
    
    process
    {
        if ($Template)
        {
            # Validate that the name isn't already taken.
            $templateList = Read-Templates
            $existingTemplate = $templateList | Where-Object { $_.Name -eq $Name }
            Write-Verbose "Validating parameters and the configuration file."
            if ($null -ne $existingTemplate)
            {
                if ($Force)
                {
                    Write-Verbose "Existing template named: '$Name' exists, but since the '-Force' switch is present, the existing template will be deleted."
                    $existingTemplate | Remove-YoutubeDlItem -Template
                }
                else
                {
                    Write-Error "The name: '$Name' is already taken for a template."
                    return
                }
            }
            
            # Validate that the configuration file exists and can be used.
            if ([YoutubeDlTemplate]::HasInvalidPath($Path))
            {
                Write-Error "The configuration file path: '$Path' is invalid."
                return
            }
            if ([YoutubeDlTemplate]::HasNoInput($Path))
            {
                Write-Error "The configuration file located at: '$Path' has no input definitions.`nFor help regarding the configuration file, see the `"SETTING UP A CONFIGURATION FILE`" section in the help at: `'about_ytdlWrapper_templates`'."
                    return
            }
            
            if (-not $DontMoveConfigurationFile -and $PSCmdlet.ShouldProcess("Moving configuration file from '$(Split-Path -Path $Path -Parent)' to '$script:Folder\Templates'.", "Are you sure you want to move the configuration file from '$(Split-Path -Path $Path -Parent)' to '$script:Folder\Templates'?", "Move File Prompt")) 
            {
                # Move the file over to the module appdata folder, and rename it
                # to the unique name of the template to avoid any potential
                # filename collisions.
                $fileName = Split-Path -Path $Path -Leaf
                Move-Item -Path $Path -Destination "$script:Folder\Templates" -Force -WhatIf:$false `
                    -Confirm:$false | Out-Null
                Rename-Item -Path "$script:Folder\Templates\$fileName" -NewName "$Name.conf" -Force -WhatIf:$false `
                    -Confirm:$false | Out-Null
                $Path = "$script:Folder\Templates\$Name.conf"
            }
            
            # Create the object and save it to the database.
            Write-Verbose "Creating new youtube-dl template object."
            $newTemplate = [YoutubeDlTemplate]::new($Name, $Path)
            $templateList.Add($newTemplate)
            if ($PSCmdlet.ShouldProcess("Saving newly-created template to database at '$script:TemplateData'.", "Are you sure you want to save the newly-created template to the database at '$script:TemplateData'?", "Save File Prompt"))
            {
                Export-Clixml -Path $script:TemplateData -InputObject $templateList -Force -WhatIf:$false `
                    -Confirm:$false | Out-Null
            }
            
            Write-Output $newTemplate
        }
        elseif ($Job)
        {
            # Validate that the name isn't already taken.
            $jobList = Read-Jobs
            $existingJob = $jobList | Where-Object { $_.Name -eq $Name }
            Write-Verbose "Validating parameters and the configuration file."
            if ($null -ne $existingJob)
            {
                if ($Force)
                {
                    Write-Verbose "Existing job named: '$Name' exists, but since the '-Force' switch is present, the existing job will be deleted."
                    $existingJob | Remove-YoutubeDlItem -Job
                }
                else
                {
                    Write-Error "The name: '$Name' is already taken for a job."
                    return
                }
            }
            
            # Validate that each required variable in the configuration file
            # has been given an initial value.
            $variableDefinitions = Read-ConfigDefinitions -Path $Path -VariableDefinitions
            $initialVariableValues = @{}
            foreach ($definition in $variableDefinitions)
            {
                if ($PSBoundParameters.ContainsKey($definition))
                {
                    $initialVariableValues[$definition] = $PSBoundParameters[$definition]
                }
                else
                {
                    Write-Error "The variable: '$definition' has not been provided an initial value as a parameter!"
                    return
                }
            }
            # Validate that the configuration file exists and can be used.
            if ([YoutubeDlJob]::HasInvalidPath($Path))
            {
                Write-Error "The configuration file path: '$Path' is invalid."
                return
            }
            if ([YoutubeDlJob]::HasInputs($Path))
            {
                Write-Error "The configuration file at: '$Path' has input definitions, which a job cannot have.`nFor help regarding the configuration file, see the `"SETTING UP A CONFIGURATION FILE`" section in the help at: `'about_ytdlWrapper_jobs`'."
                return
            }
            
            if (-not $DontMoveConfigurationFile -and $PSCmdlet.ShouldProcess("Moving configuration file from '$(Split-Path -Path $Path -Parent)' to '$script:Folder\Jobs'.", "Are you sure you want to move the configuration file from '$(Split-Path -Path $Path -Parent)' to '$script:Folder\Jobs'?", "Move File Prompt"))
            {
                # Move the file over to the module appdata folder, and rename it
                # to the unique name of the template to avoid any potential
                # filename collisions.
                $fileName = Split-Path -Path $Path -Leaf
                Move-Item -Path $Path -Destination "$script:Folder\Jobs" -Force -WhatIf:$false -Confirm:$false `
                    | Out-Null
                Rename-Item -Path "$script:Folder\Jobs\$fileName" -NewName "$Name.conf" -Force -WhatIf:$false `
                    -Confirm:$false | Out-Null
                $Path = "$script:Folder\Jobs\$Name.conf"
            }
            
            # Create the object and save it to the database.
            Write-Verbose "Creating new youtube-dl job object."
            $newJob = [YoutubeDlJob]::new($Name, $Path, $initialVariableValues, $null, $null)
            $jobList.Add($newJob)
            if ($PSCmdlet.ShouldProcess("Saving newly-created template to database at '$script:JobData'.", "Are you sure you want to save the newly-created template to the database at '$script:JobData'?", "Save File Prompt"))
            {
                Export-Clixml -Path $script:JobData -InputObject $jobList -Force -WhatIf:$false -Confirm:$false `
                    | Out-Null
            }
            
            Write-Output $newJob
        }
    }
}

<#
.SYNOPSIS
    Deletes a specified youtube-dl item.
     
.DESCRIPTION
    The `Remove-YoutubeDlItem` cmdlet deletes one or more youtube-dl templates
    or jobs, specified by their name(s).
     
.PARAMETER Template
    Indicates that this cmdlet will be deleting youtube-dl template(s).
     
.PARAMETER Job
    Indicates that this cmdlet will be deleting youtube-dl job(s).
     
.PARAMETER Names
    Specifies the name(s) of the items to delete.
     
 [!]Once you specify a '-Template'/'-Job' switch, this parameter will
    autocomplete to valid names for the respective item type.
     
.PARAMETER WhatIf
    Shows what would happen if the cmdlet runs. The cmdlet does not run.
     
.PARAMETER Confirm
    Prompts you for confirmation before running any state-altering actions
    in this cmdlet.
     
.INPUTS
    System.String[]
        You can pipe one or more strings containing the names of the items
        to delete.
     
.OUTPUTS
    None
     
.NOTES
    This cmdlet is aliased by default to 'rydl'.
     
.EXAMPLE
    PS C:\> Remove-YoutubeDlItem -Template -Names "music","video"
     
    Deletes the youtube-dl templates named "music" and "video".
     
.EXAMPLE
    PS C:\> Remove-YoutubeDlItem -Job -Name "archive"
     
    Deletes a youtube-dl job named "archive".
     
.LINK
    New-YoutubeDlItem
    Get-YoutubeDlItem
    Set-YoutubeDlItem
    about_ytdlWrapper
     
#>

function Remove-YoutubeDlItem
{
    [Alias("rydl")]
    
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Template")]
        [switch]
        $Template,
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Job")]
        [switch]
        $Job,
        
        [Parameter(Position = 1, Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("Name")]
        [string[]]
        $Names
        
    )
    
    begin
    {
        # Read in the correct list of templates or jobs.
        $objectList = if ($Template)
        {
            Read-Templates
        }
        elseif ($Job)
        {
            Read-Jobs
        }
        
        # Get the correct database path.
        $databasePath = if ($Template)
        {
            $script:TemplateData
        }
        elseif ($Job)
        {
            $script:JobData
        }
    }
    
    process
    {
        # Iterate through all the passed in names.
        foreach ($name in $Names)
        {
            # If the object doesn't exist, warn the user.
            $object = $objectList | Where-Object { $_.Name -eq $name }
            if ($null -eq $object)
            {
                Write-Error "There is no $(if($Template){`"template`"}else{`"job`"}) named: '$name'."
                continue
            }
            
            # Remove the object from the list.
            Write-Verbose "Deleting the youtube-dl $(if($Template){`"template`"}else{`"job`"}) object."
            $objectList.Remove($object) | Out-Null
        }
    }
    
    end
    {
        # Save the modified database.
        if ($PSCmdlet.ShouldProcess("Updating database at '$databasePath' with the changes (deletions).", "Are you sure you want to update the database at '$databasePath' with the changes (deletions)?", "Save File Prompt"))
        {
            Export-Clixml -Path $databasePath -InputObject $objectList -Force -WhatIf:$false `
                -Confirm:$false | Out-Null
        }
    }
}

<#
.SYNOPSIS
    Changes a value of a youtube-dl item.
     
.DESCRIPTION
    The `Set-YoutubeDlItem` cmdlet changes the value of a youtube-dl template
    or job.
     
    This cmdlet can be used to change a template's/job's path of the location
    of the configuration file to use.
     
    This cmdlet can be used to change a value of a variable of a job.
     
    This cmdlet can be used to update a job if the configuration file changes,
    initialising any new variables which have been added since the last time,
    and removing any now-unnecessary variables.
     
.PARAMETER Template
    Indicates that this cmdlet will be changing a youtube-dl template.
     
.PARAMETER Job
    Indicates that this cmdlet will be changing a youtube-dl job.
     
.PARAMETER Name
    Specifies the name of the item to be changed.
     
 [!]Once you specify the '-Template'/'-Job' switch, this parameter will
    autocomplete to valid names for the respective item type.
     
.PARAMETER Path
    Specifies the new path of the location of the configuration file to use.
     
.PARAMETER Variable
    Specifies the name of the variable to change the value of for a job.
     
 [!]Once you specify a valid '-Name' when using the '-Job' switch, this
    parameter will autocomplete to valid names of variables within this job.
     
.PARAMETER Value
    Specifies the new value of the variable being changed.
     
.PARAMETER Update
    Updates the variables of a job to match with what the defined configuration
    file has defined.
     
.PARAMETER WhatIf
    Shows what would happen if the cmdlet runs. The cmdlet does not run.
     
.PARAMETER Confirm
    Prompts you for confirmation before running any state-altering actions
    in this cmdlet.
     
.INPUTS
    System.String
        You can pipe the name of the item to change.
     
.OUTPUTS
    YoutubeDlTemplate
    YoutubeDlJob
     
.NOTES
    When changing a job using the '-Job' switch, a dynamic parameter
    corresponding to each NEW variable definition, found within the
    configuration file, will be generated. The parameter sets the initial
    value of the variable to make the job ready for execution.
     
    For detailed help regarding updating a job, see the
    "CHANGING THE PROPERTIES OF A JOB" section in the help at:
    'about_ytdlWrapper_jobs'.
     
    This cmdlet is aliased by default to 'sydl'.
     
.EXAMPLE
    PS C:\> Set-YoutubeDlItem -Template -Name "music" -Path ~\new\music.conf
     
    Changes the path of the location of the configuration file, for the
    youtube-dl template named "music".
     
.EXAMPLE
    PS C:\> Set-YoutubeDlItem -Job -Name "archive" -Path ~\new\archive.conf
                 
    Changes the path of the location of the configuration file, for the
    youtube-dl job named "archive".
     
.EXAMPLE
    Assuming the job 'archive' has a variable "Autonumber"=5
     
    PS C:\> Set-YoutubeDlItem -Job -Name "archive" -Variable "Autonumber"
                -Value "100"
     
    Changes the "Autonumber" variable of the job named "archive" to the new
    value of "100". The next time the job will be run, this new value will
    be used.
     
.EXAMPLE
    Assuming the job 'archive' has the variables "Autonumber"=5 and
    "Format"=best.
     
    Assuming the configuration file has the variable definitions "Autonumber"
    and "Quality".
     
    PS C:\> Set-YoutubeDlItem -Job -Name "archive" -Update -Quality "normal"
     
    Updates the job named "archive" to reflect its modified configuration file.
    The configuration file has a new variable named "Quality", whose initial
    value is provided through the '-Quality' parameter. The configuration file
    lacks the "Format" variable now, so that is deleted from the job.
     
.LINK
    Get-YoutubeDlItem
    Set-YoutubeDlItem
    Remove-YoutubeDlItem
    about_ytdlWrapper
     
#>

function Set-YoutubeDlItem
{
    [Alias("sydl")]
    
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Template")]
        [switch]
        $Template,
        
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Job-Path")]
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Job-Update")]
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = "Job-Property")]
        [switch]
        $Job,
        
        [Parameter(Position = 1, Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [string]
        $Name,
        
        [Parameter(Position = 2, Mandatory = $true, ParameterSetName = "Template")]
        [Parameter(Position = 2, Mandatory = $true, ParameterSetName = "Job-Path")]
        [Alias("ConfigurationFilePath")]
        [string]
        $Path,
        
        [Parameter(Position = 2, Mandatory = $true, ParameterSetName = "Job-Update")]
        [switch]
        $Update,
        
        [Parameter(Position = 2, Mandatory = $true, ParameterSetName = "Job-Property")]
        [string]
        $Variable,
        
        [Parameter(Position = 3, Mandatory = $true, ParameterSetName = "Job-Property")]
        $Value        
        
    )
    
    dynamicparam
    {
        # Only run the variable detection logic if a job is given, and the job
        # exists, and it has a valid configuration file path, and the '-Update'
        # switch is on.
        if (-not $Job) { return }
        if ($null -eq $Name) { return }
        $jobList = Read-Jobs
        $jobObject = $jobList | Where-Object { $_.Name -eq $Name }
        if ($null -eq $jobObject) { return }
        if ($jobObject.GetState() -eq "InvalidPath") { return }
        if (-not $Update) { return }
        
        # Figure out which are the new variables in the configuration file
        # to add parameters for.
        $jobVariables = $jobObject.GetStoredVariables()
        $configVariables = $jobObject.GetVariables()
        $newVariables = $configVariables | Where-Object { $jobVariables -notcontains $_ }
        
        #Define the dynamic parameter dictionary to add all new parameters to
        $parameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        
        # Now that a list of all new variable definitions is found, create a dynamic parameter for each
        foreach ($variable in $newVariables)
        {
            $paramAttribute = New-Object System.Management.Automation.ParameterAttribute
            $paramAttribute.Mandatory = $true
            $paramAttribute.ParameterSetName = "Job-Update"
            
            $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($paramAttribute)                
            $param = New-Object System.Management.Automation.RuntimeDefinedParameter($variable, [String], `
                $attributeCollection)
            
            $parameterDictionary.Add($variable, $param)
        }
        
        # Create parameters for every uninitialised variable.
        foreach ($variable in $jobObject.GetNullVariables())
        {
            $paramAttribute = New-Object System.Management.Automation.ParameterAttribute
            $paramAttribute.Mandatory = $true
            $paramAttribute.ParameterSetName = "Job-Update"
            
            $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($paramAttribute)                
            $param = New-Object System.Management.Automation.RuntimeDefinedParameter($variable, [String], `
                $attributeCollection)
            
            $parameterDictionary.Add($variable, $param)
        }
        
        return $parameterDictionary
    }
    
    process
    {
        if ($Template)
        {
            # If the template doesn't exist, warn the user.
            $templateList = Read-Templates
            $templateObject = $templateList | Where-Object { $_.Name -eq $Name }
            Write-Verbose "Validating parameters and the configuration file."
            if ($null -eq $templateObject)
            {
                Write-Error "There is no template named: '$Name'."
                return
            }
            
            # Validate that the new configuration file exists and can be used.
            if ([YoutubeDlTemplate]::HasInvalidPath($Path))
            {
                Write-Error "The configuration file path: '$Path' is invalid."
                return
            }
            if ([YoutubeDlTemplate]::HasNoInput($Path))
            {
                Write-Error "The configuration file located at: '$Path' has no input definitions.`nFor help regarding the configuration file, see the `"SETTING UP A CONFIGURATION FILE`" section in the help at: `'about_ytdlWrapper_templates`'."
                    return
            }
            
            Write-Verbose "Changing the path property of the template object."
            $templateObject.Path = $Path
            
            if ($PSCmdlet.ShouldProcess("Updating database at '$script:TemplateData' with the changes.", "Are you sure you want to update the database at '$script:TemplateData' with the changes?", "Save File Prompt"))
            {
                Export-Clixml -Path $script:TemplateData -InputObject $templateList -Force -WhatIf:$false `
                    -Confirm:$false | Out-Null
            }
        }
        elseif ($Job -and -not $Update)
        {
            # If the job doesn't exist, warn the user.
            $jobList = Read-Jobs
            $jobObject = $jobList | Where-Object { $_.Name -eq $Name }
            Write-Verbose "Validating parameters and the configuration file."
            if ($null -eq $jobObject)
            {
                Write-Error "There is no job named: '$Name'."
                return
            }
            
            if ($Path)
            {
                if ([YoutubeDlJob]::HasInvalidPath($Path))
                {
                    Write-Error "The configuration file path: '$Path' is invalid."
                    return
                }
                
                Write-Verbose "Changing the path property of the job object."
                $jobObject.Path = $Path
            }
            else
            {
                # Validate that the job can be used.
                if ($jobObject.HasInvalidPath())
                {
                    Write-Error "The configuration file path: '$Path' is invalid."
                    return
                }
                if ($jobObject.HasInputs())
                {
                    Write-Error "The configuration file at: '$Path' has input definitions, which a job cannot have.`nFor help regarding the configuration file, see the `"SETTING UP A CONFIGURATION FILE`" section in the help at: `'about_ytdlWrapper_jobs`'."
                    return
                }
                
                # Validate that the variable-to-modify exists.
                if ($jobObject._Variables.Keys -notcontains $Variable)
                {
                    Write-Error "The job: '$name' does not contain the variable named: '$Variable'!"
                    return
                }
                
                if ([System.String]::IsNullOrWhiteSpace($Value)) 
                {
                    Write-Error "The new value for the variable: '$Variable' cannot be empty!"
                    return
                }
                
                Write-Verbose "Changing the variable property of the job object."
                $jobObject._Variables[$Variable] = $Value
            }
            
            if ($PSCmdlet.ShouldProcess("Updating database at '$script:JobData' with the changes.", "Are you sure you want to update the database at '$script:JobData' with the changes?", "Save File Prompt"))
            {
                Export-Clixml -Path $script:JobData -InputObject $jobList -WhatIf:$false -Confirm:$false `
                    | Out-Null
            }
            
        }
        elseif ($Job -and $Update)
        {
            # If the job doesn't exist, warn the user.
            $jobList = Read-Jobs
            $jobObject = $jobList | Where-Object { $_.Name -eq $Name }
            Write-Verbose "Validating parameters and the configuration file."
            if ($null -eq $jobObject)
            {
                Write-Error "There is no job named: '$Name'."
                return
            }
            
            # Validate that the job can be used.
            if ($jobObject.HasInvalidPath())
            {
                Write-Error "The configuration file path: '$Path' is invalid."
                return
            }
            if ($jobObject.HasInputs())
            {
                Write-Error "The configuration file at: '$Path' has input definitions, which a job cannot have.`nFor help regarding the configuration file, see the `"SETTING UP A CONFIGURATION FILE`" section in the help at: `'about_ytdlWrapper_jobs`'."
                return
            }
            
            # Figure out which are the new variables in the configuration file
            # and which variables in the job (may) need to be removed.
            $jobVariables = $jobObject.GetStoredVariables()
            $configVariables = $jobObject.GetVariables()
            $newVariables = $configVariables | Where-Object { $jobVariables -notcontains $_ }
            $oldVariables = $jobVariables | Where-Object { $configVariables -notcontains $_ }
            
            $variableList = $jobObject._Variables
            # First remove all of the not-needed-anymore variables from the
            # hashtable.
            foreach ($key in $oldVariables)
            {
                $variableList.Remove($key)
            }
            # Then add all of the new variables which need an initial value
            # before the job can be ran.
            foreach ($key in $newVariables)
            {
                if ($PSBoundParameters.ContainsKey($key))
                {
                    $variableList[$key] = $PSBoundParameters[$key]
                }
                else
                {
                    Write-Error "The new variable: '$key' has not been provided an initial value as a parameter!"
                    return
                }
            }
            # Then set the values of any uninitialised variables too.
            foreach ($key in $jobObject.GetNullVariables())
            {
                if ($PSBoundParameters.ContainsKey($key))
                {
                    $variableList[$key] = $PSBoundParameters[$key]
                }
                else
                {
                    Write-Error "The existing variable: '$key' has not been provided an initial value as a parameter!"
                    return
                }
            }
            
            # Set the modified variable hashtable.
            Write-Verbose "Updating the variables of the job object."
            $jobObject._Variables = $variableList
            if ($PSCmdlet.ShouldProcess("Updating database at '$script:JobData' with the changes.", "Are you sure you want to update the database at '$script:JobData' with the changes?", "Save File Prompt"))
            {
                Export-Clixml -Path $script:JobData -InputObject $jobList -WhatIf:$false -Confirm:$false `
                    | Out-Null
            }
        }
    }
}

# Scriptblocks used for tab expansion assignments
$argCompleter_ItemName = {
    param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    
    # Import all objects from the database file depending on the switch.
    $list = if ($fakeBoundParameters.Template)
    {
        Read-Templates
    }
    elseif ($fakeBoundParameters.Job)
    {
        Read-Jobs
    }
    
    if ($list.Count -eq 0) {
        Write-Output ""
    }
    
    # Return the names which match the currently typed in pattern.
    # This first strips the string of any quotation marks, then matches it to
    # the valid names, and then inserts the quotation marks again.
    # This is necessary so that strings with spaces have quotes, otherwise
    # they will not be treated as one parameter.
    $list.Name | Where-Object { $_ -like "$($wordToComplete.Replace(`"`'`", `"`"))*" } | ForEach-Object { "'$_'" }
    
}

$argCompleter_JobVariable = {
    param ($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    
    # Only proceed if specifying a job.
    if ($fakeBoundParameters.Template) { return }
    
    # Get the already typed in job name
    $jobName = $fakeBoundParameters.Name
    
    if ($null -ne $jobName) {
        # Import all youtube-dl.Job objects from the database file
        $jobList = Read-Jobs
        $job = $jobList | Where-Object { $_.Name -eq $jobName }
        
        if ($null -ne $job) {
            # Return the variables which match currently typed in pattern
            $job._Variables.Keys | Where-Object { $_ -like "$($wordToComplete.Replace(`"`'`", `"`"))*" } `
                | ForEach-Object { "'$_'" }
        }
    }
}

# Tab expansion assignements for commands
Register-ArgumentCompleter -CommandName Get-YoutubeDlItem -ParameterName Names -ScriptBlock $argCompleter_ItemName
Register-ArgumentCompleter -CommandName Set-YoutubeDlItem -ParameterName Name -ScriptBlock $argCompleter_ItemName
Register-ArgumentCompleter -CommandName Set-YoutubeDlItem -ParameterName Variable -ScriptBlock $argCompleter_JobVariable
Register-ArgumentCompleter -CommandName Remove-YoutubeDlItem -ParameterName Names -ScriptBlock $argCompleter_ItemName
Register-ArgumentCompleter -CommandName Invoke-YoutubeDL -ParameterName Names -ScriptBlock $argCompleter_ItemName
    #endregion Load compiled code
}

# TEMPLATE DATA MIGRATION
# -----------------------
Write-Debug "Checking for template databse migration"
$templateDatabaseVersion = [Regex]::Match((Get-Item -Path "$Folder\template-database.*.xml" -ErrorAction Ignore), ".*?ytdlWrapper\\template-database.(.*).xml").Groups[1].Value
if ($templateDatabaseVersion -eq "0.2.0")
{
    Write-Debug "`e[4mDetected database version 0.2.0!`e[0m"
    Rename-Item -Path "$Folder\template-database.0.2.0.xml" -NewName "template-database.0.2.1.xml" -Force -WhatIf:$false -Confirm:$false
}

# JOB DATA MIGRATION
# ------------------
Write-Debug "Checking for job database migration"
$jobDatabaseVersion = [Regex]::Match((Get-Item -Path "$Folder\job-database.*.xml" -ErrorAction Ignore), ".*?ytdlWrapper\\job-database.(.*).xml").Groups[1].Value
if ($jobDatabaseVersion -eq "0.2.0")
{
    Write-Debug "`e[4mDetected database version 0.2.0!`e[0m"
    $jobList = New-Object -TypeName System.Collections.Generic.List[YoutubeDlJob]
    $xmlData = Import-Clixml -Path "$Folder\job-database.$jobDatabaseVersion.xml"
    foreach ($item in $xmlData)
    {
        if ($item.pstypenames[0] -eq "Deserialized.YoutubeDlJob")
        {
            $job = [YoutubeDlJob]::new($item.Name, $item.Path, $item._Variables, $null, $null)
            $jobList.Add($job)
        }
    }
    
    Export-Clixml -Path "$Folder\job-database.0.2.1.xml" -InputObject $jobList -WhatIf:$false -Confirm:$false | Out-Null
    Remove-Item -Path "$Folder\job-database.$jobDatabaseVersion.xml" -Force -WhatIf:$false -Confirm:$false | Out-Null
}