#!/bin/bash -e

# build
#	Simple wiringPi build and install script
#
#	Copyright (c) 2012-2015 Gordon Henderson
#################################################################################
# This file is part of wiringPi:
#	A "wiring" library for the Raspberry Pi
#
#    wiringPi is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    wiringPi 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 Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
#################################################################################
#
# wiringPi is designed to run on a Raspberry Pi only.
#	However if you're clever enough to actually look at this script to
#	see why it's not building for you, then good luck.
#
#	To everyone else: Stop using cheap alternatives. Support the
#	Raspberry Pi Foundation	as they're the only ones putting money
#	back into education!
#################################################################################

check_make_ok() {
  if [ $? != 0 ]; then
    echo ""
    echo "Make Failed..."
    echo "Please check the messages and fix any problems. If you're still stuck,"
    echo "then please email all the output and as many details as you can to"
    echo "  projects@drogon.net"
    echo ""
    exit 1
  fi
}

select_boards()
{
	local cnt=0
	local choice
	local call=${1}

	boards=("orangepir1"
		"orangepizero"
		"orangepizero-lts"
		"orangepipc"
		"orangepipch5"
		"orangepipcplus"
		"orangepiplus2e"
		"orangepione"
		"orangepioneh5"
		"orangepilite"
		"orangepiplus"
		"orangepizeroplus2h3"
		"orangepizeroplus"
		"orangepipc2"
		"orangepiprime"
		"orangepizeroplus2h5"
		"orangepiwin"
		"orangepiwinplus"
		"orangepi3"
		"orangepi3-lts"
		"orangepilite2"
		"orangepioneplus"
		"orangepi4"
        "orangepi4-lts"
		"orangepirk3399"
		"orangepi800"
		"orangepizero2"
		"orangepizero2-lts"
		"orangepizero2-b"
		"orangepizero3"
		"orangepir1plus-lts"
		"orangepir1plus"
        "blueberry")

	if [[ -f /etc/orangepi-release ]]; then

		source /etc/orangepi-release

	elif [[ -f /etc/armbian-release ]]; then

		source /etc/armbian-release
		[[ $BOARD == orangepi-r1 ]] && BOARD=orangepir1
		[[ $BOARD == orangepi-rk3399 ]] && BOARD=orangepirk3399
		[[ $BOARD == orangepizeroplus2-h3 ]] && BOARD=orangepizeroplus2h3
		[[ $BOARD == orangepizeroplus2-h5 ]] && BOARD=orangepizeroplus2h5

	else

		printf "All available boards:\n"
		for var in ${boards[@]} ; do
			printf "%4d. %s\n" $cnt ${boards[$cnt]}
			((cnt+=1))
		done

		while true ; do
		read -p "Choice: " choice
		if [ -z "${choice}" ] ; then
			continue
		fi
		if [ -z "${choice//[0-9]/}" ] ; then
			if [ $choice -ge 0 -a $choice -lt $cnt ] ; then
			export BOARD="${boards[$choice]}"
			break
			fi
		fi
		printf "Invalid input ...\n"
		done
	fi

	[[ $BOARD == orangepir1 ]] && BOARD=orangepir1-h2
	[[ $BOARD == orangepizero ]] && BOARD=orangepizero-h2
	[[ $BOARD == orangepizero-lts ]] && BOARD=orangepizero-h2
	[[ $BOARD == orangepipc ]] && BOARD=orangepipc-h3
	[[ $BOARD == orangepipch5 ]] && BOARD=orangepipc-h3
	[[ $BOARD == orangepipcplus ]] && BOARD=orangepipcplus-h3
	[[ $BOARD == orangepiplus2e ]] && BOARD=orangepiplus2e-h3
	[[ $BOARD == orangepione ]] && BOARD=orangepione-h3
	[[ $BOARD == orangepioneh5 ]] && BOARD=orangepione-h3
	[[ $BOARD == orangepilite ]] && BOARD=orangepilite-h3
	[[ $BOARD == orangepiplus ]] && BOARD=orangepiplus-h3
	[[ $BOARD == orangepizeroplus ]] && BOARD=orangepizeroplus-h5
	[[ $BOARD == orangepipc2 ]] && BOARD=orangepipc2-h5
	[[ $BOARD == orangepiprime ]] && BOARD=orangepiprime-h5
	[[ $BOARD == orangepiwin ]] && BOARD=orangepiwin-a64
	[[ $BOARD == orangepiwinplus ]] && BOARD=orangepiwinplus-a64
	[[ $BOARD == orangepi3 ]] && BOARD=orangepi3-h6
	[[ $BOARD == orangepi3-lts ]] && BOARD=orangepi3-h6
	[[ $BOARD == orangepilite2 ]] && BOARD=orangepilite2-h6
	[[ $BOARD == orangepioneplus ]] && BOARD=orangepioneplus-h6
	[[ $BOARD == orangepizero2 ]] && BOARD=orangepizero2-h616
	[[ $BOARD == orangepizero2-lts ]] && BOARD=orangepizero2-h616
	[[ $BOARD == orangepizero2-b ]] && BOARD=orangepizero2-h616
	[[ $BOARD == orangepizero3 ]] && BOARD=orangepizero2-h616
	[[ $BOARD == orangepir1plus ]] && BOARD=orangepir1plus-rk3328
	[[ $BOARD == orangepir1plus-lts ]] && BOARD=orangepir1plus-rk3328
    [[ $BOARD == blueberry ]] && BOARD=blueberry

	export BOARD="${BOARD}"
}

sudo=${WIRINGPI_SUDO-sudo}

if [ x$1 = "xclean" ]; then
  cd wiringPi
  echo -n "wiringPi:   "	; make V=1 clean
  cd ../devLib
  echo -n "DevLib:     "	; make V=1 clean
  cd ../gpio
  echo -n "gpio:       "	; make V=1 clean
  cd ../examples
  echo -n "Examples:   "	; make V=1 clean
  cd Gertboard
  echo -n "Gertboard:  "	; make V=1 clean
  cd ../PiFace
  echo -n "PiFace:     "	; make V=1 clean
  cd ../q2w
  echo -n "Quick2Wire: "	; make V=1 clean
  cd ../PiGlow
  echo -n "PiGlow:     "	; make V=1 clean
  cd ../scrollPhat
  echo -n "scrollPhat: "	; make V=1 clean
  cd ../..
  echo -n "Deb: " ; rm  -f debian-template/wiringpi*.deb
  echo
  exit
fi

if [ x$1 = "xuninstall" ]; then
  cd wiringPi
  echo -n "wiringPi: " ; $sudo make V=1 uninstall
  cd ../devLib
  echo -n "DevLib:   " ; $sudo make V=1 uninstall
  cd ../gpio
  echo -n "gpio:     " ; $sudo make V=1 uninstall
  exit
fi

# Only if you know what you're doing!

if [ x$1 = "xdebian" ]; then
  here=`pwd`
  echo "removing old libs"
  cd debian-template/wiringPi
  rm -rf usr
  echo "building wiringPi"
  cd $here/wiringPi
  make V=1 install-deb
  echo "building devLib"
  cd $here/devLib
  make V=1 install-deb INCLUDE='-I. -I../wiringPi'
  echo "building gpio"
  cd $here/gpio
  make V=1 install-deb INCLUDE='-I../wiringPi -I../devLib' LDFLAGS=-L../debian-template/wiringPi/usr/lib
  echo "Building deb package"
  cd $here/debian-template
  fakeroot dpkg-deb --build wiringPi
  mv wiringPi.deb  wiringpi-`cat $here/VERSION`-1.deb
  exit
fi

if [ x$1 != "x" ]; then
  echo "Usage: $0 [clean | uninstall]"
  exit 1
fi

  select_boards

  echo "wiringPi Build script"
  echo "====================="
  echo

  hardware=`fgrep Hardware /proc/cpuinfo | head -1 | awk '{ print $3 }'`

#  if [ x$hardware != "xBCM2708" ]; then
#    echo ""
#    echo "   +------------------------------------------------------------+"
#    echo "   |   wiringPi is designed to run on the Raspberry Pi only.    |"
#    echo "   |   This processor does not appear to be a Raspberry Pi.     |"
#    echo "   +------------------------------------------------------------+"
#    echo "   | In the unlikely event that you think it is a Raspberry Pi, |"
#    echo "   | then please accept my apologies and email the contents of  |"
#    echo "   | /proc/cpuinfo to projects@drogon.net.                      |"
#    echo "   |    - Thanks, Gordon                                        |"
#    echo "   +------------------------------------------------------------+"
#    echo ""
#    exit 1
#  fi


  echo
  echo "WiringPi Library"
  cd wiringPi
  $sudo make V=1 uninstall
  if [ x$1 = "xstatic" ]; then
    make V=1 -j5 static
    check_make_ok
    $sudo make V=1 install-static
  else
    make V=1 -j5
    check_make_ok
    $sudo make V=1 install
  fi
  check_make_ok

  echo
  echo "WiringPi Devices Library"
  cd ../devLib
  $sudo make V=1 uninstall
  if [ x$1 = "xstatic" ]; then
    make V=1 -j5 static
    check_make_ok
    $sudo make V=1 install-static
  else
    make V=1 -j5
    check_make_ok
    $sudo make V=1 install
  fi
  check_make_ok

  echo
  echo "GPIO Utility"
  cd ../gpio
  make V=1 -j5
  check_make_ok
  $sudo make V=1 install
  check_make_ok

# echo
# echo "wiringPi Daemon"
# cd ../wiringPiD
# make V=1 -j5
# check_make_ok
# $sudo make V=1 install
# check_make_ok

# echo
# echo "Examples"
# cd ../examples
# make V=1
# cd ..

echo
echo All Done.
echo ""
echo "NOTE: To compile programs with wiringPi, you need to add:"
echo "    -lwiringPi"
echo "  to your compile line(s) To use the Gertboard, MaxDetect, etc."
echo "  code (the devLib), you need to also add:"
echo "    -lwiringPiDev"
echo "  to your compile line(s)."
echo ""