D:\OneDrive\Programs\bin\task.ps1
Clear-Host

#Variables
switch ($env:COMPUTERNAME){
    "WORKSTATION"
    {
        $Env:dirDownload = 'F:\Library'
        $Env:dirJson = 'F:\Library\gallery-dl'
    }
    "SURFACE"    
    {
        $Env:dirDownload = 'C:\Users\JawZ\Downloads'
        $Env:dirJson = '\\WORKSTATION\Library\gallery-dl'
    }
}

$Env:database = "$Env:dirJson\manga.sqlite3"
$mAll= "$Env:dirJson\manga.json"
$mVol = "$Env:dirJson\manga-i(vol).json"
$list = "$Env:dirJson\manga.txt"
$downloadList_1 = "$Env:dirJson\download_1.txt"
$downloadList_2 = "$Env:dirJson\download_2.txt"
$downloadList_3 = "$Env:dirJson\download_3.txt"
$downloadList_4 = "$Env:dirJson\download_4.txt"
$downloadList_5 = "$Env:dirJson\download_5.txt"
$textInfo = (Get-Culture).TextInfo

function removeEmptyDirectory{
    remove-emtpy.ps1 -Path .\ -Remove -VerifyNoEmpty
}

function Write-Separator{Write-Color '---' -Color DarkGray}

function email ($body){
    $EmailFrom = "jawzoned@gmail.com"
    $EmailTo = "captainjawz@outlook.com"
    $Subject = "New files on the library! "
    $SMTPServer = "smtp.gmail.com"
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("jawzoned", "od3g4t!T&yJq");
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, "$Body")
}
function automaticManga{#Download manga automatically
    foreach ($webtoon in [System.IO.File]::ReadLines("$list")){	
        if ($webtoon | Select-String -Pattern 'webtoon'){	
            gallery-dl --sleep 5 --download-archive $Env:database -c "$mAll" --chapter-range '1' "$webtoon" 	
        } 
    }
    foreach ($manga in [System.IO.File]::ReadLines("$list")){	
        if ($manga | Select-String -Pattern 'manga'){	
            gallery-dl --sleep 5 --download-archive $Env:database -c "$mAll" --chapter-range '1-5' "$manga" 	
        } 
    }
    Set-Location $Env:dirDownload\New
    removeEmptyDirectory
    $newDirectories = Get-ChildItem -Path "$Env:dirDownload\New" -Directory -Recurse 
    if ($newDirectories){
        $body = $newDirectories.Name | Out-String
        email $body
    }
}

function mangaRange($range){#Download manga automatically
    $counter = -1
    Write-Separator
    foreach ($line in [System.IO.File]::ReadLines($list)){
        $counter++
        if ($line | Select-String -Pattern 'manga'){	
            $lineName = $line.Substring(($line.LastIndexOf('/')+1)).Replace("-", " ").Replace("_", " ") #Fix the name so it is presentable
            $lineName = $textInfo.ToTitleCase($lineName)
            Write-Color "$counter) ", "$lineName" -Color White, Yellow
        } 
    }
    Write-Separator
    Write-Color 'Make a choice:' -C White
    Write-Separator
    $selection = Read-Host
    Clear-Host
    #[string]$range = "'" + $range + "'"
    $line = [System.IO.File]::ReadLines($list) | Select-Object -Index $selection
    gallery-dl --download-archive $Env:database -c $mAll --chapter-range $range $line
    Set-Location $Env:dirDownload\New
    removeEmptyDirectory
}

$DO = $args[0]
if($DO){
    Switch($DO){
        'a'{automaticManga}
        'edit'{
            Write-Color '1) ', 'Edit manga list' -C White, Yellow
            Write-Color '2) ', 'Edit download list 1' -C White, Yellow
            Write-Color '3) ', 'Edit download list 2' -C White, Yellow
            Write-Color '4) ', 'Edit download list 3' -C White, Yellow
            Write-Color '5) ', 'Edit download list 4' -C White, Yellow
            Write-Color '6) ', 'Edit download list 5' -C White, Yellow
            Write-Separator
            $selectOperation = Read-Host
            switch($selectOperation){
                '1'{Invoke-Item $list}
                '2'{Invoke-Item $downloadList_1}
                '3'{Invoke-Item $downloadList_2}
                '4'{Invoke-Item $downloadList_3}
                '5'{Invoke-Item $downloadList_4}
                '6'{Invoke-Item $downloadList_5}
            }
        }
    }
}
else{
    #Menu
    Write-Color '1) ', 'Update Manga custom range' -C White, Yellow
    Write-Color '2) ', 'Update comic from list' -C White, Yellow
    Write-Color '3) ', 'Update everything' -C White, Yellow
    Write-Color '4) ', 'Download individual comic/manga' -C White, Yellow
    Write-Color '5) ', 'Download individual comic/manga per Volume' -C White, Yellow
    Write-Color '6) ', 'Download batch' -C White, Yellow
    Write-Color '7) ', 'Sort list file'-C White, Yellow
    Write-Separator
    #Actions
    if ((Test-Path $Env:database) -and (Test-Path $mAll) -and (Test-Path $mVol) -and (Test-Path $list) -and (Test-Path $Env:dirDownload)){

        $selectOperation = Read-Host
        Write-Separator
        switch ($selectOperation){
            '1' {
                Write-Color 'Insert a custom range ', '(ex 1-10)' -C Gray, DarkGray
                Write-Separator
                $range = Read-Host
                mangaRange $range
            }
            '2' { #Update comic
                $userInput = ''
                Do  {
                    Clear-Host
                    $counter = -1
                    foreach ($line in [System.IO.File]::ReadLines($list)) { #Print each lines from the list containing Comic pattern
                        $counter++
                        if ($line | Select-String -Pattern 'readcomiconline') {
                            $lineName = $line.Substring(($line.LastIndexOf('/')+1)).Replace("-", " ").Replace("_", " ") #Fix the name so it is presentable
                            $lineName = $textInfo.ToTitleCase($lineName)
                            Write-Color "$counter) ", "$lineName" -Color White, Yellow
                        }
                    }
                    Write-Separator
                    Write-Color 'Make a selection:' -C White
                    Write-Separator
                    $selection = Read-Host
                    Clear-Host
                    $line = [System.IO.File]::ReadLines($list) | Select-Object -Index $selection
                    $lineName = $line.Substring(($line.LastIndexOf('/')+1)).Replace("_", " ").Replace("-"," ") #Fix name so it is presentable
                    $lineName = $textInfo.ToTitleCase($lineName)
                    Write-Color 'Your selection is: ', $lineName -C White, Yellow
                    Write-Separator
                    Write-Color 'Is your choice correct?', '(y/n)' -C White, DarkGray
                    Write-Separator
                    $userInput = Read-Host #Ask user if selection is correct
                } while (($userInput -eq '') -or ($userInput -eq 'n')) #if selection is incorrect reprint the list
                Clear-Host
                gallery-dl --download-archive $Env:database -c $mAll --chapter-range '1' "$line"
            }
            '3'{
                Clear-Host
                gallery-dl --download-archive $Env:database -c "$mAll" -i "$list"
            }
            '4'{
                Clear-Host
                Write-Separator
                Write-Color 'Enter link' -C White
                Write-Separator
                $link = Read-Host
                Write-Separator
                Write-Color 'Do you want to use the Database?', '(y/n)' -C White, DarkGray
                Write-Separator
                $useDB = Read-Host
                Write-Separator
                Clear-Host
                if ($useDB -eq 'y'){
                    gallery-dl  --download-archive $Env:database -c "$mAll" "$link"
                }
                else{
                    gallery-dl -c "$mAll" "$link"
                }
                
            }
            '5'{
                Clear-Host
                Write-Color 'Enter link:' -C White
                Write-Separator
                $link = Read-Host
                Write-Separator
                Clear-Host
                gallery-dl --download-archive $Env:database -c "$mVol" "$link"
            }
            '6'{
                #$date = get-date -Format '(ddMMyyy_hhmm)'
                #$log = "$Env:dirJson\logs\log $date.txt"
                Clear-Host
                Write-Color 'Which list do you want to use?' -C White
                Write-Separator
                $listNumber = Read-Host
                switch ($listNumber) {
                    '1'{gallery-dl --sleep 0 -c "$mAll" -i "$downloadList_1"}
                    '2'{gallery-dl --sleep 0 -c "$mAll" -i "$downloadList_2"}
                    '3'{gallery-dl --sleep 0 -c "$mAll" -i "$downloadList_3"}
                    '4'{gallery-dl --sleep 0 -c "$mAll" -i "$downloadList_4"}
                    '5'{gallery-dl --sleep 2 --download-archive $Env:database -c "$mAll" -i "$downloadList_5"}
                }
                
            }
            '7'{
                $finalContent = @()
                $content = (Get-Content $list).ToLower() | Sort-Object | Select-Object -Unique
                foreach ($line in $content){
                    [string]$line = $line
                    <#$line = $line.Replace('http:', 'https:')
                    if ($line -Match '\/$'){	
                        $line = $line.TrimEnd('/')
                    }#>
                    $finalContent += $line
                }
                $finalContent -match '\S' > $list
            }
        }
    }

    else {
        Clear-Host
        Write-Output 'ERROR: One of the configuration files is missing'
        Write-Output "Database"
        Test-Path $Env:database
        Write-Output "manga.json"
        Test-Path $mAll
        Write-Output "manga-1(vol).json"
        Test-Path $mVol
        Write-Output "Manga list.txt"
        Test-Path $list
        Write-Output "Download directory"
        Test-Path $Env:dirDownload
    }
}
function Capitalize{
    #Set-Location 'F:\Library\New'
	<#foreach ($directory in Get-ChildItem -Recurse -Directory){
		Set-Location $directory
        $capitalizeFiles = Get-ChildItem -File
        ReNamer.exe /silent /rename Capitalize $capitalizeFiles
        Wait-Process -Name "ReNamer"
	}#>
    Set-Location 'F:\Library\New'
	foreach ($directory in Get-ChildItem -Directory -Recurse){
        $directory.FullName
		ReNamer.exe /silent /rename Capitalize $directory.FullName 
		Wait-Process -Name "ReNamer"
	}
}

function moveSeries{
    $serieLocationNames= @(
    "Amory Wars"
	"Batman"
	"Borderlands"
	"Brit"
	"Clive Barker"
	"Colder"
	"Constantine"
	"Critical Role"
	"Dark Ark"
	"Deadpool"
	"Dicks"
	"Ether"
	"Fables"
	"Grimm Fairy Tales"
	"Guardians of the Galaxy"
	"Gwenpool"
	"Hack Slash"
	"Harley Quinn"
	"Hellboy"
	"Hercules"
	"Joker"
	"Justice League"
	"Kick-Ass"
	"Lazarus"
	"Lobo"
	"Mad Max"
	"Midnighter"
	"Nailbiter"
	"Pilot Season"
	"Punisher"
	"Rumble"
	"Sonic"
	"Star Wars"
	"Suicide Squad"
	"The Boys"
	"The Last Zombie"
	"The Realm"
	"The Walking Dead"
	"Thor"
	"Transformers"
	"Venom"
	"Vikings"
	"WWE"
	"X-Men"
	"Wolverine"
	"Battle Angel Alita"
	"Battle Through The Heavens"
	"Boku No Hero Academia"
	"Dr. Stone"
	"Franken Fran"
	"Kakegurui"
	"Kami-Sama No Iutoori"
	"Shaman King"
	"Tensei Shitara Slime Datta Ken"
	"Touhou"
    )

    foreach ($serieLocationName in $serieLocationNames){
        $testPathComicLocation = 'F:\Library\New\Comic\' + $serieLocationName
        $testPathMangaLocation = 'F:\Library\New\Manga\' + $serieLocationName
        if(Test-Path $testPathComicLocation){
            $files = Get-ChildItem -Path $testPathComicLocation
            $newDir = $testPathComicLocation + '\' + $serieLocationName
            if(!(Test-Path $newDir)){mkdir $newDir}
            Move-Item $files $newDir
        }
        if(Test-Path $testPathMangaLocation){
            $files = Get-ChildItem -Path $testPathMangaLocation
            $newDir = $testPathMangaLocation + '\' + $serieLocationName
            if(!(Test-Path $newDir)){mkdir $newDir}
            Move-Item $files $newDir
        }
    }
}
Clear-Host
Capitalize
moveSeries