#!/bin/bash
# This script is written by Martynas Bendorius and DirectAdmin
# It is used to move user from one reseller to another
# Official DirectAdmin webpage: http://www.directadmin.com
# Usage:
# ./move_user_to_reseller.sh <user> <oldreseller> <newreseller>

if [ "$(/usr/bin/id -u)" != 0 ]; then
        echo "You require Root Access to run this script";
        exit 0;
fi

if [ $# != 3 ]; then
        echo "Usage:";
        echo "$0 <user> <oldreseller> <newreseller>";
        echo "you gave #$#: $0 $1 $2 $3";
        exit 0;
fi

PAYLOAD=$(printf '{"account": "%s", "creator": "%s"}' "$1" "$3")
OUTPUT=$(curl --insecure --silent --write-out '\n%{http_code}' --data "${PAYLOAD}" "$(/usr/local/directadmin/directadmin api-url)/api/change-user-creator")
CODE=$(echo "${OUTPUT}" | tail -n 1)

if [ "${CODE}" = "200" ] || [ "${CODE}" = "204" ]; then
        echo "User has been moved to $3"
        exit 0
else
        echo "${OUTPUT}" | sed '$d'
        exit 1
fi
