| 1657 | /* ── Skill management ─────────────────────────────────────────── */ |
| 1658 | |
| 1659 | int cbm_install_skills(const char *skills_dir, bool force, bool dry_run) { |
| 1660 | if (!skills_dir) { |
| 1661 | return 0; |
| 1662 | } |
| 1663 | int count = 0; |
| 1664 | |
| 1665 | /* Clean up only empty old directories. Historical files may have been |
| 1666 | * customized, and their old content is not an ownership proof. */ |
| 1667 | for (int i = 0; i < OLD_SKILL_COUNT; i++) { |
| 1668 | char old_path[CLI_BUF_1K]; |
| 1669 | snprintf(old_path, sizeof(old_path), "%s/%s", skills_dir, old_skill_names[i]); |
| 1670 | (void)cbm_remove_empty_directory(old_path, dry_run); |
| 1671 | } |
| 1672 | |
| 1673 | for (int i = 0; i < CBM_SKILL_COUNT; i++) { |
| 1674 | char skill_path[CLI_BUF_1K]; |
| 1675 | snprintf(skill_path, sizeof(skill_path), "%s/%s", skills_dir, skills[i].name); |
| 1676 | char file_path[CLI_BUF_1K]; |
| 1677 | snprintf(file_path, sizeof(file_path), "%s/SKILL.md", skill_path); |
| 1678 | |
| 1679 | struct stat skill_state; |
| 1680 | #ifndef _WIN32 |
| 1681 | if (lstat(skill_path, &skill_state) == 0 && !S_ISDIR(skill_state.st_mode)) { |
| 1682 | continue; |
| 1683 | } |
| 1684 | #else |
| 1685 | if (stat(skill_path, &skill_state) == 0 && !S_ISDIR(skill_state.st_mode)) { |
| 1686 | continue; |
| 1687 | } |
| 1688 | #endif |
| 1689 | |
| 1690 | /* Check if already exists */ |
| 1691 | if (!force) { |
| 1692 | struct stat st; |
| 1693 | if (stat(file_path, &st) == 0) { |
| 1694 | continue; |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | if (dry_run) { |
| 1699 | count++; |
| 1700 | continue; |
| 1701 | } |
| 1702 | |
| 1703 | if (mkdirp(skill_path, DIR_PERMS) != 0) { |
| 1704 | continue; |
| 1705 | } |
| 1706 | |
| 1707 | if (cbm_text_write_owned_document(file_path, skills[i].content) == 0) { |
| 1708 | count++; |
| 1709 | } |
| 1710 | } |
| 1711 | return count; |
| 1712 | } |
| 1713 | |
| 1714 | int cbm_remove_skills(const char *skills_dir, bool dry_run) { |
| 1715 | if (!skills_dir) { |
no test coverage detected