Conbine 2 sketches into 1

Im building something the requires me to upload sketches via wifi. The system has multiple wireless modules and currently is using nrf24 cards and wifi cards. I want to make the communication wifi only so am thinking of designing a program for an esp to handle all of the communication. I came across a website that shows how to use esp as a serial bridge. Hal way down this page theres a program called WiFi-Serial ESP8266 and i would like to know if its possible to break the program up into multiple roles on startup. For example, after reset or power up start in role 1 to enter wifi serial bridge mode so i can flash an arduino over network. however if there is no incoming command after so many seconds then switch to role 2 and go into AP and ST mode and handle the communication for my modules? is this possible?

i thought i would post the program in another post to save readers time. This is the program i want to merge with my program. I dont currently have the program i want to merge this program with but was looking for advice

/**** This version is ported by Atadiat to work on ESP8266 (tested on NodeMCU 1.0) ****/
/**** Porting was done by Yahya Tawil****/

// ESP32 WiFi <-> 3x UART Bridge
// by AlphaLima

// Disclaimer: Don't use  for life support systems
// or any other situations where system failure may affect
// user or environmental safety.

#include <ESP8266WiFi.h>
#define MODE_AP // phone connects directly to ESP
//#define MODE_STA // ESP connects to WiFi router

//#define PROTOCOL_UDP
bool debug = true;

#define VERSION "1.10"
#ifdef MODE_AP
// For AP mode:
const char *ssid = "Serial_wifi";  // You will connect your phone to this Access Point
const char *pw = "123456789"; // and this is the password
IPAddress ip(192, 168, 4, 1); // From RoboRemo app, connect to this IP
IPAddress netmask(255, 255, 255, 0);

// You must connect the phone to this AP, then:
// menu -> connect -> Internet(TCP) ->  for UART0

#ifdef MODE_STA
// For STATION mode:
const char *ssid = "YourSSID";  // Your ROUTER SSID
const char *pw = "password"; // and WiFi PASSWORD

// You must connect the phone to the same router,
// Then somehow find the IP that the ESP got from router, then:
// menu -> connect -> Internet(TCP) -> [ESP_IP]:8880  for UART0

/*************************  COM Port 0 *******************************/
#define UART_BAUD0 19200            // Baudrate UART0
#define SERIAL_PARAM0 SERIAL_8N1    // Data/Parity/Stop UART0
#define SERIAL0_TCP_PORT 8880       // Wifi Port UART0
/*************************  COM Port 1 *******************************/
#define UART_BAUD1 19200            // Baudrate UART0
#define SERIAL_PARAM0 SERIAL_8N1    // Data/Parity/Stop UART0
#define SERIAL1_TCP_PORT 8881       // Wifi Port UART0

#define bufferSize 1024
#define NUM_COM 2
#define DEBUG_COM 0 // debug output to COM0

#include <WiFiClient.h>
WiFiServer server_0(SERIAL0_TCP_PORT);
WiFiServer server_1(SERIAL1_TCP_PORT);
WiFiServer *server[NUM_COM] = {&server_0, &server_1};

#include <WiFiUdp.h>
WiFiUDP udp;
IPAddress remoteIp;

HardwareSerial* COM[NUM_COM] = {&Serial, &Serial1};

uint8_t buf1[NUM_COM][bufferSize];
uint16_t i1[NUM_COM] = {0,0};

uint8_t buf2[bufferSize];
uint16_t i2[NUM_COM] = {0,0};

void setup() {
  if (debug) COM[DEBUG_COM]->println("\n\nLK8000 WiFi serial bridge V1.00");
#ifdef MODE_AP
  if (debug) COM[DEBUG_COM]->println("Open ESP Access Point mode");
  WiFi.softAPConfig(ip, ip, netmask); // configure ip address for softAP
  WiFi.softAP(ssid, pw); // configure ssid and password for softAP

#ifdef MODE_STA
  if (debug) COM[DEBUG_COM]->println("Open ESP Station mode");
  // STATION mode (ESP connects to router and gets an IP)
  // Assuming phone is also connected to that router
  // from RoboRemo you must connect to the IP of the ESP
  WiFi.begin(ssid, pw);
  if (debug) COM[DEBUG_COM]->println("try to Connect to Wireless network: ");
  if (debug) COM[DEBUG_COM]->println(ssid);
  while (WiFi.status() != WL_CONNECTED) {
    if (debug) COM[DEBUG_COM]->print(".");
  if (debug) COM[DEBUG_COM]->println("\nWiFi connected");


  Serial.println("Starting TCP Server 1");
  if (debug) COM[DEBUG_COM]->println("Starting TCP Server 1");
  server[0]->begin(); // start TCP server
  COM[1]->println("Starting TCP Server 2");
  if (debug) COM[DEBUG_COM]->println("Starting TCP Server 2");
  server[1]->begin(); // start TCP server

  if (debug) COM[DEBUG_COM]->println("Starting UDP Server 1");
  udp.begin(SERIAL0_TCP_PORT); // start UDP server

  if (debug) COM[DEBUG_COM]->println("Starting UDP Server 2");
  udp.begin(SERIAL1_TCP_PORT); // start UDP server

void loop()

  for (int num = 0; num < NUM_COM ; num++)
    if (server[num]->hasClient())
      for (byte i = 0; i < MAX_NMEA_CLIENTS; i++) {
        //find free/disconnected spot
        if (!TCPClient[num][i] || !TCPClient[num][i].connected()) {
          if (TCPClient[num][i]) TCPClient[num][i].stop();
          TCPClient[num][i] = server[num]->available();
          if (debug) COM[DEBUG_COM]->print("New client for COM");
          if (debug) COM[DEBUG_COM]->print(num);
          if (debug) COM[DEBUG_COM]->print('/');
          if (debug) COM[DEBUG_COM]->println(i);
      //no free/disconnected spot so reject
      WiFiClient TmpserverClient = server[num]->available();

  for (int num = 0; num < NUM_COM ; num++)
    if (COM[num] != NULL)
      for (byte cln = 0; cln < MAX_NMEA_CLIENTS; cln++)
        if (TCPClient[num][cln])
          while (TCPClient[num][cln].available())
            buf1[num][i1[num]] = TCPClient[num][cln].read(); // read char from client (LK8000 app)
            if (i1[num] < bufferSize - 1) i1[num]++;

          COM[num]->write(buf1[num], i1[num]); // now send to UART(num):
          i1[num] = 0;

      if (COM[num]->available())
        String myString[NUM_COM] ;
        while (COM[num]->available())
          myString[num] += (char); // read char from UART(num)
        // now send to WiFi:

        for(byte cln = 0; cln < MAX_NMEA_CLIENTS; cln++)
        i2[num] = 0;

There's no simple solution to merge two programs.
Just copy all the #defines, #includes and declarations to the top, copy the contents of the setup() and loop() functions, then click on the checkmark to compile the program. When you get a ton of errors, and you will, print them out and fix them one at a time.