#!/bin/bash
# Script to clone / update GITs according to a text-file
# and build Debian-Packages from them as well as sort them into the repository
# 2014-10-08 V1 by Michael Mrozek (EvilDragon)
#

# Setup some Env-Vars

builddir="/srv/www/vhosts/domains/packages.pyra-handheld.com/build"
gitlist="/srv/www/vhosts/domains/packages.pyra-handheld.com/build/packages.txt"
repodir="/srv/www/vhosts/domains/packages.pyra-handheld.com/httpdocs/debian"
logdir="/srv/www/vhosts/domains/packages.pyra-handheld.com/httpdocs/buildlogs"

# Package-Build-Loop
while read url          
do           
  # Clean old stuff
  rm "$builddir/*.build"
  rm "$builddir/*.changes"
  rm "$builddir/*.dsc"
  rm "$builddir/*.deb"
  rm "$builddir/*.tar.gz"
  build=false
  
  # Read the GIT URL
  gitdir="$(echo ${url%.*} | awk -F'/' '{print $NF}')"
  
  # Does GIT exist?
  if [ ! -d "$builddir/$gitdir" ]; then
    # Clone it!
    echo New Package! - Cloning $url
    git clone $url
    build=true
  else
    # Otherwise: Update the GIT.
    echo Update GIT
    cd "$builddir/$gitdir"
    git fetch
    
    # Check if the package needs to be rebuilt
    update="$(git rebase origin | grep "is up to date")"
    if [ -n "$update" ]; then
      build=false
    else
      build=true
    fi
  fi  
  
  # Build if Package is updated
  
  if [ $build == true ]; then
  
    # Get package name
    package="$(head -1 "${builddir}/${gitdir}/debian/control" | awk '{print $NF}')"
  
    # Run debuild
    cd "$builddir/$gitdir"
    debuild -us -uc -S
  
    # Build the package!
  
    cd "$builddir"
    sbuild -d jessie-pyra -c jessie-armhf --arch=armhf --arch-all --source

    # Put it into the repository

    name="$(ls *.changes)"
    reprepro -V -b "$repodir" include jessie-pyra $name
    
    # Move the build log files into the build-log-dir
    mv "$builddir/*.build" "$logdir/"
    
  fi

done <$gitlist