Reply - Raw
#!/bin/bash
# -*- coding: utf8 -*-
#
# Copyright 2017 - Odd Stråbø <oddstr13@openshell.no>
# License: MIT - https://opensource.org/licenses/MIT
#
# Dependencies:
#  * wget
#  * 7z
#
# Description:
#  Downloads the latest version of specified Atmel device packs.

PACKDIR="${HOME}/tools/atmel/packs"
PACKS=(atmega attiny)

BASEURL="http://packs.download.atmel.com"

wget -Oindex.html $BASEURL

# Following two functions are from StackOverflow
function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
function containsElement () {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}


packs=$(grep '.atpack' index.html | sed -r 's|.*"([^\"]*\.atpack).*?|\1|p' | sort | uniq)

current=""
cv=0
cp=""
for pack in $packs; do
    echo -n "$pack "
    foo=($(echo $pack | sed -rn 's|^[^\.]+\.([^\._]+)(_DFP)?\.(.*)\.atpack|\L\1 \3|p'))
    name=${foo[0]}
    version=${foo[1]}
    echo "$name - $version "

    if [[ "$name" != "$current" ]]; then
        if [ ! -z "$name" ]; then
            if containsElement "$current" "${PACKS[@]}"; then
                if [ ! -f "${PACKDIR}/${current}/${cv}/package.content" ]; then
                    echo "DOWNLOADING $current $cv - $cp"
                    mkdir -p "${PACKDIR}/${current}/${cv}"
                    cd "${PACKDIR}/${current}/${cv}"
                    rm -f -- "./${cp}"
                    wget "${BASEURL}/${cp}"
                    7z x -- "${cp}"
                    if [ -f "${PACKDIR}/${current}/${cv}/package.content" ]; then
                        rm -f "${PACKDIR}/${current}/latest"
                        ln -s "${PACKDIR}/${current}/${cv}" "${PACKDIR}/${current}/latest"
                    fi
                fi
            fi
        fi
        current="$name"
        cp=""
        cv=0
    fi
    if version_gt "$version" "$cv"; then
        cv="$version"
        cp="$pack"
    fi
done