# 設置編碼為 UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# 停止 WM_WEB service
Write-Host "檢查 WM_WEB service..."
Get-Service -Name "WM_WEB"
Stop-Service -Name "WM_WEB" -Force

# 備份原本的wmcms資料夾
$today = (Get-Date).ToString("yyyyMMdd_HHmmss")
if (Test-Path "D:\wmweb") {
    # 確保備份目錄存在
    if (-not (Test-Path "D:\bk")) {
        New-Item -ItemType Directory -Path "D:\bk"
    }
    
    # 先將wmcms改名並直接移動到bk目錄
    Rename-Item -Path "D:\wmweb" -NewName "wmweb_bk_$today"
    Move-Item -Path "D:\wmweb_bk_$today" -Destination "D:\bk"
    Write-Host "備份已完成至 D:\bk\wmweb_bk_$today"
}

# 解壓縮 WMCMS.zip 檔案到wmcms資料夾
Write-Host "解壓縮 wmweb.zip ..."
Write-Host "`n`n"
Expand-Archive -Path "wmweb.zip" -DestinationPath "D:\wmweb" -Force
Write-Host "`n"

# 重啟 Windows 服務
Write-Host "重啟 WM_WEB service..."
Start-Service -Name "WM_WEB"

Write-Host "Done!"
pause