#!/bin/bash

###################################################
# Based on Ubuntu 22.04 LTS 2022-08-14 13:16:00 UTC+09:00:00
# => KST
# By Cellularhacker
#
# # No.01 : logrotate (Main file)
T_LOGROTATE='/etc/logrotate.conf'
T_LOGROTATE_SUB_D='/etc/logrotate.d'
#
# MARK: Appending compressing with xz option setup.
echo '' >> "${T_LOGROTATE}" && \
echo '' >> "${T_LOGROTATE}" && \
echo '# MARK: Compressing with XZ' >> "${T_LOGROTATE}" && \
echo 'compresscmd /usr/bin/xz' >> "${T_LOGROTATE}" && \
echo 'uncompresscmd /usr/bin/unxz' >> "${T_LOGROTATE}" && \
echo 'compressext .xz' >> "${T_LOGROTATE}" && \
echo '' >> "${T_LOGROTATE}" && \
echo '# MARK: Set Default max log file size to 2G' >> "${T_LOGROTATE}" && \
echo 'maxsize 2G' >> "${T_LOGROTATE}" && \
echo '' >> "${T_LOGROTATE}"
#
# MARK: Change remaining counts of weeks from 4 to 24 (About 8 months)
sed -i 's|rotate 4|rotate 24|g' "${T_LOGROTATE}"
#
# MARK: Use Date extention instead of .1, .2, .3, ...
sed -i 's|#dateext|dateext|g' "${T_LOGROTATE}"
#
# MARK: Compress for all log files
sed -i 's|#compress|compress|g' "${T_LOGROTATE}"
#
# ---------------------------------------------------
# # No.01-001 : logrotate - rsyslog
# MARK: Change remaining counts of weeks from 4 to 52 (About 12 months)
T_LR_RSYSLOG='rsyslog'
sed -i 's|rotate 4|rotate 52|g' "${T_LOGROTATE_SUB_D}/${T_LR_RSYSLOG}"
#
# ---------------------------------------------------
# # No.01-002 : logrotate - nginx
# MARK: Change remaining counts of weeks from 4 to 52 (About 12 months)
T_LR_NGINX='nginx'
sed -i 's|rotate 4|rotate 52|g' "${T_LOGROTATE_SUB_D}/${T_LR_NGINX}"
#
# ---------------------------------------------------
# # No.01-003 : logrotate - bootlog
# MARK: Change remaining counts of daily from 7 to 365 (About 1 year)
T_LR_BOOTLOG='bootlog'
sed -i 's|rotate 7|rotate 365|g' "${T_LOGROTATE_SUB_D}/${T_LR_BOOTLOG}"
#
# ---------------------------------------------------
# # No.02-001 : sysctl.conf - vm.max_map_count
# MARK: Set 'vm.max_map_count' to '262,244' at maximum
T_SYSCTL_OPTIMIZE_FILE_NAME='10-sysctl-optimize.conf'
cd "${HOME}" && \
wget "https://kr.mirrors.naho.moe/naho/tools/sysctl/${T_SYSCTL_OPTIMIZE_FILE_NAME}" && \
mv "${T_SYSCTL_OPTIMIZE_FILE_NAME}" /etc/sysctl.d/
#
# ---------------------------------------------------
# # No.02-002 : security/limits.conf - nofile,nproc,memlock
# MARK: Set 'nofile','nproc' to '300,000' at maximum.
# MARK: Set 'memlock' to 'unlimited'.
T_SECURITY_LIMITS_OPTIMIZE_FILE_NAME='10-limits-optimize.conf'
cd "${HOME}" && \
wget "https://kr.mirrors.naho.moe/naho/tools/security/${T_SECURITY_LIMITS_OPTIMIZE_FILE_NAME}" && \
mv "${T_SECURITY_LIMITS_OPTIMIZE_FILE_NAME}" /etc/security/limits.d/


# MARK: Run logrotate twice(→2 times)
echo '' && \
echo '[INIT_SETUP/sysctl/Apply] (1/2) Applying sysctl.conf changes ...' && \
echo '[INIT_SETUP/security/limits/Apply] (2/2) Applying security/limits.conf changes ...' && \
service procps force-reload && \
deb-systemd-invoke restart procps.service && \
sysctl -p && \
echo '[INIT_SETUP/security/limits/Apply] (1/2) Finished applying sysctl.conf' && \
echo '[INIT_SETUP/security/limits/Apply] (2/2) Finished applying security/limits.conf' && \
echo '' && \
echo '[INIT_SETUP/logrotate/Execute] Initializing compresstion for syslogs ...' && \
echo '' && \
echo "[INIT_SETUP/logrotate/Execute] (1/3) Started Running  ... $(date +20%y%m%d-%H%M%S:Z+%Z)" && \
systemctl restart logrotate && \
echo "[INIT_SETUP/logrotate/Execute] (1/3) Finished Running! $(date +20%y%m%d-%H%M%S:Z+%Z)" && \
echo "[INIT_SETUP/logrotate/Execute] (2/3) Started Running  ... $(date +20%y%m%d-%H%M%S:Z+%Z)" && \
systemctl restart logrotate && \
echo "[INIT_SETUP/logrotate/Execute] (2/3) Finished Running! $(date +20%y%m%d-%H%M%S:Z+%Z)" && \
echo "[INIT_SETUP/logrotate/Execute] (3/3) Started Running  ... $(date +20%y%m%d-%H%M%S:Z+%Z)" && \
systemctl restart logrotate && \
echo "[INIT_SETUP/logrotate/Execute] (3/3) Finished Running! $(date +20%y%m%d-%H%M%S:Z+%Z)"  && \
echo '' && \
echo '[INIT_SETUP/logrotate/Execute] Initializing compresstion has been successfully finished!' && \
echo '' && \
echo ''
