# 設定 ASCII 藝術字的內容
$asciiArt = @"

  _____  ______ _      ______           _____ ______ 
 |  __ \|  ____| |    |  ____|   /\    / ____|  ____|
 | |__) | |__  | |    | |__     /  \  | (___ | |__   
 |  _  /|  __| | |    |  __|   / /\ \  \___ \|  __|  
 | | \ \| |____| |____| |____ / ____ \ ____) | |____ 
 |_|  \_\______|______|______/_/    \_\_____/|______|
                                                     
                                                     
                                                                     
"@

# 顯示 ASCII 藝術字
Write-Host $asciiArt
#第一次建立專案請先設定ACR Name
$registryname="evdevcontainerregistry"
$fullregistryname="evdevcontainerregistry.azurecr.io"
#第一次建立專案請先設定專案名稱
$imagerepositoryname="server"
#來源TAG
$source_tag="Docker_RC"
$targetTagPrefix="Docker_v"

#檢查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

$sourcetag_response = Read-Host "Do you want to do release tag with $source_tag ?(Y/N)"
while($sourcetag_response -ne "Y")
{
	$source_tag = Read-Host "Please type the source tag that you want to do newtag."
	$sourcetag_response = Read-Host "Do you want to do release tag with $source_tag ?(Y/N)"
}
	
$target_tag = Read-Host "Please type the release tag version (make sure that the same as tagname from azure devops)."
$target_tag = $targetTagPrefix + $target_tag
$targettag_response = Read-Host "Please confirm the release tag $target_tag (Y/N)."

while($targettag_response -ne "Y")
{
	$target_tag = Read-Host "Please type the release tag version (make sure that the same as tagname from azure devops)."
	$target_tag = $targetTagPrefix + $target_tag
	$targettag_response = Read-Host "Please confirm the release tag $target_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 new tag name "$target_imagename" to the existing tag with "$source_imagename"?(Y/N)"
if($final_response -ne "Y")
{
	write-host "Please restart the process."
}
else
{
	#取得目前RC的message
	$rc_message = git tag -l --format='%(contents)' $source_tag
	if ($status -match "unknown revision or path not in the working tree")
	{
		Write-Host "source tag missing"
		exit
	}
	#暫存RC message
	$filePath = Join-Path -Path (Get-Location) -ChildPath "rc_message.txt"
	[System.IO.File]::WriteAllLines($filePath, $rc_message)
	#標註RC為新的tag
	git tag -a $target_tag -F "rc_message.txt" $source_tag
	#更新遠端tag
	git push --follow-tags
	#刪除暫存檔
	Remove-Item "rc_message.txt"
	
	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
}