+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()
+{
+ downloadinfo *di;
+ 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;
+}
+
+/* obsolete: numdownloads_added contains the same
+static qboolean Curl_Have_forthismap()
+{
+ downloadinfo *di;
+ for(di = downloads; di; di = di->next)
+ if(di->forthismap)
+ return true;
+ return false;
+}
+*/
+
+/*
+====================
+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()
+{
+ if(!curl_dll)
+ return;
+ if(numdownloads_added && (numdownloads_success == numdownloads_added) && *command_when_done)
+ {
+ Con_DPrintf("Map downloads occurred, executing %s\n", command_when_done);
+ Cbuf_AddText("\n");
+ Cbuf_AddText(command_when_done);
+ Cbuf_AddText("\n");
+ Curl_Clear_forthismap(NULL);
+ }
+ else if(numdownloads_added && numdownloads_fail && *command_when_error)
+ {
+ Con_DPrintf("Map downloads FAILED, executing %s\n", command_when_error);
+ Cbuf_AddText("\n");
+ Cbuf_AddText(command_when_error);
+ Cbuf_AddText("\n");
+ Curl_Clear_forthismap(NULL);
+ }
+}
+