#!/bin/sh

#set default options
PYTHON="--python3"

USR_BIN_FILES="backintime-qt4"
DBUS_SERVICE_FILES="net.launchpad.backintime.serviceHelper.service"

usage () {
echo "Usage:"
echo "$0 [--python | --python3]"
echo ""
echo "--python"
echo "\tuse 'python' to start Python3"
echo "--python3"
echo "\tuse 'python3' to start Python3"
}

#get commandline arguments
for arg in $*; do
	case $arg in
		--python | --python3) PYTHON=$arg;;
		--help | -h | *) usage; exit 0;;
	esac
done

#patch python command
#use 'python' or 'python3' to start Python Version 3.x
case $PYTHON in
	--python)   PYVERSION="" ;;
	--python3)  PYVERSION="3";;
esac
sed -e "s#^python3\? #python${PYVERSION} #g" \
    -e "s#^ssh-agent python3\? #ssh-agent python${PYVERSION} #g" \
    -i $USR_BIN_FILES
sed -e "s#^Exec=/usr/bin/python3\? #Exec=/usr/bin/python${PYVERSION} #g" \
    -i $DBUS_SERVICE_FILES

cp Makefile.template Makefile

if [ $(python${PYVERSION} --version 2>&1 | grep -c "^Python 3") -ne 1 ]; then
    printf "Wrong Python version.\n"
    printf "Please make sure Python 3.x is used by adding '--python' or '--python3'.\n"
    exit 1
fi

printf "All OK. Now run:\n"
printf "    make\n"
printf "    sudo make install\n"

