# Custom backup script

function backup_to_server {

    param(
        [switch]$skipappdir, 
        [switch]$skipspudir,
        [switch]$noupload, 
        [switch]$skipoutlook)

    . (Join-Path $PSScriptRoot handle_exclude_lists.ps1)

    # Backup folder
    $backup_dir = "$HOME"
    if (!(Test-Path $backup_dir) ) { mkdir $backup_dir }

    # Make sure we have an authentication key
    $key_file = "$HOME\.ssh\bethany_putty.ppk"
    if (!(Test-Path $key_file) ) { write-host "Unable to find a key for connecting to the server"; return }

    # Setup to use 7zip and add WinSCP to the path
    $env:Path += ";C:\Program Files\7-Zip;C:\Program Files (x86)\WinSCP"
    if (! (Test-Path 'C:\Program Files\7-Zip\7z.exe')) {
        Write-Error "Unable to locate 7-zip program file"
        exit 1
    }
    Set-Alias sz 'C:\Program Files\7-Zip\7z.exe'


    # Create a compressed version of the App Dir
    if (!($skipappdir)) {

        # Create the main backup file
        $backup_file = "$backup_dir\appdir.7z"

        # Make sure we start in the home folder
        Set-Location $HOME

        # Remove the previous file (start clean each time)
        if (Test-Path $backup_file) { Remove-Item $backup_file }

        # Load the exclude list
        $exclude = parse_appdata_exclude

        # Generate the compressed backup
        Invoke-Command -ScriptBlock { sz a -bb0 -t7z $args[0] $args[1] "$HOME\AppData"
            } -ArgumentList @($exclude, $backup_file)
    }

    # Create a compressed version of the OneDrive - SPU Dir
    if (!($skipspudir)) {

        # Create the main backup file
        $backup_file = "$backup_dir\spudir.7z"

        # Make sure we start in the home folder
        Set-Location $HOME

        # Remove the previous file (start clean each time)
        if (Test-Path $backup_file) { Remove-Item $backup_file }

        # Load the exclude list
        $exclude = parse_appdata_exclude

        # Generate the compressed backup
        Invoke-Command -ScriptBlock { sz a -bb0 -t7z $args[0] "$HOME\OneDrive - Seattle Pacific University"
            } -ArgumentList @($backup_file)
    }
    

    if (!($noupload)) {

        # Create a timestamp file to confirm when the last backup was done
        Set-Content -Value (Get-Date) -Path ("$backup_dir\updated.txt")
        Set-Location $HOME

        # Config file
        $config_file = Join-Path $env:TEMP backup_script.txt
        Set-Content $config_file "open sftp://bethany@gandalf.acosi.lan/ -hostkey=""ssh-ed25519 256 unhOA2cFNFx/xmUsqSihnsnEimflWnma9wJMVH2Enuc"" -privatekey=""$key_file"""
        Add-Content $config_file "option batch continue"
        
        # Sync to server
        $remote_directory = "/data/backups/bethany/{0}/Profiles/" -f ( $env:COMPUTERNAME )
        $local_directory = "c:\Users"
        $mask = parse_sync_exclude
        $config = "synchronize remote -delete -filemask=""$mask"" ""$local_directory"" $remote_directory"
        Add-Content $config_file "$config"
        
        # If requested, sync the outlook data files as well
        if (!($skipoutlook)) {
            $local = "$HOME\AppData\Local\Microsoft\Outlook\*.ost"
            $remote = "/data/backups/bethany/{0}/" -f $env:COMPUTERNAME
            $config = "put -resume ""$local"" $remote"
            Add-Content $config_file $config
        }

        # Complete the config file and execute
        Add-Content $config_file "exit"
        WinSCP.com /script=$config_file
    }
}

Export-ModuleMember backup_to_server