#!/usr/bin/env bash

#This bash script trys to install a python package *first* by using a wheel,
#and if here isn't one there or it fails, it falls
#back on using pip to build from source.

USAGE=".travis.pip.wheel PYVERSION PACKAGENAME [PACKAGEVERSION] [-q] [-p]"
#"-q" means use "pip install -q ..." if a source install is needed
#"-p" means it's a pure-python package (necessary for looking up wheels correctly)

#these are variables that help locate the wheels

BASEWHEELURL="http://www.mpia.de/~robitaille/python/wheels"
PLATFORM="linux_x86_64"

#array mapping python versions to their wheel code
#this is based on wheels made for Ubuntu 12.04 64-bit, April 2013
#this is for non pure-python code - it requires extra information about the C compilation flags
declare -A pyvtowheelcodecp=(
  [2.6]="cp26-none"
  [2.7]="cp27-none"
  [3.2]="cp32-32mu"
  [3.3]="cp33-33dm"
)
#this is for pure-python
declare -A pyvtowheelcodepy=(
  [2.6]="py26-none"
  [2.7]="py27-none"
  [3.2]="py32-32mu"
  [3.3]="py33-33dm"
)



#process command line arguments
PYVERSION=""
PKGNAME=""
PKGVERS=""
PIPFLAGS=""
PUREPYTHON=false
while [ "$1" != "" ]; do
    if [ "$1" == '-q' ]; then
      PIPFLAGS="-q"
    elif [ "$1" == '-p' ]; then
      PUREPYTHON=true
    elif [[ $1 == -* ]]; then
      echo "Invalid option $1. Usage: $USAGE"
      exit 1
    elif [ "$PYVERSION" == "" ]; then
      PYVERSION="$1"
    elif [ "$PKGNAME" == "" ]; then
      PKGNAME="$1"
    elif [ "$PKGVERS" == "" ]; then
      PKGVERS="$1"
    else
      echo "Too many arguments $1. Usage: $USAGE"
      exit 1
    fi
    shift
done
if [ "$PYVERSION" == "" -o "$PKGNAME" == "" ]; then
  echo "Not enough arguments. Usage: $USAGE"
  exit 1
fi
#done w/command line arguments

#figure out the version code
if $PUREPYTHON
then
  PYVERSION=${pyvtowheelcodepy[$PYVERSION]}
  #the pure python code, the platform is "any"
  PLATFORM="any"
else
  PYVERSION=${pyvtowheelcodecp[$PYVERSION]}
fi

# now figure out the name sphinx wants to see in case we need to fall back
if [ "$PKGVERS" == "" ]
then
  PACKAGENAMEANDVERSDASH="$PKGNAME"
  PACKAGENAMEANDVERSEQ="$PKGNAME"
else
  PACKAGENAMEANDVERSDASH="$PKGNAME-$PKGVERS"
  PACKAGENAMEANDVERSEQ="$PKGNAME==$PKGVERS"
fi

#now construct the url
WHEELURL="$BASEWHEELURL/$PACKAGENAMEANDVERSDASH-$PYVERSION-$PLATFORM.whl"

echo "Trying to install with wheel:"
echo "pip install $WHEELURL"
pip install $WHEELURL
if [ $? -ne 0 ]
then
  echo "Wheel install failed, trying to build from source:"
  echo "pip install $PACKAGENAMEANDVERSEQ --use-mirrors $PIPFLAGS"
  pip install $PACKAGENAMEANDVERSEQ --use-mirrors $PIPFLAGS
fi
exit $?
