// refcount command v0.5
// Submitted by Wonder
// Syntax: refcount increment <var> [silent]
//         refcount decrement <var> [silent]
//         refcount count [return-var] <var>
block load
{
  es_xsetinfo _tempcore 0
  es_xexists _tempcore command refcount
  es es_xif (server_var(_tempcore) = 0) do 
  {
    es_xregcmd refcount corelib/refcount "Increase or decrease the reference count for a variable"
    keygroupremove _refc_refs
  }
  es_xsetinfo _refc_ac 0
  es_xsetinfo _refc_a1 0
  es_xsetinfo _refc_a2 0
  es_xsetinfo _refc_curcount 0
  es_xsetinfo _refc_a3 0
  es_xsetinfo _refc_old 0
}
block unload
{
  es_xkeygroupdelete _refc_refs
}
block refcount
{
  es_xgetargc _refc_ac
  es es_xif (server_var(_refc_ac) > 1) do
  { 
    es_xgetargv _refc_a1 1
    es es_xif (server_var(_refc_a1) = increment) do
    { 
      es_xgetargv _refc_a2 2
      es es_xif (server_var(_refc_ac) > 2) do
      {
        es_xsetinfo _tempcore 0
        es es_xexists _tempcore variable server_var(_refc_a2)
        es es_xif (server_var(_tempcore) = 1) do
        {
          es_xsetinfo _tempcore 0
          es es_xexists _tempcore key _refc_refs server_var(_refc_a2)
          es es_xif (server_var(_tempcore) = 1) do
          {
            es es_xkeygetvalue _refc_curcount _refc_refs server_var(_refc_a2) refcount
            es_xmath _refc_curcount + 1
            es es_xkeysetvalue _refc_refs server_var(_refc_a2) refcount server_var(_refc_curcount)
          }
          else do
          {
            es es_xkeycreate _refc_refs server_var(_refc_a2)
            es es_xkeysetvalue _refc_refs server_var(_refc_a2) refcount 1
            es es_xkeysetvalue _refc_refs server_var(_refc_a2) oldvalue server_var(server_var(_refc_a2))
            es_xsetinfo _refc_curcount 1
          }
          es es_xif (server_var(_refc_ac) > 3) do
          {
            es_xgetargv _refc_a3 3
            es es_xif (server_var(_refc_a3) = 1) do
            {
              es es_xforcevalue server_var(_refc_a2) 1
            }
            else do
            {
              es server_var(_refc_a2) 1
            }
          }
          else do
          {
            es server_var(_refc_a2) 1
          }
          es es_xdbgmsg 1 [refcount] The ref. count for server_var(_refc_a2) is now server_var(_refc_curcount)!
        }
        else do
        {
          es es_xdbgmsg 0 [refcount] The CVAR server_var(_refc_a2) does not exist!
        }
      }
      else do
      {
        es_xdbgmsg 0 [refcount] You must provide a return CVAR!
      }
    }
    else do
    {
      es es_xif (server_var(_refc_a1) = decrement) do
      {
        es_xgetargv _refc_a2 2
        es es_xif (server_var(_refc_ac) > 2) do
        {
          es_xsetinfo _tempcore 0
          es es_xexists _tempcore variable server_var(_refc_a2)
          es es_xif (server_var(_tempcore) = 1) do
          {
            es_xsetinfo _tempcore 0
            es es_xexists _tempcore key _refc_refs server_var(_refc_a2)
            es es_xif (server_var(_tempcore) = 1) do
            {
              es es_xkeygetvalue _refc_curcount _refc_refs server_var(_refc_a2) refcount
              es_xmath _refc_curcount - 1
              es es_xkeysetvalue _refc_refs server_var(_refc_a2) refcount server_var(_refc_curcount)
              es es_xif (server_var(_refc_curcount) <= 0) do
              {
                es_xsetinfo _refc_curcount 0
                es es_xkeysetvalue _refc_refs server_var(_refc_a2) refcount server_var(_refc_curcount)
                es es_xkeygetvalue _refc_old _refc_refs server_var(_refc_a2) oldvalue
                es es_xif (server_var(_refc_ac) > 3) do
                {
                  es_xgetargv _refc_a3 3
                  es es_xif (server_var(_refc_a3) = 1) do
                  {
                    es es_xforcevalue server_var(_refc_a2) server_var(_refc_old)
                  }
                  else do
                  {
                    es server_var(_refc_a2) server_var(_refc_old)
                  }
                }
                else do
                {
                  es server_var(_refc_a2) server_var(_refc_old)
                }
              }
              else do
              {
                es es_xif (server_var(_refc_ac) > 3) do
                {
                  es_xgetargv _refc_a3 3
                  es es_xif (server_var(_refc_a3) = 1) do
                  {
                    es es_xforcevalue server_var(_refc_a2) 1
                  }
                  else do
                  {
                    es server_var(_refc_a2) 1
                  }
                }
                else do
                {
                  es server_var(_refc_a2) 1
                }
              }
              es es_xdbgmsg 1 [refcount] The ref. count for server_var(_refc_a2) is now server_var(_refc_curcount)!
            }
            else do
            {
              es es_xdbgmsg 0 [refcount] There is no ref. count for server_var(_refc_a2)!
            }
          }
          else do
          {
            es es_xdbgmsg 0 [refcount] The CVAR server_var(_refc_a2) does not exist!
          }
        }
        else do
        {
          es_xdbgmsg 0 [refcount] You must provide a return CVAR!
        }
      }
      else do
      {
        es es_xif (server_var(_refc_a1) = count) do
        {
          es es_xif (server_var(_refc_ac) > 2) do
          {
            es es_xif (server_var(_refc_ac) > 3) do
            {
              es_xgetargv _refc_a2 2
              es_xsetinfo _tempcore 0
              es es_xexists _tempcore variable server_var(_refc_a2)
              es es_xif (server_var(_tempcore) = 1) do
              {
                es_xgetargv _refc_a3 3
                es_xsetinfo _tempcore 0
                es es_xexists _tempcore variable server_var(_refc_a3)
                es es_xif (server_var(_tempcore) = 1) do
                {
                  es_xsetinfo _tempcore 0
                  es es_xexists _tempcore key _refc_refs server_var(_refc_a3)
                  es es_xif (server_var(_tempcore) = 1) do
                  {
                    es es_xkeygetvalue _refc_curcount _refc_refs server_var(_refc_a3) refcount
                    es server_var(_refc_a2) server_var(_refc_curcount)
                    es es_xdbgmsg 0 [refcount] The ref. count for server_var(_refc_a3) is server_var(_refc_curcount)!
                  }
                  else do
                  {
                    es server_var(_refc_a2) server_var(_refc_curcount)
                    es es_xdbgmsg 0 [refcount] There is no ref. count for server_var(_refc_a2)!
                  }
                }
                else do
                {
                  es es_xdbgmsg 0 [refcount] The CVAR server_var(_refc_a2) does not exist!
                }
              }
              else do
              {
                es es_xdbgmsg 0 [refcount] The return CVAR server_var(_refc_a2) does not exist!
              }
            }
            else do
            {
              es_xgetargv _refc_a2 2
              es_xsetinfo _tempcore 0
              es es_xexists _tempcore variable server_var(_refc_a2)
              es es_xif (server_var(_tempcore) = 1) do
              {
                es_xsetinfo _tempcore 0
                es es_xexists _tempcore key _refc_refs server_var(_refc_a2)
                es es_xif (server_var(_tempcore) = 1) do
                {
                  es es_xkeygetvalue _refc_curcount _refc_refs server_var(_refc_a2) refcount
                  es es_xdbgmsg 0 [refcount] The ref. count for server_var(_refc_a2) is server_var(_refc_curcount)!
                }
                else do
                {
                  es es_xdbgmsg 0 [refcount] There is no ref. count for server_var(_refc_a2)!
                }
              }
              else do
              {
                es es_xdbgmsg 0 [refcount] The CVAR server_var(_refc_a2) does not exist!
              }
            }
          }
          else do
          {
            es_xdbgmsg 0 [refcount] You must provide a CVAR!
          }
        }
        else do
        {
          es es_xdbgmsg 0 [refcount] server_var(_refc_a1) is an invalid command!
        }
      }
    }
  }
  else do
  {
    es_xdbgmsg 0 [refcount] You must provide a command!
  }
}