#!/usr/bin/bash

#############################################################
##
## This script is used to download the Freesurfer tutorial
## data onto the users machine. 
## 
## At the time of writing this script, the tutorial data 
## is about 5Gigs. And that represents only a subset
## of the entire tutorial data set required to run all 
## commands in the tutorial.
## 
## The tutorial data will be downloaded into the directory
## specified by the TUTORIAL_DATA environment variable, or 
## if undefined than it is installed to $FREESURFER_HOME/subjects/tutorial_data.
#############################################################

HELP=" 
 Usages:
  
 1) Tutorial data will be downloaded into TUTORIAL_DATA.
    If TUTORIAL_DATA is undefined, it will be downloaded
    into $FREESURFER_HOME/subjects/tutorial_data

    $> fs_tutorial_data

 2) Tuturial data will by downloaded into TUTORIAL_DATA
    with additional rsync flags:

    $> fs_tutorial_data <rsync_options>

 3) Show help:
 
    $> fs_update -h, -help, --help 
"

RSYNC_COMMAND="rsync -ztrlv --progress"

while [[ $# > 0 ]]; do
  key="$1"
  case $key in
    -h|-help|--h|--help)
        echo "${HELP}"
        exit 1
        ;;
    *) 
        RSYNC_COMMAND="${RSYNC_COMMAND} ${key}"
        ;;
  esac
  shift
done

## Figure out where we want to install this data.
if [[ -z "$TUTORIAL_DATA" ]]; then
  TUTORIAL_DATA=$FREESURFER_HOME/subjects/tutorial_data
fi
if [[ ! -d "$TUTORIAL_DATA" ]]; then
  mkdir -p "$TUTORIAL_DATA"
fi

## Begin download
echo    " Tutorial data will be downloaded and installed into:" 
echo
echo    "   $TUTORIAL_DATA"
echo
read -p " Shall I proceed? [y/n/Abort]: "
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    ${RSYNC_COMMAND} rsync://surfer.nmr.mgh.harvard.edu/pub/data/tutorial_data/ $TUTORIAL_DATA
    exit 0
else
    exit 1
fi
