#!/bin/sh
set -e

MSG_FILE=$1
MSG_SOURCE=$2

case "$MSG_SOURCE" in
  "") ;;
  *) exit 0 ;;
esac

if [ ! -f "$MSG_FILE" ]; then
  exit 0
fi

has_content=0
while IFS= read -r line || [ -n "$line" ]; do
  case "$line" in
    ''|\#*) ;;
    *)
      has_content=1
      break
      ;;
  esac
done < "$MSG_FILE"

if [ "$has_content" -eq 1 ]; then
  exit 0
fi

tmp_file=${MSG_FILE}.nex-template.$$
{
  cat <<'EOF'
# <类型>: <简短中文描述>
#
# <详细说明>
#
# 类型: feat / fix / refactor / docs / style / test / chore
# 示例: feat: 添加供应商批量管理功能
EOF
  if [ -s "$MSG_FILE" ]; then
    printf '\n'
    while IFS= read -r line || [ -n "$line" ]; do
      printf '%s\n' "$line"
    done < "$MSG_FILE"
  fi
} > "$tmp_file"

mv "$tmp_file" "$MSG_FILE"
