HOW TO PROGRAM AUTOMATION

<#missing components
sync to Server, logging, sorting by last update to report, refactor story to single format.
#>

#region default program
Remove-Variable * -ErrorAction SilentlyContinue
$date = Get-Date
$date_format_unix = [int](($date).ToUniversalTime()-([datetime]"1970-01-01T00:00:00Z").ToUniversalTime()).TotalSeconds
$date_format_string = $date.tostring("yyyy-MM-dd HH:mm:ss")
$date_format_file = $date.tostring("yyyyMMddHHmmss")
$program_directory = "C:\_young\automation"
$program_library = "$program_directory\library"
#endregion default program

#region list colors 4096 colors from 16777216
foreach ($i in @(1..4096)) {
    $list += @($list[-1]+4096)
    $value1 = $list[$i-1]-1
    $value2 = $list[$i]-1
    
}
#>

#region get story_main
$database_dir = "$program_directory\database"
$database_file_latest = $((Get-ChildItem $database_dir | Sort-Object LastWriteTime -Descending)[0])
$story_main = Import-Clixml $database_file_latest.FullName
$story_main | Add-Member -NotePropertyName "Found" -NotePropertyValue $False -Force
#endregion get story_main

#region scriptblock to add new to story_main
$story_add_new = {
    $object = [PSCustomObject]@{}
    @($story_main[0] | Get-Member | Where-Object {($_.MemberType -eq "NoteProperty")}).name | % {
        $object | Add-Member -NotePropertyName $_ -NotePropertyValue $(switch ($_) {
            'UniqueID'    { $(($story_main | Sort-Object 'C++' -Descending)[0].'C++')+1 }
            'Name'   { $($i.name) }
            "FullPath"   { $($i.FullName) }
            "URL" {$false}
            "Update" {$date_format_string}
            "Status" { "New" }
            "Contents"{ @($(Get-ChildItem $i.FullName).FullName | % {$_}) }
            Default { "New Property Detected";$Error = $i}
        })
    }
    $story_main += $object
}
#endregion

#region get story_release
$story_release_dir = "$program_directory\story_release"
$story_release_list = Get-ChildItem $story_release_dir
#endregion get story_release

#region story_release and update story_main
foreach ($i in $story_release_list) {
    $found = $false
    foreach ($i2 in $story_main) {
        if ($i.name -eq $i2.Name) {
            $found = $true
            $i2.Status = "Release"
            break
        }
    }
    if ($found -eq $false) {
        $story_add_new
    } 
}
#endregion story_release and update story_main

#region get story
$story_dir = "$program_directory\story"
$story_list = Get-ChildItem $story_dir
#endregion get story

#region story and update story_main
foreach ($i in $story_list) {
    $found = $false
    foreach ($i2 in $story_main) {
        if ($i.name -eq $i2.Name) {
            $found = $true
            $i2.Status = "Sync Pending"
            break    
        }
    }
    if ($found -eq $false) {
        $story_add_new
    } 
}
#endregion story and update story

#region story status update 
foreach ($i in $story_main) {
    if (($i.status -ne "Complete") -or ($i.status -ne "Release")) {
        if ($i.sync -eq $true) {
            $i.status = "Synced"
        } 
    }
}
#endregion story status update 

#region re-organize stories by status
$story_main | Group-Object status | Sort-Object Count | % {$temp += $_.group}
$story_main = $temp 
#endregion

<#region mistakes happen, learn to be wrong and be okay. you cannot learn unless you are wrong. wisest words from YC for those who are afraid to be wrong.
#to peace of mind, brain storming
#to iterate through using foreach is faster than all verbs, as all verbs require foreach as to group-object iterates as foreach.
#to have the below is a peace of mind until ready to move on.
#to say it the first time is the correct answer, iterate through.
#to have it be Divine, it is to say God loves all.
if (@($($story_main | Group-Object Found)).count -ne 1) {
    pause
}

$story_main2 = @()
foreach ($i in $story_main) {
    $story_main2 += ([pscustomobject]@{    
        UniqueID = $i.'C++'
        Name = $i.Name
        FullPath = $i.Path
        URL = $i.'Closure Required On'
        Update = $i.'Last Update Date'
        Status = $i.status
        Contents = @($(Get-ChildItem $i.FullPath).FullName | % {$_})
    })
}
$story_main = $story_main2
foreach ($i in $story_main) {
    #if (!($i.status -eq "Other")) {
        if ($i.found -eq $false) {
            pause
        }
    #}
}
endregion#>


$list = Get-ChildItem
$regex = [regex]'[a-zA-Z0-9]+'
#$regex = [regex]'[a-zA-Z0-9-_]*' 
$list | Group-Object attributes
foreach ($i in $list) {
    if ($i.Attributes -eq "Archive") {
        $temp = "$((($regex.matches($($i.Name.ToLower()))).value -join "_"))"
        $dir = "C:\#young\_divineyc\#story\$($temp)"
        $temp
        $dir
        mkdir "$($dir)"
        mv $($i.FullName) "$($dir)\$($i.name)"
    }
}    
    
    mkdir "C:\#young\_divineyc\#story
}
#>