#!/usr/bin/env perl

use strict;
use warnings;

use FindBin::libs;

use Trog::Config();
use Getopt::Long qw{GetOptionsFromArray};

my $user;
GetOptionsFromArray(\@ARGV,
    'user' => \$user,
);

my $hostname = $ARGV[0];

my $conf = Trog::Config->get();
if ($hostname) {
    $conf->param('general.hostname', $hostname);

    # write(), not save().  save() writes back to whatever file get() loaded,
    # which is the tracked config/default.cfg until the instance has saved a
    # configuration of its own.
    $conf->write($Trog::Config::home_cfg) or die "Could not write $Trog::Config::home_cfg";
}

my $domain = $conf->param('general.hostname');
die "Hostname not set in tCMS configuration.  Please set this first by passing the hostname to bin/tcms-hostname." unless $domain;

# Transform the domain name into something maximally compatible with being a username.
if ($user) {
    $domain = substr($domain, 0, 32);
    $domain =~ s/\./-/g;
}

print "$domain\n";
