def validate_input():
    mesos_build = VagrantConfig.mesos_build()
    marathon_build = VagrantConfig.marathon_build()
    operating_system = VagrantConfig.operating_system()

    if operating_system not in VagrantConfig.supported_operating_systems():
        Utils.exit_with_cmd_error( __file__,
                                   "Operating system ({}) is not supported. Available values: {}".format(
                                    operating_system,
                                    str(VagrantConfig.supported_operating_systems()) ))
    
    if mesos_build == "":
        Utils.exit_with_cmd_error( __file__, "Mesos build version not given. Run mesos-toolbox show-builds to see what the available builds are.")
    if marathon_build == "":
        Utils.exit_with_cmd_error( __file__, "Marathon build version not given. Run marathon-toolbox show-builds to see what the available builds are.")
    mesos_build_name = "{}-{}".format(VagrantConfig.mesos_build(), VagrantConfig.operating_system().replace(":", "-"))
    if mesos_build_name not in Utils.list_builds("{}/mesos".format(VagrantConfig.mesos_packages_dir())):
        Utils.exit_with_cmd_error( __file__, "Mesos build {} does not exist. Please build that version first with mesos-toolbox.".format(mesos_build_name))
    if VagrantConfig.marathon_build() not in Utils.list_builds(VagrantConfig.marathon_packages_dir()):
        Utils.exit_with_cmd_error( __file__, "Marathon build {} does not exist. Please build that version first with mesos-toolbox.".format(VagrantConfig.marathon_build()))
            LOG.error( "Marathon build failed. Leaving build log and temp directory for inspection. marathon={}".format( build_dir ) )
            exit(107)

def op_show_releases():
    if Utils.ensure_sources(LOG, MarathonConfig.marathon_repository_dir(), MarathonConfig.marathon_git_repository()):
        LOG.info("Releases:")
        for line in list_releases():
            if line != "":
                print line

def op_remove_build():
    validate_input()
    if not Utils.confirm("You are about to remove Marathon build for {}.".format( MarathonConfig.marathon_version() )):
        exit(0)
    Utils.cmd("rm -rf {}/{}".format( MarathonConfig.packages_dir(),
                                     MarathonConfig.marathon_version()))

def op_remove_sources():
    if not Utils.confirm("You are about to remove Marathon sources for {}.".format( MarathonConfig.marathon_git_repository() )):
        exit(0)
    Utils.cmd("rm -rf {}".format( MarathonConfig.marathon_repository_dir() ))

if __name__ == "__main__":

    if "build" == MarathonConfig.command(): op_build()
    if "show-releases" == MarathonConfig.command(): op_show_releases()
    if "show-builds" == MarathonConfig.command(): Utils.list_builds( LOG, MarathonConfig.packages_dir() )
    if "remove-build" == MarathonConfig.command(): op_remove_build()
    if "show-sources" == MarathonConfig.command(): Utils.list_sources(MarathonConfig.source_dir(), 'marathon')
    if "remove-sources" == MarathonConfig.command(): op_remove_sources()
    
            else:
                LOG.info("Patch applied.")
        else:
            LOG.info("No patches for mesos-deb-packaging {}.".format( MesosConfig.deb_packaging_sha() ))

        # ensure branch / tag
        Utils.exit_if_git_release_not_set( LOG,
                                           build_dir_mesos,
                                           MesosConfig.mesos_version(),
                                           MesosConfig.mesos_master_branch(),
                                           MesosConfig.mesos_git_repository() )

        # We have the right sources now:
        if MesosConfig.operating_system() == "osx":
            build_with_osx( build_dir_mesos, build_dir_packaging, packages_dir )
        else:
            build_with_docker( build_dir_mesos, build_dir_packaging, packages_dir )

if __name__ == "__main__":

    if "build" == MesosConfig.command(): op_build()
    if "docker" == MesosConfig.command(): op_docker_image()
    if "show-releases" == MesosConfig.command(): op_show_releases()
    if "show-builds" == MesosConfig.command(): Utils.list_builds( LOG, MesosConfig.packages_dir() )
    if "remove-build" == MesosConfig.command(): op_remove_build()
    if "show-mesos-sources" == MesosConfig.command(): Utils.list_sources(MesosConfig.source_dir(), 'mesos')
    if "show-packaging-sources" == MesosConfig.command(): Utils.list_sources(MesosConfig.source_dir(), 'mesos-packaging')
    if "remove-mesos-sources" == MesosConfig.command(): op_remove_mesos_sources()
    if "remove-packaging-sources" == MesosConfig.command(): op_remove_packaging_sources()
    if "check-this-system" == MesosConfig.command(): op_check_this_system()