Imports System.IO
Imports IntraSell_DLL

''' <summary>
''' Lösche alte Dateien
''' </summary>
''' <remarks></remarks>
Public Class ClassCleanUp

    Public Shared Sub CleanUpOnShutDown()
        Dim lastClean As Date = VarValue_Default(LETZTES_CLEANUP_AM, Now)
        If lastClean.AddDays(7) < Now Then
            CleanUp()
            SetVarValue(LETZTES_CLEANUP_AM, Now)
        End If
    End Sub

    Public Shared Sub CleanUp()
        Try
            WriteLog("Cleanup old files ")
            RemoveOldUpdateFiles()
            RemoveFilesByName("google*.*")
            RemoveFilesByName("qr*.png")
            RemoveFilesByName("*.bak")
            RemoveFilesByName("*.pos")
            RemoveFilesByName("AR*.txt")
            RemoveFilesByName("AU*.txt")
            RemoveFilesByName("AN*.txt")
            RemoveFilesByName("LI*.txt")
            RemoveFilesByName("tconn*.xml")
            RemoveFilesByName("order*.xml")
            RemoveFilesByName("rechnung*.xml")
            RemoveFilesByName("rechnung*.xml")
            RemoveFilesByName("*order*.xml")
            RemoveFilesByName("*.log")
            RemoveFilesByName("mjam2intrasell*.*")
            RemoveFilesByName("order*.*")
            RemoveFilesByName("out*.*")
            RemoveFilesByName("*.gzip")
            RemoveFilesByName("*.pdb")
            RemoveFilesByName("AR*.html")
            RemoveFilesByName("AN*.html")
            RemoveFilesByName("AU*.html")
            RemoveFilesByName("LI*.html")
            RemoveFilesByName("*.doc")
            RemoveFilesByName("*.csv")
            RemoveFilesByName("EXP_*.*")
            RemoveFilesByName("A*_*.pdf")
            Process.Start("cmd", "/c del in\*.xml")

            RemoveDirByName("tmp")
            RemoveDirByName("Testdaten")
            RemoveDirByName("archive")
            RemoveDirByName("IIS_Express")
            RemoveDirByName("IntraSell_Net_Web")

        Catch ex As Exception
            WriteLog("Error during cleanup " + ex.Message)
        End Try
    End Sub

    Private Shared Sub RemoveDirByName(ByVal dirname As String)
        Try
            If Directory.Exists(dirname) Then
                Directory.Delete(dirname, recursive:=True)
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="filename">Beispiel:  "*.zip"</param>
    ''' <remarks></remarks>
    Public Shared Sub RemoveFilesByName(filename)
        WriteLog("RemoveFilesByName " + filename)
        Try
            Dim appPath = GetAppPath()
            Dim allZipFiles = Directory.GetFiles(appPath, filename, SearchOption.TopDirectoryOnly)
            For Each file In allZipFiles
                IO.File.Delete(file)
            Next
        Catch ex As Exception
            WriteLog("Error during RemoveFilesByName " + ex.Message)
        End Try
    End Sub

    Public Shared Sub RemoveOldUpdateFiles()
        WriteLog("Cleanup old update files ")
        Try
            If Not IO.File.Exists("update.txt") Then
                Exit Sub
            End If
            Dim appPath = GetAppPath()
            Dim updateContent = File.ReadAllText("update.txt")
            If updateContent.Length > 1000 Then
                Dim allZipFiles = Directory.GetFiles(appPath, "*.zip", SearchOption.TopDirectoryOnly)
                For Each file In allZipFiles
                    Dim filenameOnly As String = file.Replace(appPath, "")
                    If Not updateContent.Contains(filenameOnly) Then
                        IO.File.Delete(file)
                    End If
                Next
            End If
        Catch ex As Exception
            WriteLog("Error during RemoveOldUpdateFiles " + ex.Message)
        End Try
    End Sub

End Class
