aboutsummaryrefslogtreecommitdiff
path: root/transmission/.local/bin/trans
blob: 24abae3251b60c97951ba7dd4ea7e6071380cadb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/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