# This file will build the project from source using the buildah toolchain.
# It will create a container for the host to push up to the container repository.

# NAME="flowms-web"

function query_largest_vcs_semver {
    # Get the largest Semantic Version number of
    # the project in the current working directory.

    git tag | awk '{ if ($1 ~ /-/) print; else print $0"_" ; }' | sort -rV | sed 's/_$//' | awk 'NR==1{print $1}'
}

function query_latest_vcs_commit {
    # Get the latest shortened git tag.

    git log --oneline --max-count=1 | cut -d ' ' --fields=1
}

function build_wc_clearup {
    # This function takes a working container name or ID and
    # deletes it.

    local working_container=$1

    buildah rm "${working_container}"
}

function build_container {
    # Container build orchistrator function.
    # It takes a project name, a working container name or ID, and a binary file name.

    local name=$1
    local working_container=$2

    { build_setup_container "${working_container}"
      build_config_container "${working_container}"
      build_import_artifacts "${working_container}"
      build_finalise_container "${working_container}" "${name}"
    } || {
	build_wc_clearup "${working_container}"
	return 1
    }

}

function build_setup_container {
    # This function takes a working container name or ID and sets up the standard folders
    # required for the project.

    local name=$1

    buildah run "${name}" -- ash -c 'apk update && apk upgrade' || return 1
}

function build_config_container {
    # This function takes a working container name or ID and sets the working directory
    # and launch command.

    local name=$1

    { buildah config --workingdir '/usr/share/caddy' "${name}"
      buildah config --env SITE_ADDRESS="${SITE_ADDRESS}" "${name}"
    } || return 1
}

function build_import_artifacts {
    # This function takes a working container name or ID and a name for a binary.
    # It copies database migration files and the binary from the current working directory
    # and makes it executable.

    local name=$1

    { buildah copy "${name}" 'docs/' '/usr/share/caddy/'
      buildah copy "${name}" 'Caddyfile' '/etc/caddy/Caddyfile'
    } || return 1
}

function build_finalise_container {
    # Commit the final image.
    # Takes a working container name or ID and a project name.

    local con_name=$1
    local proj_name=$2

    { buildah commit "${con_name}" "docker.io/lavalogic/${proj_name}"
      build_wc_clearup "${con_name}"
    } || return 1
}

function build_tag_container {
    # Tag container.
    # Takes a container name and a tag.

    local name=$1
    local tag=$2

    buildah tag "docker.io/lavalogic/${name}" "docker.io/lavalogic/${name}:${tag}"
}

function build_repo_login {
    # Log in to a repository.

    local repo=$1

    buildah login "${repo}"
}

function build_push_image {
    # This function will push a container image to a repository.
    # It takes a short description (This can be anything, but if it is specifically "release"
    # then it does not send the "latest" tag with the image), a container name, and a tag.

    local desc=$1
    local name=$2
    local tag=$3

    if
	[ "${desc}" = "release" ]
    then
	buildah push "docker.io/lavalogic/${name}:${tag}" || {
	    build_clearup_images "docker.io/lavalogic/${name}"
	    return 1
	}
    else
	{ buildah push "docker.io/lavalogic/${name}:${tag}"
	  buildah push "docker.io/lavalogic/${name}:latest"
	} || {
	    build_clearup_images "docker.io/lavalogic/${name}"
	    return 1
	}
    fi

    build_clearup_images "docker.io/lavalogic/${name}"
}

function build_clearup_images {
    # Removes all images of named container.
    # Takes a container name.

    local name=$1

    buildah rmi -f $(buildah images -q "${name}")
}

function kube_update_image {
    # This function updates an image in a deployment with the requested tag.
    # It takes a deployment name, an image definition (in the format podname=full/image/name:tag),
    # and a Kubernetes cluster context.

    local deployment=$1
    local image=$2
    local context=$3

    (kubectl get deployments/"${deployment}" --context="${context}") || return 1
    kubectl set image deployments/"${deployment}" "${image}" --context="${context}"
}

function kube_restart_deployment {
    # This function restarts the specified deployment.
    # It takes a deployment name.

    local deployment=$1

    kubectl rollout restart deployments/"${deployment}" --context="${context}" || return 1
}
