aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/edusync
blob: a0da2c982a394420b8ba04794886f3b7741456e7 (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
#!/usr/bin/env bash

year="$(date '+%Y')"
usage="Usage: $0 <command> [-r <remote>] [-y <year>]"

print_help() {
    cat << EOF
$usage

Description:
  This script synchronizes files using rsync

Options:
  -r <remote>    Specify the target machine (nest, lnest, laptop, roost).
  -y <year>      Specify the year for data synchronization.
  -h             Display this help message and exit.

commands:
  push           Push data to the specified target.
  pull           Pull data from the specified target.

Examples:
  $0 push -r roost -y 2023     # Push data to the 'roost' target for the year 2023.
  $0 pull -r nest              # Pull data from the 'nest' target for the current year ($year).
EOF
}

check_requirements(){
     if ! command -v rsync > /dev/null; then
        echo "Missing rsync, exiting"
        exit 1
    fi
}

find_local(){
    case "$(uname)" in
        "Linux")
            local_dir="$HOME/doc/edu"
            ;;
        "Darwin")
            local_dir="$HOME/Documents"
            ;;
        *)
            exit 1
    esac
    echo "$local_dir"
}

find_remote(){
    case "$1" in
        "nest"|"lnest")
            echo "$1:doc/edu"
            ;;
        "laptop"|"macbook")
            echo "laptop:Documents"
            ;;
        "roost")
            echo "eduroost:doc"
            ;;
        *)
            exit 1
    esac
}

push_data(){
    local lroot="$1"
    rsync -aiuvzCP --exclude-from="$lroot/.syncignore" "$lroot/$year" "$remote_root"
}

pull_data(){
    local lroot="$1"
    rsync -aiuvzCP "$remote_root/$year" "$lroot"
}

check_requirements

# set command to first argument
command="$1"
[ -z "$command" ] && {
    echo "missing command"
    echo "$usage"
    exit 1
}
shift

# parse options
while getopts ":r:y:h" opt; do
    case $opt in
        r) remote="$OPTARG";;
        y) year="$OPTARG";;
        h) print_help; exit 0 ;;
        \?)
            echo "Invalid options: -$OPTARG" >&2
            exit 1
            ;;
        :)
            echo "Option -$OPTARG requires an argument" >&2
            exit 1
            ;;
    esac
done


# check for valid remote
[ -z "$remote" ] && {
    echo "$usage" >&2
    exit 1
}

# set remote root
remote_root="$(find_remote "$remote")"
[ -z "$remote_root" ] && {
    echo "Unrecognized remote: $remote"
    exit 1
}

local_root="$(find_local)"
[ -z "$local_root" ] && {
    echo "Machine not recognized, exitting"
    exit 1
}

# execute commands
case "$command" in 
    "push") push_data "$local_root";;
    "pull") pull_data "$local_root";;
    "help") print_help ;;
    *)
        echo "unrecognized command: $command"
        exit 1
esac