#!/bin/bash

# Installer script for darts
# Author: Mohamed Bamakhrama (mohamed@liacs.nl)
# Copyrights (c) 2012, Leiden University. All rights reserved


##################################################################
# 
#  Packages
#
##################################################################

DARTS_PACKAGE_URL=http://daedalus.liacs.nl/darts/release_20120907/darts-0.4.tar.gz
DARTS_PACKAGE=`basename $DARTS_PACKAGE_URL`
DARTS_DIR_NAME=darts-0.4

##################################################################
# 
#  Functions
#
##################################################################

# Takes two parameters. The response from a command and error message
# If the response is not zero, print the error message and exit
check() {
    if [ $1 -ne 0 ]
    then
        echo "ERROR: $2"
        exit 1
    fi
}


# This function downloads and extracts darts. Requires Internet access
download_extract_packages() {

    # Download phase
    echo "wget -q -nc $DARTS_PACKAGE_URL"
    wget -q -nc $DARTS_PACKAGE_URL
    check $? "can not download $DARTS_PACKAGE"

    # Extraction phase
    tar -xvzf $DARTS_PACKAGE
    check $? "can not extract $DARTS_PACKAGE"    
    
}


#############################################################
#
# Start of the setup procedure
#
#############################################################

if [ "$1" = "clean" ] 
then
    # Clean existing directories
    rm -f $DARTS_PACKAGE   
    rm -rf $DARTS_DIR_NAME
        
    echo "All generated files removed"
    exit 0
fi

download_extract_packages

echo "darts installed successfully!"
exit 0

