#!/usr/bin/env bash
set -euo pipefail
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[ "$(id -u)" = 0 ] || { echo '请使用root安装'; exit 1; }
KIT=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
cd "$KIT"
sha256sum -c PAYLOAD.sha256
command -v logrotate >/dev/null || { echo '请先执行：dnf install -y logrotate'; exit 1; }
/usr/bin/python3 -c 'import sys; assert sys.version_info >= (3,7), "需要Python3.7以上"'
TARGET=/usr/local/lib/foodsafe-maintenance
[ ! -e "$TARGET" ] && [ ! -L "$TARGET" ] || { echo '安装目录已存在，停止以免覆盖，请先核对已有版本'; exit 1; }
# Preview validates all four roots and logrotate syntax before installing anything.
[ ! -L "$KIT/foodsafe-logrotate.conf" ] || { echo '配置文件不能为符号链接'; exit 1; }
chown root:root "$KIT/foodsafe-logrotate.conf"
chmod 644 "$KIT/foodsafe-logrotate.conf"
/usr/bin/python3 "$KIT/cleanup.py"
install -d -o root -g root -m 755 "$TARGET"
install -o root -g root -m 644 cleanup.py foodsafe-logrotate.conf "$TARGET/"
if [ ! -e /var/log/foodsafe-maintenance-last.log ]; then
  install -o root -g root -m 600 /dev/null /var/log/foodsafe-maintenance-last.log
fi
printf '%s\n' '安装完成；没有删除文件，没有启用定时任务。' '下一步按INSTALL.md预览并向root crontab追加一条任务。'
