.dots/scripts/update/conf.d/snappymail.sh

47 lines
2.1 KiB
Bash
Executable file

#!/usr/bin/env zsh
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
snappymail="/srv/snappymail"
user="nginx"
[[ ! -e "$snappymail" ]] && printf "> error: directory '"$snappymail"' does not exist\n" 1>&2 && exit 1
current_version="$(ls -ltr "$snappymail/snappymail/v" | tail -n 1 | awk '{print $NF}')"
[[ -z "$current_version" ]] && printf "> error: could not determine current version\n" 1>&2 && exit 1
latest_release="$(curl -s -L https://api.github.com/repos/the-djmaze/snappymail/releases/latest | grep tag_name | cut -d "\"" -f4 | cut -d "v" -f2)"
[[ -z "$latest_release" ]] && printf "> error: could not determine latest release\n" 1>&2 && exit 1
[[ "$current_version" == "$latest_release" ]] && printf "> current version '"$current_version"' is already up to date\n" && exit 0
printf "> updating snappymail...\n"
tmp="$(mktemp -d)"
printf "> downloading latest release '"$latest_release"' to temporary directory '"$tmp"'..."
curl -s -L -o "$tmp/snappymail-$latest_release.tar.gz" "https://github.com/the-djmaze/snappymail/releases/download/v$latest_release/snappymail-$latest_release.tar.gz" &> /dev/null
[[ "$?" != "0" ]] && printf " error!\n" 1>&2 && exit 1
printf " done!\n"
printf "> extracting downloaded archive..."
tar -xzf "$tmp/snappymail-$latest_release.tar.gz" -C "$snappymail" --overwrite &> /dev/null
[[ "$?" != "0" ]] && printf " error!\n" 1>&2 && exit 1
printf " done!\n"
printf "> getting custom themes..."
git clone "https://git.velvettear.de/velvettear/snappymail-themes.git" "/tmp/snappymail-themes"
themes="$(find "$snappymail/snappymail/v/$latest_release" -type d -iname "themes")"
find "/tmp/snappymail-themes" -mindepth 1 -maxdepth 1 ! -iname ".git" -type d -exec mv {} "$themes/" \;
rm -rf "/tmp/snappymail-themes"
printf "> setting correct permissions..."
find "$snappymail" -type d -exec chmod 755 {} \;
find "$snappymail" -type f -exec chmod 644 {} \;
chown -R "$user":"$user" "$snappymail"
printf " done!\n"
printf "> cleaning up..."
find "$snappymail/snappymail/v" -type d -maxdepth 1 ! -iname "v" ! -iname "$latest_release" -exec rm -rf {} \; &> /dev/null
rm -rf "$tmp"
printf " done!\n"