aboutsummaryrefslogtreecommitdiff
path: root/bin/spexmgr
blob: d36ef0c8dbcc532af32fd13102854e78fb4f8c1e (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
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
227
228
#!/usr/bin/env bash
# vim: tabstop=4 shiftwidth=4 expandtab
topdir="$( dirname $(cd $(dirname ${BASH_SOURCE[0]}) && pwd) )"
cachedir="${topdir}/cache"
datadir="${topdir}/share"
spexdir="${topdir}/spex"

release_server="http://irtfweb.ifa.hawaii.edu/~spex/Spextool"
releases=(
    1.4 
    1.5
    2.0
    2.1
    2.2
    2.3
    3.1
    3.2
    3.3
    3.4
    4.0
    4.0.1
    4.0.2
    4.0.3
    4.1
)
dependencies=(
    https://github.com/wlandsman/IDLAstro/archive/refs/heads/master.tar.gz 1 IDLAstro
    https://idlastro.gsfc.nasa.gov/ftp/coyote_astron.tar.gz 1 coyote
    https://pages.physics.wisc.edu/~craigm/idl/down/cmtotal.tar.gz 0 cmtotal
)

archive_suffix="tar.gz"
archive_name="Spextool"
archive_version=

pushd() {
    command pushd "$1" &>/dev/null
}

popd() {
    command popd &>/dev/null
}

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

msg_warn() {
    echo "WARN: $@" >&2
}

msg_info() {
    echo "INFO: $@" >&2
}

idl_exists() {
    ([ type -a idl ] || [ type -p idl ]) && [ -n "$IDL_DIR" ]
}

initialize() {
    mkdir -p "$spexdir" \
        "$cachedir" \
        "$datadir"

    if ! idl_exists; then
        msg_warn IDL is not initialized, or not installed.
    fi
}

release_list() {
    local fmt="%-10s %-10s\n"
    local installed=()
    echo "==========="
    echo " Available"
    echo "==========="
    for x in "${releases[@]}"; do
        mark=""
        if release_exists $x; then
            installed+=($x)
            continue
        fi
        echo "  $x"
    done

    if (( ${#installed[@]} )); then
        echo
        echo "==========="
        echo " Installed"
        echo "==========="
        for x in "${installed[@]}"; do
            echo "  $x"
        done
    fi
}

release_exists() {
    local version="$1"
    test -d "$spexdir/$version"
}

release_download() {
    local version="$1"
    local package="${archive_name}_v${version}.${archive_suffix}"

    pushd "$cachedir"
        if [ -f "$package" ]; then
            msg_info Using cached package: $package
        else
            msg_info Downloading: $package
            if ! curl -L -O "$release_server/$package"; then
                msg_err Unable to download $package
                return 1
            fi
        fi
    popd
    echo "$cachedir/$package $version"
}

dep_install() {
    pushd "$cachedir"
        for ((i=0; i < ${#dependencies[@]}; i+=3)); do 
            local dep="${dependencies[i]}"
            local archive="$(basename $dep)"
            if [ -f "$archive" ]; then
                continue
            fi
            msg_info Downloading: $dep
            if ! curl -L -O "$dep"; then
                msg_error Unable to download $dep
                return 1
            fi
        done

        for ((i=0; i < ${#dependencies[@]}; i+=3)); do 
            local archive="$(basename ${dependencies[i]})"
            local strip_level="${dependencies[i+1]}"
            local dname="${dependencies[i+2]}"
            local dest="${datadir}/${dname}"
            if [ -d "$dest" ]; then
                msg_info $dname is already installed
                continue
            fi
            mkdir -p "$dest"
            msg_info Installing dependency: $dname
            tar -xf "$archive" --strip-components=$strip_level -C "$dest"
        done
    popd
}

release_install() {
    local package="$1"
    local version="$2"
    pushd "${spexdir}"
        mkdir -p ${version}
        tar -xf "$package" --strip-components=1 -C ${version}
    popd
}


usage() {
    echo "usage: $(basename $0) [-h] {command} [arg ...]"
    echo
    echo "Arguments:"
    echo "  --help      this message"
    echo
    echo "Commands:"
    echo "  clean               Removed cached packages/archives"
    echo "  list                List available spextool releases"
    echo "  install {release}   Install a spextool release"
    echo
}

version=4.1

# main()
initialize

if (( $# < 1 )); then
    msg_error Not enough arguments
    usage
    exit 1
fi

while [[ $# != 0 ]]; do
    key="$1"
    case "$key" in
        clean)
            do_clean=1
            ;;
        list)
            do_list=1
            ;;
        install)
            do_install=1
            version=$2
            if [ -z "$version" ]; then
                msg_error install requires a release version
                release_list
                exit 1
            fi
            shift
            ;;
        *)
            ;;
    esac
    shift
done

if (( do_clean )); then
    msg_info Cleaning: $cachedir
    if [ -z "$cachedir" ] || [ "$cachedir" = "/" ]; then
        msg_error "Refusing to clean unsafe path: '$cachedir'"
        exit 1
    fi
    rm -rf "$cachedir"/*
elif (( do_list )); then
    release_list
elif (( do_install )); then
    if release_exists $version; then
        msg_info $archive_name v$version is already installed
    else
        read package package_version <<< $(release_download $version)
        release_install $package $package_version
    fi
    dep_install
fi