# 主脚本参数
param (
    [string]$Action, 
    [string]$Version
)
. .\comm.ps1 

$FtpServiceName = "FileZilla Server"

function Install {
    param (
        [string]$Version
    )

    #判断服务是否存在
    if (Get-Service -Name $FtpServiceName -ErrorAction SilentlyContinue) {
        Lang-Print -Msg "{} 服务已存在，准备卸载" -Arguments @($FtpServiceName)
        Invoke-Cmd -Command "sc stop `"$FtpServiceName`""
        Invoke-Cmd -Command "sc delete `"$FtpServiceName`""
    }

    $ftp_dir_path = "$SOFT_PATH/ftpServer"
    Lang-Print -Msg "正在安装 {}: {}" -Arguments @($FtpServiceName, $Version)

    # 定义 FTP 安装路径
    $ftp_package_path =  "$SOFT_PATH/temp/ftpServer.zip"
    $DownUrl = "$DownNode/win/ftpServer.zip"
    if (-not (Download-File -Url $DownUrl -OutputPath $ftp_package_path)) {
        Lang-Print -Msg "下载 {} 安装包失败" -Arguments @("FTP")
        return
    }
    Lang-Print -Msg "正在解压 {} 安装包" -Arguments @("FTP")
    # 解压 FTP 安装包
    Un-Zip -FilePath $ftp_package_path -DestinationPath $SOFT_PATH
    
    Lang-Print -Msg "正在安装{} 服务" -Arguments @($FtpServiceName)
    $BinaryPathName = "`"$ftp_dir_path/FileZilla_Server.exe`""
    if (Install-Service -ServiceName $FtpServiceName -BinaryPathName $BinaryPathName -DisplayName "FileZilla Server" -Description "FileZilla FTP Server") {
        Lang-Print -Msg "{} 服务安装成功" -Arguments @($FtpServiceName)
    } else {
        Lang-Print -Msg "{} 服务安装失败" -Arguments @($FtpServiceName)
        return
    }
    Remove-VersionFiles -Path $ftp_dir_path
    $version  = (Get-Item "$ftp_dir_path/FileZilla_Server.exe").VersionInfo.FileVersion

    $version_parts = $version -split ','
    if ($version_parts.Count -ge 3) {
        $major = $version_parts[0]
        $minor = $version_parts[1]
        $build = $version_parts[2]
        $version = "$major.$minor.$build"
        $version = $version -replace ' ', ''
    }
    #写入版本号
    $version_file = "$ftp_dir_path/version.pl"
    if (-not (Test-Path -Path $version_file)) {
        New-Item -Path $version_file -ItemType File -Force | Out-Null
    }
    Set-Content -Path $version_file -Value $version -Force

    # 启动 FTP 服务
    Start-Service -Name $FtpServiceName
    Write-Host "Successfully"
}

function UnInstall {
    Lang-Print -Msg "正在卸载:{}" -Arguments @("FTP")

    # 停止 FTP 服务
    if (Get-Service -Name $FtpServiceName -ErrorAction SilentlyContinue) {
        Stop-Service -Name $FtpServiceName -Force
        Set-Service -Name $FtpServiceName -StartupType Disabled
    }

    # 删除 FTP 服务   
    Invoke-Cmd -Command "sc delete `"$FtpServiceName`""

    Lang-Print -Msg "卸载完成:{}" -Arguments @("FTP")
}

function Upgrade() {
    param (
        [string]$Version
    )
    Lang-Print -Msg "正在升级 {}" -Arguments @($FtpServiceName)

    if (Get-Service -Name $FtpServiceName -ErrorAction SilentlyContinue) {
        Stop-Service -Name $FtpServiceName -Force
    }

    $conf_file = "$SOFT_PATH/ftpServer/FileZilla Server.xml"
    Back-ConfigFile -FilePath $conf_file

    Install -Version $Version

    Restore-ConfigFile -FilePath $conf_file
    Start-Service -Name $FtpServiceName

}

switch ($Action.ToLower()) {
    "install" {       
        Install -Version $Version
    }
    "upgrade" {
        Upgrade -Version $Version
    }
    "uninstall" {
        UnInstall
    }    
}
