#!/bin/sh
################################################################################
#      This file is part of LibreELEC - https://LibreELEC.tv
#      Copyright (C) 2016 Team LibreELEC
#
#  LibreELEC is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 of the License, or
#  (at your option) any later version.
#
#  LibreELEC is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with LibreELEC.  If not, see <http://www.gnu.org/licenses/>.
################################################################################

# the script uses 7z to extract the iso to avoid root 
# sudo apt-get install p7zip-full

# set version you want to use
if [ -z "$1" ]; then
  echo "Usage: $0 5.0.123"
  exit 0
fi

VBOX_VERSION="$1"

echo "getting sources..."
  mkdir vbox && cd vbox/
  wget http://download.virtualbox.org/virtualbox/${VBOX_VERSION}/VBoxGuestAdditions_${VBOX_VERSION}.iso

echo "extracting iso..."
  7z e VBoxGuestAdditions_${VBOX_VERSION}.iso VBoxLinuxAdditions.run -r
  chmod 0755 VBoxLinuxAdditions.run

echo "extract VBoxLinuxAdditions.run..."
  ./VBoxLinuxAdditions.run --tar xf

echo "extract VBoxGuestAdditions-amd64..."
  mkdir xf86-video-virtualbox-${VBOX_VERSION} 
  tar -jxvf VBoxGuestAdditions-amd64.tar.bz2 -C xf86-video-virtualbox-${VBOX_VERSION}/

echo "packing sources..."
  tar cvJf xf86-video-virtualbox-${VBOX_VERSION}.tar.xz xf86-video-virtualbox-${VBOX_VERSION}
  mv xf86-video-virtualbox-${VBOX_VERSION}.tar.xz ../xf86-video-virtualbox-${VBOX_VERSION}.tar.xz

echo "remove temporary sourcedir..."
  cd ..  
  rm -rf vbox/
