#!/usr/bin/env bash
# -------------------------------------------------------------------------- #
# Copyright 2002-2025, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
# -------------------------------------------------------------------------- #

: "${RESTIC_VERSION:=0.17.3}"

set -o errexit -o nounset -o pipefail

which curl bzip2 dirname realpath xargs 1>/dev/null

readonly SELF=$(realpath "$0" | xargs dirname) && cd "$SELF/"

readonly RESTIC_URL="https://github.com/restic/restic/releases/download/v${RESTIC_VERSION}/restic_${RESTIC_VERSION}_linux_amd64.bz2"
readonly RESTIC_URL_ARM64="https://github.com/restic/restic/releases/download/v${RESTIC_VERSION}/restic_${RESTIC_VERSION}_linux_arm64.bz2"

install -d "$SELF/bin/"
curl -fsSL "$RESTIC_URL" | bzip2  -d -s > "$SELF/bin/restic"
curl -fsSL "$RESTIC_URL_ARM64" | bzip2  -d -s > "$SELF/bin/restic.arm64"

chmod ugo+x "$SELF/bin/restic" "$SELF/bin/restic.arm64"
