Files
nixos-config/home-manager/modules/zsh/move_avatar.sh
T
2025-11-27 18:09:51 +03:00

64 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
PICS="$HOME/Pictures"
AVATAR="$PICS/avatar.png"
BCKP="$PICS/.bckp"
NO_BCKP=0
DEL_SRC=0
while getopts ":nr" opt; do
case "$opt" in
n) NO_BCKP=1 ;;
r) DEL_SRC=1 ;;
*) echo "Usage: $0 [-n] [-r] <file.png>" >&2; exit 2 ;;
esac
done
shift $((OPTIND-1))
src=${1:-}
if [ -z "$src" ]; then
echo "Usage: $0 [-n] [-r] <file.png>" >&2
exit 2
fi
if [ ! -f "$src" ]; then
echo "Error: '$src' not found" >&2
exit 3
fi
orig_src="$src"
if command -v magick >/dev/null 2>&1; then
IM_CMD="magick"
else
echo "Error: ImageMagick 'convert' not found" >&2
exit 5
fi
tmp=$(mktemp --suffix=.png) || { echo "Error: mktemp failed" >&2; exit 6; }
if ! $IM_CMD -- "$src" "$tmp" >/dev/null 2>&1; then
echo "Error: ImageMagick conversion failed" >&2
rm -f -- "$tmp"
exit 7
fi
src="$tmp"
case "${src,,}" in
*.png) ;;
*) echo "Error: only .png files are accepted" >&2; exit 4 ;;
esac
if [ "$NO_BCKP" -eq 0 ] && [ -f "$AVATAR" ]; then
mkdir -p "$BCKP"
ts=$(date +%Y%m%d_%H%M%S)
mv -f -- "$AVATAR" "$BCKP/avatar${ts}.png"
fi
cp -f -- "$src" "$AVATAR"
if [ "$DEL_SRC" -eq 1 ]; then
rm -f -- "$orig_src"
fi