jcb-power-updater/src/jcb-power-updater.sh

226 lines
6.2 KiB
Bash

#!/bin/bash
# The most recent program version.
_VERSION="1.0.3"
_V="1.0"
# The program full name
PROGRAM_NAME="JCB Power Updater"
# make sure git is installed
command -v git >/dev/null 2>&1 || {
echo >&2 "ERROR: ${PROGRAM_NAME} v${_VERSION} script requires git."
exit 1
}
# make sure rsync is installed
command -v rsync >/dev/null 2>&1 || {
echo >&2 "ERROR: ${PROGRAM_NAME} v${_VERSION} script requires rsync."
exit 1
}
current_date=$(date +"%Y-%m-%d %T")
gitea_url="https://git.vdm.dev"
quiet=false
install() {
# Check if the script is already installed
if [ -f /etc/systemd/system/jcb-power-updater.service ]; then
echo "The script is already installed. Updating..."
update
return
fi
# Download and install jcb-power-updater script
sudo curl -L "https://git.vdm.dev/api/v1/repos/octoleo/jcb-power-updater/raw/src/jcb-power-updater.sh" -o /usr/local/bin/jcb-power-updater
sudo chmod +x /usr/local/bin/jcb-power-updater
# Ask for the target directory
echo "Enter the path to the directory containing the repositories:"
read target_dir
# Create log directory and log file
log_dir="/var/log/jcb-power-updater"
log_file="jcb-power-updater.log"
sudo mkdir -p "$log_dir"
sudo touch "$log_dir/$log_file"
# Create the jcb-power-updater.service file
sudo bash -c "cat > /etc/systemd/system/jcb-power-updater.service" << EOL
[Unit]
Description=Update JCB Power Repositories
[Service]
ExecStart=/usr/local/bin/jcb-power-updater -d "$target_dir" -q
StandardOutput=file:$log_dir/$log_file
StandardError=inherit
EOL
# Create the jcb-power-updater.timer file
sudo bash -c "cat > /etc/systemd/system/jcb-power-updater.timer" << EOL
[Unit]
Description=Run jcb-power-updater.service every hour
[Timer]
OnBootSec=12min
OnUnitActiveSec=1h
Unit=jcb-power-updater.service
[Install]
WantedBy=timers.target
EOL
# Enable and start the timer
sudo systemctl daemon-reload
sudo systemctl enable --now jcb-power-updater.timer
echo "Installation complete."
}
uninstall() {
# Stop and disable the timer
sudo systemctl disable --now jcb-power-updater.timer
# Remove systemd service and timer files
sudo rm /etc/systemd/system/jcb-power-updater.service
sudo rm /etc/systemd/system/jcb-power-updater.timer
# Remove jcb-power-updater script
sudo rm /usr/local/bin/jcb-power-updater
# Remove log directory and log file
sudo rm -r /var/log/jcb-power-updater
# Reload systemd daemon
sudo systemctl daemon-reload
echo "Uninstallation complete."
}
update() {
# Update the jcb-power-updater script
sudo curl -L "https://git.vdm.dev/api/v1/repos/octoleo/jcb-power-updater/raw/src/jcb-power-updater.sh" -o /usr/local/bin/jcb-power-updater
sudo chmod +x /usr/local/bin/jcb-power-updater
echo "Update complete."
}
# Parse command line options
while [[ "$#" -gt 0 ]]; do
case $1 in
--install)
install
exit 0
;;
--uninstall)
uninstall
exit 0
;;
--update)
update
exit 0
;;
--gitea)
gitea_url="$2"
shift 2
;;
-d|--dir)
target_dir="$2"
shift 2
;;
-q)
quiet=true
shift
;;
*)
echo "Unknown parameter passed: $1"
exit 1
;;
esac
done
# Verify directory is provided
if [ -z "$target_dir" ]; then
echo "Error: Directory not provided. Use -d or --dir flag to specify the directory."
exit 1
fi
# Change to the directory containing the repositories
cd "$target_dir"
# Loop through all directories
for owner_dir in */; do
cd "$owner_dir"
# Loop through all repositories inside the owner directory
for repo_dir in */; do
cd "$repo_dir"
# Check if the current directory is a Git repository
if [ -d ".git" ]; then
# Check for local changes
git_status=$(git status -s)
# If there are changes, stage, commit, and push
if [[ ! -z "$git_status" ]]; then
git add .
git commit -am "update $current_date"
git push
if [ "$quiet" = false ]; then
echo "[${owner_dir}/${repo_dir}]: Changes have been committed and pushed."
fi
fi
else
# Check if the remote repository exists
if git ls-remote "${gitea_url}/${owner_dir}/${repo_dir}.git" > /dev/null 2>&1; then
# The remote repository exists
cd ..
mv "${repo_dir}" "${repo_dir}_tmp"
git clone "${gitea_url}/${owner_dir}/${repo_dir}.git"
# Move the stashed files back with force, overwriting any files that exist in the ${repo_dir} folder
rsync -av --force "${repo_dir}_tmp/" "${repo_dir}/"
rm -rf "${repo_dir}_tmp"
# Make a full commit
cd "$repo_dir"
git add .
git commit -am "update $current_date"
git push
if [ "$quiet" = false ]; then
echo "[${owner_dir}/${repo_dir}]: A existing Git repository has been merged. Changes have been committed and pushed."
fi
else
# Remove protocol (http:// or https://) from gitea_url
url_no_protocol="${gitea_url#*://}"
# Extract the domain from the URL
domain="${url_no_protocol%%/*}"
# Construct the SSH URL
ssh_url="ssh://git@${domain}/${owner_dir}/${repo_dir}.git"
# The remote repository does not exist, set the remote and create the repository
git init
git remote add origin "${ssh_url}"
git add .
git commit -am "first commit"
git push -u origin master
if [ "$quiet" = false ]; then
echo "[${owner_dir}/${repo_dir}]: A new Git repository has been initialized."
fi
fi
fi
# Return to the owner directory
cd ..
done
# Return to the main directory
cd ..
done