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


function Install {
    param (
        [string]$Version
    )

    #查找进程，找到后结束进程
    $redisProcess = Get-Process -Name "redis_server" -ErrorAction SilentlyContinue
    if ($redisProcess) {
        Stop-Process -Name "redis_server" -Force
    }
    $redisProcess = Get-Process -Name "redis-server" -ErrorAction SilentlyContinue
    if ($redisProcess) {
        Stop-Process -Name "redis-server" -Force
    }

    Lang-Print -Msg "正在安装 {}: {}" -Arguments @("Redis", $Version)
    if (Get-Service -Name redis -ErrorAction SilentlyContinue) {
        Invoke-Cmd -Command "sc stop redis"
        Delete-Panel-Service -ServiceName "redis"
    }
    # 定义 Redis 安装路径
    $redis_dir_path = "$SOFT_PATH/redis"
   
    $is_2008 = $false
    $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
    if ($osInfo.Version -eq "6.1") {
        $is_2008 = $true
    }
    if ($is_2008) {
        $Version = "3.2"       
    }
    $redis_package_path = "$SOFT_PATH/temp/redis.zip"
    $DownUrl = "$DownNode/win/redis/redis$Version.zip"
    
    
    Download-File -Url $DownUrl -OutputPath $redis_package_path

    Lang-Print -Msg "正在解压 {} 安装包" -Arguments @("Redis")
    Un-Zip -FilePath $redis_package_path -DestinationPath $SOFT_PATH

    if (-not (Test-Path $redis_dir_path)) {
        New-Item -Path $redis_dir_path -ItemType Directory -Force
    }
    Remove-VersionFiles -Path $redis_dir_path

    $redis_list = @("3.2","5.0","5.1","5.2","5.3","5.4","5.5")
    if ($Version -in $redis_list) {
        $redis_server_file = "$SOFT_PATH/redis/redis-server.exe"
        Install-Service -ServiceName "redis" -BinaryPathName "`"$redis_server_file`" --service-run redis.windows.conf --loglevel verbose" -DisplayName "Redis" -Description "Redis Key-Value Store"
    } else {
        $redis_server_file = "$SOFT_PATH/redis/redis_server.exe"
        Install-Service -ServiceName "redis" -BinaryPathName "`"$redis_server_file`"" -DisplayName "Redis" -Description "Redis Key-Value Store"
    }
    Start-Service -Name redis

    Lang-Print -Msg "安装完成 {}: {}" -Arguments @("Redis", $Version)
    if ($is_2008) {
        Lang-Print -Msg "当前系统为 Windows Server 2008 或 Windows 7，Redis 版本将使用 {}" -Arguments @($Version)
    } 
    Write-Host "Successfully"
}

function UnInstall {
    Lang-Print -Msg "正在卸载 {} 服务" -Arguments @("Redis")

    if (Get-Service -Name redis -ErrorAction SilentlyContinue) {
        Stop-Service -Name redis -Force
        Delete-Panel-Service -ServiceName "redis"
    }

    $redis_dir_path = "$SOFT_PATH/redis"
    if (Test-Path $redis_dir_path) {
        Remove-Item -Path $redis_dir_path -Recurse -Force
    }
    
    Lang-Print -Msg "{} 服务卸载完成" -Arguments @("Redis")
}


    # def update_soft(self,downurl):
    #     public.kill('redis_server.exe')
    #     public.kill('redis-server.exe')
    #     path = self._setup_path + '/redis'
    #     sfile = path +'/redis.windows.conf'
    #     dfile = path + 'redis.windows.conf.backup'
    #     shutil.copy (sfile,dfile)
    #     rRet = self.install_soft(downurl)
    #     if not rRet['status'] : rRet;
    #     if public.set_server_status(self._name,'stop'):
    #         shutil.copy (dfile,sfile)
    #         os.remove(dfile);
    #         if public.set_server_status(self._name,'start'):
    #              return public.returnMsg(True,'更新成功!');
    #     return public.returnMsg(False,'更新失败!');


function Upgrade() {
    param (
        [string]$Version
    )
    Lang-Print -Msg "正在升级 {}" -Arguments @("Redis")
    
    Stop-Process -Name "redis_server.exe" -Force
    Stop-Process -Name "redis-server.exe" -Force
    Stop-Service -Name redis -Force

    $conf_file = "$SOFT_PATH/redis/redis.windows.conf"
    Back-ConfigFile -FilePath $conf_file

    Install -Version $Version
    Restore-ConfigFile -FilePath $conf_file

    Start-Service -Name redis
    Lang-Print -Msg "{} 升级成功" -Arguments @("Redis")
}

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