#!/bin/bash

#
# Only run one instance of this script.
#

if pidof -x $(basename $0) > /dev/null; then
  for p in $(pidof -x $(basename $0)); do
    if [ $p -ne $$ ]; then
      echo "Script $0 is already running: exiting"
      exit
    fi
  done
fi

[ ! -d git ] && mkdir git

#
# pull_all is set once a day in case we are missing a commit event
#

if [ -e git/pull_all ]; then
 rm git/pull_all
 pull_all=1
else
 pull_all=0
fi

# eeschema segfaults if DISPLAY is not set
export DISPLAY=:0

#
# $1 url to git repo
# $2 local directory name
#

function is_git_updated
{
 if [ ! -d git/$2 ]; then
  rm -f git/$2.last git/$2.pull_needed
  (cd git && git clone $1 $2)
 elif [ $pull_all -gt 0 ] || [ -e git/$2.pull_needed ]; then
  rm -f git/$2.pull_needed
  (cd git/$2 && git pull)
 fi
 if [ -e git/$2.last ]; then
  LAST_COMMIT=`cat git/$2.last`
 fi
 CURRENT_COMMIT=`cd git/$2 && git rev-parse @{0}`
 if [ "$LAST_COMMIT" != "$CURRENT_COMMIT" ]; then
  echo $CURRENT_COMMIT > git/$2.last
  return 1
 fi
 return 0
}

#
# run_schhist2web parameters
#  -n -f -S -c cache: will be passed to schhist2web
#  --order order: will become SCHHIST_ORDER
#  --title title: will become SCHHIST_TITLE
#  $1 local git directory
#  $2 root schematic
#  $3 output directory
#  $4 remote url home
#  $5 remote url commit template
#

function run_schhist2web
{
 unset SCHHIST_TITLE
 unset SCHHIST_ORDER
 while true; do
 case "$1" in
  -n | -f | -S) schhist2web_options="$schhist2web_options $1"
      shift;;
  -c) schhist2web_options="$schhist2web_options $1 $2"
      shift 2;;
  --order) export SCHHIST_ORDER=$2; shift 2;;
  --title) export SCHHIST_TITLE=$2; shift 2;;
  *)  break;;
 esac
 done
 export SCHHIST_HOME_URL=$4
 export SCHHIST_COMMIT_TEMPLATE=$5
 export SCHHIST_ADDLIBS=device
 ./eda-tools/schhist/schhist2web $schhist2web_options git/$1 $2 $3
}

#
# main
#

is_git_updated git://projects.qi-hardware.com/kicad-libs.git kicad-libs

is_git_updated git://projects.qi-hardware.com/ben-wpan.git ben-wpan || (

 run_schhist2web -S --title ben-wpan/atusb --order atusb:USB:RF \
	ben-wpan atusb/atusb.sch output/atusb \
	http://projects.qi-hardware.com/p/ben-wpan/ \
	"http://projects.qi-hardware.com/p/ben-wpan/source/commit/{}/"

 run_schhist2web -S --title ben-wpan/cntr \
	ben-wpan cntr/cntr.sch output/cntr \
	http://projects.qi-hardware.com/p/ben-wpan/ \
	"http://projects.qi-hardware.com/p/ben-wpan/source/commit/{}/"

 run_schhist2web -S --title ben-wpan/atben \
	ben-wpan atben/atben.sch output/atben \
	http://projects.qi-hardware.com/p/ben-wpan/ \
	"http://projects.qi-hardware.com/p/ben-wpan/source/commit/{}/"
)

is_git_updated https://github.com/milkymist/board-m1.git board-m1 || (

 run_schhist2web -S --title "Milkymist One" --order m1:POWER:FPGA_Dec:FPGA_BANK0:FPGA_BANK1:FPGA_BANK2:FPGA_BANK3:EXPANSION_RESET:DRAM:NOR_FLASH:AUDIO:DVI-I:VIDEO_IN:MISC_CONTROL:ETHERNET:USB_A-B:USB_C-D:USB_E-F:MISC \
	board-m1 r4/m1.sch output/board-m1 \
	https://github.com/milkymist/board-m1/ \
	"https://github.com/milkymist/board-m1/commit/{}"
)

is_git_updated https://github.com/kristianpaul/m1-gps-expansion.git m1-gps-expansion || (

 run_schhist2web -S --title "M1 GPS expansion board" \
	m1-gps-expansion gps.sch output/m1-gps-expansion \
	https://github.com/kristianpaul/m1-gps-expansion/ \
	"https://github.com/kristianpaul/m1-gps-expansion/commit/{}"

)

is_git_updated git://projects.qi-hardware.com/m1.git m1 || (

 run_schhist2web -S --title "Milkymist One JTAG-serial" \
	m1 jtag-serial/usb_jtag.sch output/m1-jtag-serial \
	http://projects.qi-hardware.com/p/m1/ \
	"http://projects.qi-hardware.com/p/m1/source/commit/{}/"
)

is_git_updated git://projects.qi-hardware.com/ben-blinkenlights.git ben-blinkenlights || (

 run_schhist2web -S --title "Ben Blinkenlights" \
	ben-blinkenlights bbl.sch output/ben-blinkenlights \
	http://projects.qi-hardware.com/p/ben-blinkenlights/ \
	"http://projects.qi-hardware.com/p/ben-blinkenlights/source/commit/{}/"

 run_schhist2web -S --title "UBB VGA (ben-blinkenlights)" \
	ben-blinkenlights ubb-vga/ubb-vga.sch output/ubb-vga \
	http://projects.qi-hardware.com/p/ben-blinkenlights/ \
	"http://projects.qi-hardware.com/p/ben-blinkenlights/source/commit/{}/"
)

is_git_updated git://projects.qi-hardware.com/board-qi-avt2.git board-qi-avt2 || (

 run_schhist2web -S --title AVT2 \
	board-qi-avt2 main/Qi_AVT2.sch output/board-qi-avt2 \
	http://projects.qi-hardware.com/p/board-qi-avt2/ \
	"http://projects.qi-hardware.com/p/board-qi-avt2/source/commit/{}/"
)

is_git_updated git://projects.qi-hardware.com/f32xbase.git f32xbase || (

 run_schhist2web -S --title c2ben \
	f32xbase c2ben/c2ben.sch output/f32xbase_c2ben \
	http://projects.qi-hardware.com/p/f32xbase/ \
	"http://projects.qi-hardware.com/p/f32xbase/source/commit/{}/"
)

is_git_updated git://projects.qi-hardware.com/wernermisc.git wernermisc || (

 run_schhist2web -S --title labsw \
	wernermisc labsw/labsw.sch output/labsw \
	http://projects.qi-hardware.com/p/wernermisc/ \
	"http://projects.qi-hardware.com/p/wernermisc/source/commit/{}/"
)