#!/bin/bash

case $1 in
    -a|--alpha)
        generation=4
        version=$(curl -s https://dlcdn.apache.org/maven/maven-4/ | grep -oP "\d+\.\d+\.\d+\-alpha-\d+" | sort -rV | head -n 1)
    ;;

    *)
        generation=3
        version=$(curl -s https://dlcdn.apache.org/maven/maven-3/ | grep -oP "\d+\.\d+\.\d+" | sort -rV | head -n 1)
    ;;
esac

wget https://dlcdn.apache.org/maven/maven-$generation/$version/binaries/apache-maven-$version-bin.zip
unzip apache-maven-$version-bin.zip

mv apache-maven-$version /usr/local/mvn

ln -fs /usr/local/mvn/bin/mvn /usr/bin
ln -fs /usr/local/mvn/bin/mvnyjp /usr/bin
ln -fs /usr/local/mvn/bin/mvnDebug /usr/bin

rm -r apache-maven-$version-bin.zip