<?php
include '../functions.php';
if ($argc < 3) {
    die('Usage: ' . $argv[0] . ' <olduserid> <newuserid>' . "\n");
}

$oldUserId = $argv[1];
$newUserId = $argv[2];

print "Fasse das alte Konto von userId '$oldUserId' und userId '$newUserId' unter '$newUserId' zusammen.\n";

$queryold = "SELECT * FROM users WHERE userId = $oldUserId";
$querynew = "SELECT * FROM users WHERE userId = $newUserId";
$result = mysql_query($queryold) or die(mysql_error());
if (mysql_num_rows($result) < 1) {
    $queryold = "SELECT * FROM deleted_users WHERE userId = $oldUserId";
    $result = mysql_query($queryold) or die(mysql_error());
    if (mysql_num_rows($result) < 1) {
        die ("Kann oldUserId '$oldUserId' nicht finden.\n");
    }
}
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$oldUserId = $row['userId'];

$result = mysql_query($querynew) or die(mysql_error());
if (mysql_num_rows($result) < 1) {
  die ("Kann newUserId '$newUserId' nicht finden.\n");
}
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$newUserId = $row['userId'];

$logqueryuser = "UPDATE userlogs SET userId = $newUserId WHERE userId = $oldUserId";
$result = mysql_query($logqueryuser) or die(mysql_error());
if (!$result) {
  die ("Kann newUserId '$newUserId' nicht in uerslogs finden.\n");
}

$logqueryobject = "UPDATE userlogs SET object = $newUserId
WHERE object = $oldUserId";
$result = mysql_query($logqueryobject) or die(mysql_error());
if (! $result) {
  die ("Kann newUserId '$newUserId' nicht in uerslogs finden.\n");
}

$kontoquery = "UPDATE kontobewegungen SET userId = $newUserId
WHERE userId = $oldUserId";
$result = mysql_query($kontoquery) or die(mysql_error());
if (! $result) {
  die ("Kann newUserId '$newUserId' nicht in kontobewegungen finden.\n");
}


