#!/bin/bash

# 获取当前日期
date=$(date +%Y%m%d%H%M)
branch_name=$(git rev-parse --abbrev-ref HEAD)

# 升级 package.json 中的版本号
pnpm version patch --no-git-tag-version

# commit 改动，并且 push 到远程仓库的同名分支
git add .
git commit -m "hotfix: $date"
git push origin $branch_name

# 创建到 master 分支的 PR
master_pr_url=$(gh pr create --base master --head $branch_name --title "hotfix: [master] $date-$branch_name" --body "hotfix $date to master")

# 创建到 dev 分支的 PR
dev_pr_url=$(gh pr create --base dev --head $branch_name --title "hotfix: [dev] $date-$branch_name" --body "hotfix $date to dev")

# 组合 PR URL
pr_urls="Hotfix pull request are created, please take a look:\nTo master: $master_pr_url\nTo dev: $dev_pr_url"

# Copy PR URLs to clipboard (using xclip for Linux or pbcopy for macOS)
if command -v xclip &> /dev/null; then
    echo -e "$pr_urls" | xclip -selection clipboard
elif command -v pbcopy &> /dev/null; then
    echo -e "$pr_urls" | pbcopy
else
    echo "Unable to copy to clipboard. Please install xclip (Linux) or use macOS."
fi

echo "PR URLs have been copied to clipboard:"
echo -e "$pr_urls"
