aboutsummaryrefslogtreecommitdiffstats
path: root/dfly-update
blob: 04cec61a8d698ed87c0308de357b79c266527d55 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/sh
#
# Copyright (c) Aaron LI <aly@aaronly.me>
# MIT License
#
# DragonFly BSD update tool
#

set -e

DEBUG=        # Disable debug
DEBUG=true    # Enable debug

NAME="dfly-update"
VERSION="0.1.0"
PROGRAM="${0##*/}"
TOOLDIR="${0%/*}"
PREFIX="${PREFIX:-${TOOLDIR}}"
CONFIGFILE="${PREFIX}/${NAME}.conf"

#
# Error Codes
#

EC_OS=5
EC_CONFIGFILE=10
EC_CONFIG=11


#
# Default Configurations
#

# Base URL to Remote DragonFly BSD images
URL_BASE="https://mirror-master.dragonflybsd.org"
URL_DEVELOPMENT="${URL_BASE}/snapshots/x86_64/images"
URL_RELEASE="${URL_BASE}/iso-images"

# Default to track the same branch as the installed system
UPDATE_BRANCH=


#
# Helper Functions
#

error() {
    echo "ERROR: $@" >&2
}

check_os() {
    local os
    os=$(uname -s)
    if [ "${os}" != "DragonFly" ]; then
        error "Unsupported operating system: ${os}"
        exit ${EC_OS}
    fi
}

# Load configurations from file
load_config() {
    local configfile tmpcfg
    configfile="$1"
    if [ ! -r "${configfile}" ]; then
        error "Cannot read configuration file: ${configfile}"
        exit ${EC_CONFIGFILE}
    fi
    # Filter out only valid configurations to be secure
    tmpcfg=$(mktemp)
    egrep '^[^ ]*=[^;&]*' ${configfile} > ${tmpcfg}
    . ${tmpcfg}
    rm ${tmpcfg}
}

# Get the branch of the installed system
# * DEVELOPMENT
# * RELEASE
get_local_branch() {
    check_os || exit $?
    uname -r | awk -F'-' '{ print $2 }'
}

# Get the version of local installed system
get_local_version() {
    local version
    check_os
    version=$(uname -v | awk '{ print $2 }')
    echo "${version}" | awk -F'-' '{ print $1 }' | tr -d 'v'
}

# Get the latest remote system image
# Returns:
# "_filename=<latest.iso/img>; md5=<md5/of/latest.iso/img>"
get_latest_image() {
    local branch local url_checksum tmpchecksum
    local latest_filename latest_md5 line
    branch="$1"
    if [ "${branch}" = "DEVELOPMENT" ]; then
        url_checksum="${URL_DEVELOPMENT}/CHECKSUM.MD5"
    else
        url_checksum="${URL_RELEASE}/md5.txt"
    fi
    tmpchecksum=$(mktemp)
    echo "Fetch remote systems checksum: ${url_checksum}" >&2
    fetch -o ${tmpchecksum} "${url_checksum}"
    if [ "${branch}" = "DEVELOPMENT" ]; then
        line=$(tail -n 1 ${tmpchecksum})
    else
        line=$(grep -v 'gui' ${tmpchecksum} | tail -n 1)
    fi
    latest_filename=$(echo "${line}" | awk -F'[()]' '{ print $2 }')
    latest_md5=$(echo "${line}" | awk '{ print $5 }')
    rm ${tmpchecksum}
    echo "_filename='${latest_filename}'; _md5='${latest_md5}'"
}

# Extract the version from image filename
get_version_filename() {
    local branch filename version
    branch="$1"
    filename="$2"
    if [ "${branch}" = "DEVELOPMENT" ]; then
        version=$(echo "${filename}" | cut -d'-' -f5 | cut -d'.' -f1-5)
    else
        version=$(echo "${filename}" | cut -d'-' -f3 | cut -d'_' -f1)
        version=${version#v}
    fi
    echo ${version}
}

# Compare between two version strings
# Returns values:
# * 0 : The two versions are the same
# * 1 : The first version is older than the other one
# * 2 : The first version is newer than the other one
compare_version() {
    echo 1
}


#
# Sub-command functions
#

cmd_version() {
    cat <<_EOF_
dfly-update : DragonFly BSD update tool
Version: ${VERSION}
Author: Aaron LI <aly@aaronly.me>
https://github.com/liweitianux/dfly-update
_EOF_
}

cmd_usage() {
    cmd_version
    echo
    cat <<_EOF_
Usage:
    help
        Show this help.
    version
        Show version information of this tool.
    status
        Show local installed system version and remote available version.
_EOF_
}

cmd_status() {
    local branch version branch_remote version_remote has_update
    branch=$(get_local_branch)
    version=$(get_local_version)
    if [ -z "${UPDATE_BRANCH}" ]; then
         branch_remote=${branch}
    else
        branch_remote=${UPDATE_BRANCH}
    fi
    eval $(get_latest_image ${branch_remote}) || exit $?
    version_remote=$(get_version_filename ${branch_remote} ${_filename})
    cat <<_EOF_
Local installed system:
    branch: ${branch}
    version: ${version}

Remote available system:
    branch: ${branch_remote}
    version: ${version_remote}

_EOF_

    has_update=$(compare_version ${version} ${version_remote})
    if [ ${has_update} -eq 0 ]; then
        echo "^_^ Your DragonFly is update-to-update ^_^"
    elif [ ${has_update} -eq 1 ]; then
        echo "!!! Your DragonFly needs update !!!"
    else
        echo "??? Your DragonFly is newer than remote ???"
    fi
}

#
# Main
#

# Load configurations
[ -r "${CONFIGFILE}" ] && load_config ${CONFIGFILE}

COMMAND="$1"
case "${COMMAND}" in
    help|--help|-h)
        shift
        cmd_usage
        ;;
    version|--version|-V)
        shift
        cmd_version
        ;;
    status)
        shift
        cmd_status "$@"
        ;;
    *)
        cmd_extension_or_status "$@"
        ;;
esac

exit 0