#!/bin/bash NC='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' msg_info() { echo -e "${GREEN}$1${NC}" } msg_warn() { echo -e "${YELLOW}$1${NC}" } msg_error() { echo -e "${RED}$1${NC}" } print_help() { echo echo "trans - a wrapper for transmission-daemon and transmission-remote" echo echo "USAGE:" echo " trans COMMAND [...]" echo echo "COMMANDs are:" echo " ls | list:" echo " list current torrents" echo " start:" echo " start Transmission daemon (if not yet started). The script" echo " won't start the daemon is there is no connection to a VPN" echo " (in this case Mullvad). Use '-f|--force' to start anyway." echo " stop:" echo " stop Transmission daemon" echo " add:" echo " add a sequence of torrent files or a single magnet link" echo " (possibly a bug with transmission-remote). If no torrent is" echo " specified the current selection in the clipboard is used as a" echo " torrent/magnet link" echo " rm | remove:" echo " remove a list of torrents. The syntax is the same as the one for" echo " the '--torrent' option in the 'transmission-remote' command. A" echo " special token 'complete' can be provided to remove all completed" echo " torrents. If no torrent is specified no action is performed." echo " del | delete:" echo " remove a list of torrents and deletes the corresponding files." echo " The syntax is the same as the one for the '--torrent' option in" echo " the 'transmission-remote' command. If no torrent is specified no" echo " action is performed." echo " pause:" echo " pause a list of torrents. The syntax is the same as the one for" echo " the '--torrent' option in the 'transmission-remote' command. If" echo " no list is provided the token 'all' is assumed" echo " unpause:" echo " unpause a list of torrents. The syntax is the same as the one for" echo " the '--torrent' option in the 'transmission-remote' command. If no" echo " list is provided the token 'all' is assumed" echo " info:" echo " print detailed information on a list of torrents. The syntax is" echo " the same as the one for the '--torrent' option in the" echo " 'transmission-remote' command. If no list is provided the token" echo " 'all' is assumed" echo " help:" echo " print this help" echo } transmission_start() { pgrep transmission-da >/dev/null && error "Transmission daemon is already running!" || transmission-daemon "$@" } COMMAND=${1:-list} shift case $COMMAND in ls|list) transmission-remote --list ;; start) # Checking VPN connection echo "Checking Mullvad status..." mullvad status &>/dev/null vpn=$? # Collect additional flags force=0 case $1 in -f|--force) # Force run even if not connected to a VPN force=1 ;; esac echo $vpn echo $force if [[ $vpn -gt 0 ]]; then warning="You are not connected to a VPN..." msg_warn "$warning" if [[ $force -gt 0 ]]; then msg_warn "...starting Transmission daemon anyway!" transmission_start else notify-send --urgency=critical "Transmission" "$warning" msg_warn "Use -f|--force to start the Transmission daemon anyway." exit 2 fi else msg_info "Starting Transmission daemon..." transmission_start fi ;; stop) msg_info "Stopping Transmission daemon..." transmission-remote --exit exit $? ;; add) [[ $# -ge 1 ]] && transmission-remote --add "$@" || transmission-remote --add $(xclip -o -selection clipboard) ;; rm|remove) if [[ $# -ge 1 ]]; then [[ $1 == "complete" ]] && t=$(transmission-remote --list | grep 100% | grep Done | awk '{ print $1 }' | paste -sd, -) || t=$@ [[ -z $t ]] && msg_warn "No completed torrent to remove." && exit 0 transmission-remote -t"$t" --remove fi ;; del|delete) [[ $# -ge 1 ]] && transmission-remote -t"$@" --remove-and-delete ;; pause) transmission-remote -t${@:-all} --stop ;; unpause) transmission-remote -t${@:-all} --start ;; info) transmission-remote -t${@:-all} --info ;; help) print_help ;; *) msg_error "$COMMAND: invalid command" print_help exit 1 ;; esac exit 0