+static qboolean noclear = FALSE;
+
+static int numdownloads_fail = 0;
+static int numdownloads_success = 0;
+static int numdownloads_added = 0;
+static char command_when_done[256] = "";
+static char command_when_error[256] = "";
+
+/*
+====================
+Curl_CommandWhenDone
+
+Sets the command which is to be executed when the last download completes AND
+all downloads since last server connect ended with a successful status.
+Setting the command to NULL clears it.
+====================
+*/
+void Curl_CommandWhenDone(const char *cmd)
+{
+ if(!curl_dll)
+ return;
+ if(cmd)
+ strlcpy(command_when_done, cmd, sizeof(command_when_done));
+ else
+ *command_when_done = 0;
+}
+
+/*
+FIXME
+Do not use yet. Not complete.
+Problem: what counts as an error?
+*/
+
+void Curl_CommandWhenError(const char *cmd)
+{
+ if(!curl_dll)
+ return;
+ if(cmd)
+ strlcpy(command_when_error, cmd, sizeof(command_when_error));
+ else
+ *command_when_error = 0;
+}
+
+/*
+====================
+Curl_Clear_forthismap
+
+Clears the "will disconnect on failure" flags.
+====================
+*/
+void Curl_Clear_forthismap(void)
+{
+ downloadinfo *di;
+ if(noclear)
+ return;
+ for(di = downloads; di; di = di->next)
+ di->forthismap = false;
+ Curl_CommandWhenError(NULL);
+ Curl_CommandWhenDone(NULL);
+ numdownloads_fail = 0;
+ numdownloads_success = 0;
+ numdownloads_added = 0;
+}
+
+/*
+====================
+Curl_Have_forthismap
+
+Returns true if a download needed for the current game is running.
+====================
+*/
+qboolean Curl_Have_forthismap(void)
+{
+ return numdownloads_added != 0;
+}
+
+void Curl_Register_predownload(void)
+{
+ Curl_CommandWhenDone("cl_begindownloads");
+ Curl_CommandWhenError("cl_begindownloads");
+}
+
+/*
+====================
+Curl_CheckCommandWhenDone
+
+Checks if a "done command" is to be executed.
+All downloads finished, at least one success since connect, no single failure
+-> execute the command.
+*/
+static void Curl_CheckCommandWhenDone(void)
+{
+ if(!curl_dll)
+ return;
+ if(numdownloads_added && (numdownloads_success == numdownloads_added) && *command_when_done)
+ {
+ Con_DPrintf("cURL downloads occurred, executing %s\n", command_when_done);
+ Cbuf_AddText("\n");
+ Cbuf_AddText(command_when_done);
+ Cbuf_AddText("\n");
+ Curl_Clear_forthismap();
+ }
+ else if(numdownloads_added && numdownloads_fail && *command_when_error)
+ {
+ Con_DPrintf("cURL downloads FAILED, executing %s\n", command_when_error);
+ Cbuf_AddText("\n");
+ Cbuf_AddText(command_when_error);
+ Cbuf_AddText("\n");
+ Curl_Clear_forthismap();
+ }
+}
+