#!/bin/bash

print_help () {
    echo "Builds images of specified services. If no option specified, builds images of all services."
    echo "This script should be run when a new version of the project with updated requirements or Dockerfile is pulled"
    echo ""
    echo "options:"
    echo "-h, --help            show help"
    echo "--detection           build detection service image"
    echo "--tracking            build tracking service image"
    echo "--pose3d              build 3d pose estimation service image"
    echo "--distance            build distance estimation service image"
    echo "--actions             build actions classification service image"
}

devbeh_all=true
devbeh_detection=false
devbeh_tracking=false
devbeh_pose3d=false
devbeh_distance=false
devbeh_actions=false
while test $# -gt 0; do
    case "$1" in
        -h|--help)
            print_help
            exit 0
            ;;
        --detection)
            devbeh_all=false
            devbeh_detection=true
            shift
            ;;
        --tracking)
            devbeh_all=false
            devbeh_tracking=true
            shift
            ;;
        --pose3d)
            devbeh_all=false
            devbeh_pose3d=true
            shift
            ;;
        --distance)
            devbeh_all=false
            devbeh_distance=true
            shift
            ;;
        --actions)
            devbeh_all=false
            devbeh_actions=true
            shift
            ;;
        *)
            shift
            ;;
    esac
done

if [ "$devbeh_all" = true ] || [ "$devbeh_detection" = true ]; then
  devbeh_services="$devbeh_services detection"
fi

if [ "$devbeh_all" = true ] || [ "$devbeh_tracking" = true ]; then
  devbeh_services="$devbeh_services tracking"
fi

if [ "$devbeh_all" = true ] || [ "$devbeh_pose3d" = true ]; then
  devbeh_services="$devbeh_services pose3d"
fi

if [ "$devbeh_all" = true ] || [ "$devbeh_distance" = true ]; then
  devbeh_services="$devbeh_services distance"
fi

if [ "$devbeh_all" = true ] || [ "$devbeh_actions" = true ]; then
  devbeh_services="$devbeh_services actions"
fi

current_dir=$(pwd)
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
# shellcheck disable=SC2086
docker-compose build --parallel $devbeh_services
cd "$current_dir" || exit 1