# 設定 ASCII 藝術字的內容
$asciiArt = @"
  _____  _____  ______ _____  ______ _      ______           _____ ______ 
 |  __ \|  __ \|  ____|  __ \|  ____| |    |  ____|   /\    / ____|  ____|
 | |__) | |__) | |__  | |__) | |__  | |    | |__     /  \  | (___ | |__   
 |  ___/|  _  /|  __| |  _  /|  __| | |    |  __|   / /\ \  \___ \|  __|  
 | |    | | \ \| |____| | \ \| |____| |____| |____ / ____ \ ____) | |____ 
 |_|    |_|  \_\______|_|  \_\______|______|______/_/    \_\_____/|______|
"@

# 顯示 ASCII 藝術字
Write-Host $asciiArt

#第一次建立專案請先設定ACR Name
$registryname="evdevcontainerregistry"
$fullregistryname="evdevcontainerregistry.azurecr.io"
#第一次建立專案請先設定專案名稱
$imagerepositoryname="dbapi"
#target tag can be {projectname}_RC or RC on pre-release stage
$default_dev_prefix = "DockerEBUS_test_"
$target_tag="DockerEBUS_RC"

#檢查是否有尚未commit的更新
$status = git status --porcelain
if (-not $status) {
	Write-Output "All changes committed"
} else {
    Write-Output "There are uncommitted changes"
	exit
}

#檢查remote是否有新commit未處理
git fetch --tags -f
$status = git status --porcelain -b
if (($status -match "ahead") -or ($status -match "behind") -or ($status -match "gone") )
{
	Write-Output "git branch not synced"
	exit
}
else {
    Write-Output "git branch is synced"
}

#podman acr登入
Write-Host "ACR Login....."
$token = az acr login --name $registryname --expose-token --output tsv --query accessToken
$user = "00000000-0000-0000-0000-000000000000"
podman login $fullregistryname -u $user -p $token

$source_tag = Read-Host "Please type the source tag that you want to do RC."
#預設跟Dev_build一樣
if (-not $source_tag) {
	$username = az account show --query user.name
	$username = $username.TrimStart("""").Split('@')[0]
	$source_tag=$default_dev_prefix+$username
}
$sourcetag_response = Read-Host "Do you want to do the RC tag with $source_tag ?(Y/N)"
while($sourcetag_response -eq "N")
{
	$source_tag = Read-Host "Please type the source tag that you want to do RC."
	#預設跟Dev_build一樣
	if (-not $source_tag) {
		$source_tag=$default_dev_prefix+$username
	}
	$sourcetag_response = Read-Host "Do you want to do the RC tag with $source_tag ?(Y/N)"
}


$fulltag=$registryname+".azurecr.io/"+$imagerepositoryname+":"+$targettag
$source_imagename = $imagerepositoryname+":"+$source_tag
$target_imagename = $imagerepositoryname+":"+$target_tag
$source_fulltag = $registryname+".azurecr.io/"+$imagerepositoryname+":"+$source_tag
$target_fulltag = $registryname+".azurecr.io/"+$imagerepositoryname+":"+$target_tag

$final_response = Read-Host "Final confirmation: Do you want to assign the RC tag name "$target_imagename" to the existing tag with "$source_imagename"?(Y/N)"
if($final_response -eq "N")
{
	write-host "Please restart the process."
}
else
{
	#取得目前RC的message
	$rc_message = git tag -l --format='%(contents)' $target_tag
	if (!$rc_message)
	{
		$rc_message = ""
	}
	#暫存RC message
	$filePath = Join-Path -Path (Get-Location) -ChildPath "rc_message.txt"
	[System.IO.File]::WriteAllLines($filePath, $rc_message)
	#刪除遠端git的RC tag
	git push --delete origin $target_tag
	#刪除本地端git的RC tag
	git tag --delete $target_tag
	#標註目前commit為RC
	git tag -a $target_tag -F "rc_message.txt" --edit
	#更新遠端tag
	git push --follow-tags
	#刪除暫存檔
	Remove-Item "rc_message.txt"
	
	#解除image鎖定
	az acr repository update --name $registryname --image $target_imagename --delete-enabled true --write-enabled true
	podman pull $source_fulltag
	podman image tag $source_fulltag $target_fulltag
	podman push $target_fulltag

	#鎖定image
	az acr repository update --name $registryname --image $target_imagename --delete-enabled false --write-enabled false
}