#!/usr/bin/perl
#  $Id: gen_pc_id.pl 33131 2015-10-02 15:44:56Z astarostin $
# -----------------------------------------------------------------------------
#  Generate hradware-descriptive ID file, encode it
# -----------------------------------------------------------------------------
#  Author: Andrey Fomenko
#  Edited by:
#  QA by:  Christopher C Gettings
#  Copyright: videoNEXT LLC
# -----------------------------------------------------------------------------

use strict;
use warnings;
use SKM::DB;

my $idfile = '/opt/sarch/var/license/pcid.bin';

my $id;
my $serial = $ARGV[0] ? $ARGV[0] : "";
my $cpuinfo;
my $meminfo;
my $ifconfig;

if ($serial eq "")
{
    my $dbm = DBMaster;
    my $row = $dbm->selectrow_arrayref("select val from _obj_attr where attr='SERIAL_NO' and obj=53");
    $serial = $row->[0];
}

if ( $^O =~ /darwin/i) {
    $id = `/sbin/ifconfig -a|grep -A3 en0|grep ether|cut -c 8-24`;
    $cpuinfo = `/usr/sbin/system_profiler SPHardwareDataType|grep -Ev "Memory:|Hardware.*:|ROM Ver|Serial"|grep -E "[a-zA-z].*"`;
    $meminfo = `/usr/sbin/system_profiler SPMemoryDataType|grep -Ev "Memory:|Hardware.*:|ROM Ver|Serial"|grep -E "[a-zA-z].*"`;
}
else {
    $id = `/sbin/ip addr show eth0 | grep ether | cut -c 16-32`; # CentOS7 fix !
    $cpuinfo = `cat /proc/cpuinfo`;
    $meminfo = `cat /proc/meminfo`;
}

$ifconfig = `/sbin/ifconfig -a`;
$serial=~s/\s//g;

my $text = "\"SIGNATURE=NEXTCAM#MAC=$id#SERIAL=$serial#CPUINFO=$cpuinfo#MEMINFO=$meminfo#IFCONFIG=$ifconfig\"";

system("/opt/sarch/common/bin/make $text $idfile");
system("/opt/sarch/common/bin/make $text $idfile");
system('chown -f $APL_USR:$APL_HTTPD_GRP ' . $idfile);
system('chmod -f 660 ' . $idfile);

print "ID generated to $idfile\n";

# ----------===== Changes log =====----------
# $Log$
# Revision 1.8  2005/05/26 18:19:54  afomenko
# now it works with C version of crypt
#
# Revision 1.7  2005/05/24 22:13:36  afomenko
# fixed bug with not detecting eth0 interface if it's down
#
# Revision 1.6  2004/06/10 19:29:27  afomenko
# CR/LF -> LF
#
# Revision 1.5  2004/06/10 15:55:53  afomenko
# Added header/trailer with copyright, id, log
#
