#!/bin/bash
# 폰트 파일이 없을 경우 globbing이 빈 문자열을 반환하도록 설정
shopt -s nullglob
# 처리할 폰트 파일 확장자 목록
font_extensions=("*.ttf" "*.otf" "*.ttc" "*.woff" "*.woff2")
for ext_pattern in "${font_extensions[@]}"; do
for original_filepath in $ext_pattern; do
# 파일이 실제로 존재하는지 한 번 더 확인 (심볼릭 링크 등은 제외하고 일반 파일만)
if [ ! -f "$original_filepath" ]; then
continue
fi
echo "-------------------------------------"
echo "[*] Processing file: '$original_filepath'"
# fc-scan으로 fullname 정보 추출
# 오류 출력은 /dev/null로 보내고, 결과가 없으면 빈 문자열로 처리
fullname_string=$(fc-scan -f "%{fullname}" "$original_filepath" 2>/dev/null)
if [ -z "$fullname_string" ]; then
echo " [!] Could not get fullname for '$original_filepath'. Skipping."
continue
fi
echo " [*] Fullname string from fc-scan: '$fullname_string'"
# 새 파일명의 기본 부분 결정 (두 번째 이름 우선, 없으면 첫 번째 이름)
# awk: 콤마를 기준으로 필드 분리.
# NF (Number of Fields)가 2 이상이고 두 번째 필드($2)가 비어있지 않으면 $2 사용.
# 그렇지 않으면 $1 사용. 각 필드의 앞뒤 공백은 제거.
new_name_base=$(echo "$fullname_string" | awk -F, '
{
name_to_use="";
if (NF >= 2 && $2 != "") {
# 두 번째 필드의 앞뒤 공백 제거
gsub(/^[[:space:]]+|[[:space:]]+$/, "", $2);
if ($2 != "") { # 공백만으로 이루어진 경우도 방지
name_to_use = $2;
}
}
# name_to_use가 여전히 비어있다면 (두 번째 필드가 없거나 비었으면) 첫 번째 필드 사용
if (name_to_use == "") {
# 첫 번째 필드의 앞뒤 공백 제거
gsub(/^[[:space:]]+|[[:space:]]+$/, "", $1);
name_to_use = $1;
}
print name_to_use;
}')
if [ -z "$new_name_base" ]; then
echo " [!] Could not determine a base name for '$original_filepath' from '$fullname_string'. Skipping."
continue
fi
echo " [*] Selected base name: '$new_name_base'"
# 파일명으로 사용하기 안전하게 이름 정제
# 예시: 공백은 밑줄(_)로 변경, 그 외 일반적인 파일명에 부적합한 문자 제거 (알파벳, 숫자, 밑줄, 하이픈, 점만 허용)
# 이 정제 규칙은 필요에 따라 더 정교하게 수정해야 합니다.
new_name_sanitized=$(echo "$new_name_base" | sed -e 's/[[:space:]]\+/_/g' -e 's/[^a-zA-Z0-9_.-]//g')
# 정제된 이름이 너무 짧거나 비어버린 경우 처리
if [ -z "$new_name_sanitized" ] || [ "${#new_name_sanitized}" -lt 1 ]; then
echo " [!] Sanitized name is empty or too short for '$original_filepath'. Original base: '$new_name_base'. Skipping."
continue
fi
echo " [*] Sanitized name: '$new_name_sanitized'"
# 원본 파일의 디렉토리 경로와 확장자 추출
dir_path=$(dirname "$original_filepath")
extension="${original_filepath##*.}"
# 확장자가 없는 경우 (예상치 못한 경우) 처리
if [ -z "$extension" ] || [ "$extension" == "$original_filepath" ]; then
echo " [!] Could not determine extension for '$original_filepath'. Skipping."
continue
fi
# 최종 새 파일 경로 생성
final_new_filepath="${dir_path}/${new_name_sanitized}.${extension}"
echo " [*] Proposed new filename: '$final_new_filepath'"
# 원본 파일과 새 파일명이 다르고, 새 파일명이 실제로 내용이 있을 경우에만 이름 변경
if [ "$original_filepath" != "$final_new_filepath" ]; then
# 대상 파일이 이미 존재하는지 확인 (덮어쓰기 방지)
if [ -e "$final_new_filepath" ]; then
echo " [!] Target filename '$final_new_filepath' already exists. Skipping rename of '$original_filepath'."
else
echo " [+] Renaming '$original_filepath' to '$final_new_filepath'"
# !!! 실제 파일명 변경 실행 라인 !!!
# !!! 아래 mv 명령어의 주석을 해제하기 전에 반드시 테스트를 충분히 하십시오. !!!
# mv "$original_filepath" "$final_new_filepath"
fi
else
echo " [*] Filename '$original_filepath' is already same as proposed or no change needed. Skipping."
fi
done
done
echo "-------------------------------------"
echo "Font renaming process finished."
echo "Review the output above. If everything looks correct, uncomment the 'mv' command in the script and run again."