#!/bin/sh

set -e -x

# TODO.  Several variables are not properly quoted/escaped where they are
# passed to another shell.

project=$1
build_suffix=.build
case $project in
  gcc)
    branch=hurd/master
    linux_host=kepler.SCHWINGE
    hurd_host=coulomb.SCHWINGE
    ;;
  glibc)
    branch=tschwinge/Roger_Whittaker
    #linux_host=kepler.SCHWINGE
    hurd_host=coulomb.SCHWINGE
    build_suffix=.build-gcc-4.6-486
    ;;
  *)
    echo >&2 "Don't know about project »$project«."
    exit 1
    ;;
esac
shift

step=$1
case $step in
  build | install | test)
    ;;
  '')
    # If there is no step specified, try a default set.
    "$0" "$project" build
    "$0" "$project" install
    "$0" "$project" test
    exit
    ;;
  *)
    echo >&2 "Don't know about step »$step«."
    exit 1
    ;;
esac
shift

action=$1
case $action in
  fetch | diff)
    ;;
  '')
    # If there is no action specified, try a default set.
    "$0" "$project" "$step" fetch
    "$0" "$project" "$step" diff
    exit
    ;;
  *)
    echo >&2 "Don't know about action »$action«."
    exit 1
    ;;
esac
shift

case $project:$action in
  gcc:fetch | glibc:fetch)
    host=$1
    case $host in
      coulomb.SCHWINGE)
        project_base=tmp/"$project"
        mount=/media/erich
        ;;
      kepler.SCHWINGE)
        project_base=tmp/source/"$project"
        mount=/media/data
        ;;
      '')
        # If there is no host specified, try a default set.
        : "${linux_host:?}"
        : "${hurd_host:?}"
        "$0" "$project" "$step" "$action" "$linux_host"
        "$0" "$project" "$step" "$action" "$hurd_host"
        exit
        ;;
      *)
        echo >&2 "Don't know about host »$host«."
        exit 1
        ;;
    esac
    shift

    [ $# = 0 ]

    : "${branch:?}"
    : "${build_suffix:?}"
    : "${host:?}"
    : "${mount:?}"
    : "${project_base:?}"
    case $project:$step in
      gcc:build | gcc:install \
        | glibc:build | glibc:install | glibc:test)
        ssh \
          "$host" \
          'cd '"$project_base"'/ && \
          cat '"$branch$build_suffix"'/log_'"$step"'* \
            | sed -e "s%\('"$mount"'\)\?${PWD}%[...]%g"' \
          > toolchain/logs/"$project"/"$host"/"$step"
        exit
        ;;
      gcc:test)
        rsync \
          -vd --delete \
          --rsync-path='\
            cd '"$project_base"'/ && \
            base=$PWD && \
            rm -rf '"$branch$build_suffix"'.sums && \
            mkdir '"$branch$build_suffix"'.sums && \
            cd '"$branch$build_suffix"'.sums/ && \
            find "$base"/'"$branch$build_suffix"'/ -name \*.sum \
              -exec cp -bt ./ \{\} \; && \
            for f in *; do \
              sed -e "s%\('"$mount"'\)\?${base}%[...]%g" -i "$f" \
              || exit $?; \
            done &&\
            rsync' "$host": \
          toolchain/logs/"$project"/"$host"/"$step"/
        exit
        ;;
      *)
        echo >&2 "Internal error."
        exit 1
        ;;
    esac
    ;;
  gcc:diff)
    [ $# = 0 ]

    : "${linux_host:?}"
    : "${hurd_host:?}"
    case $project:$step in
      gcc:build | gcc:install)
        sed \
          -f toolchain/logs/"$project"/"$linux_host"/"$step".sed \
          < toolchain/logs/"$project"/"$linux_host"/"$step" \
          > toolchain/logs/"$project"/"$linux_host"/"$step"_
        sed \
          -f toolchain/logs/"$project"/"$hurd_host"/"$step".sed \
          < toolchain/logs/"$project"/"$hurd_host"/"$step" \
          > toolchain/logs/"$project"/"$hurd_host"/"$step"_
        r=0
        diff \
          -wu -F ^Running \
          toolchain/logs/"$project"/"$linux_host"/"$step"_ \
          toolchain/logs/"$project"/"$hurd_host"/"$step"_ \
          > toolchain/logs/"$project"/"$step".diff \
        || r=$?
        rm \
          -f \
          toolchain/logs/"$project"/"$linux_host"/"$step"_ \
          toolchain/logs/"$project"/"$hurd_host"/"$step"_
        [ "$r" = 0 ] || [ "$r" = 1 ]
        exit
        ;;
      gcc:test)
        r=0
        diff \
          -Nrwu -F ^Running \
          toolchain/logs/"$project"/"$linux_host"/"$step"/ \
          toolchain/logs/"$project"/"$hurd_host"/"$step"/ \
          > toolchain/logs/"$project"/"$step".diff \
        || r=$?
        [ "$r" = 0 ] || [ "$r" = 1 ]
        exit
        ;;
      *)
        echo >&2 "Internal error."
        exit 1
        ;;
    esac
    ;;
  *)
    echo >&2 "Internal error."
    exit 1
    ;;
esac

echo >&2 "Internal error."
exit 1