#!/usr/bin/env bash
# .drone.version.sh

VERSION_FILE="version.txt"

# 若文件不存在则初始化
if [[ ! -f "$VERSION_FILE" ]]; then
    echo "0.0.1" > "$VERSION_FILE"
    echo "Initialized version: 0.0.1"
    exit 0
fi

# 读取当前版本并拆分为数组
IFS='.' read -r -a parts < "$VERSION_FILE"

major=${parts[0]}
minor=${parts[1]}
patch=${parts[2]}

# 递增 patch，处理进位 (100进位)
patch=$((patch + 1))
if [[ $patch -ge 100 ]]; then
    patch=0
    minor=$((minor + 1))
fi
if [[ $minor -ge 100 ]]; then
    minor=0
    major=$((major + 1))
fi

new_version="${major}.${minor}.${patch}"
echo "$new_version" > "$VERSION_FILE"
echo "Updated version: $new_version"

