#!/usr/bin/perl
#--------------------
# PSMT 2.3.13
# 2013.03.26 update
# 2016.12.05 AIXのcronに対応
# 2016.12.08 cron設定の出力を/dev/nullにリダイレクト
# 2023.05.22 wgetコマンドパスをコンフィグで設定可能に
# 2024.12.15 ログ出力をするように修正
#-------------------
# setup
#-------------------
$manager = $ARGV[0];
$type = $ARGV[1];

# 初期パラメータ設定
open(CONFIG,"/opt/proscan/agent/conf/psmt_agent.conf");
while(<CONFIG>) {
    chomp;
    next if /^#/ || /^$/;
    ($name,$value) = split(/=/,$_);
    eval("\$$name = $value");
}
close(CONFIG);

$config_file = "/opt/proscan/agent/download/scan.conf";
$key_file = "/opt/proscan/agent/download/filescan.key";
$group_file = "/opt/proscan/agent/download/group";
$schedule_file = "/opt/proscan/agent/download/schedule";
$get_manager_pass = "/opt/proscan/agent/bin/agent -p";

# logfile設定
$log_file = "/var/opt/proscan/log/psmt_agent_script.log";
$log_file = $LOGFILE if $LOGFILE;

open(LOG, ">>$log_file");
print LOG `date`;
print LOG "TYPE=$type\n";

if ( $type eq 'config' ) {
    $file = $config_file;
    $fname = "scan.conf";
} elsif ( $type eq "group" ) {
    $file = $group_file;
    $fname = "group";
} elsif ( $type eq "license" ) {
    $file = $key_file;
    $arch = `arch`;
    $arch = `uname` unless $arch;
    if ( $arch =~ /i.86/ ) {
        $arch_type = "i386";
    } elsif ( $arch =~ /s390/ ) {
        $arch_type = "s390";
    } elsif ( $arch =~ /ppc/ ) {
        $arch_type = "ppc";
    } elsif ( $arch =~ /x86/ ) {
        $arch_type = "x86";
    } elsif ( $arch =~ /AIX/ ) {
        $arch_type = "aix";
    } else {
        $arch_type = "i386";
    }
    $fname = "proscan_${arch_type}.key";
} elsif ( $type eq "schedule" ) {
    $file = $schedule_file;
    $fname = "schedule";
} else {
    print LOG "[error] OS type error($arch)\n";
    close(LOG);
    exit(1);
}

print LOG "FILE=$file, NAME=$fname\n";

# wgetコマンドのパス設定
$wget = "/usr/bin/wget";
$wget = $WGET if $WGET;
$wget_opt = "-q -O $file";

# 管理サーバのアドレス設定
$manager = "$MANAGER" unless $manager;
$port = "$PORT";

# 管理サーバのダウンロードCGI名
$cgi = "download.cgi";
$cgi_param = "file=$fname";
$url = "http://$manager:$port/cgi-bin/$cgi";

# 対象マネージャのパスワード取得
$pass = `$get_manager_pass $manager`;
$post_data = "--post-data='pass=$pass&$cgi_param'";

umask(077);

print LOG qq($wget $wget_opt $post_data "$url");
print LOG `$wget $wget_opt $post_data '$url'`;

if ( -z $file || ! -f $file ) {
    print LOG "[error] $file download error\n";
    close(LOG);
    exit(1);
}

$rc = 0;

if ( $type eq 'config' ) {
    $scan_conf = "/opt/proscan/agent/conf/scan.conf";
    unlink($scan_conf);
    link($config_file,$scan_conf);
    print LOG "config setup $scan_conf\n";
} elsif ( $type eq 'group' ) {
    $group = "/opt/proscan/agent/data/group";
    unlink($group);
    link($group_file,$group);
    print LOG "group setup $group\n";
} elsif ( $type eq 'license' ) {
    $license_key = "/var/opt/proscan/db/keys/filescan.key";
    $regist_key = "/var/opt/proscan/db/keys/regist.code";
    unlink($license_key);
    unlink($regist_key);
    #link($key_file,$license_key);
    system("cp $key_file $license_key");
    $str = `/opt/proscan/bin/licenseviewer -k $license_key|awk -F= '/PSHB/ {print $2}'`;
    ($regcode) = $str =~ /(PSHB..-\d+-\d+-\d+-\d+)/;
    open(REG,">$regist_key");
    print REG "$regcode\n";
    close(REG);
    print LOG "license setup $license_key\n";
    $rc = system("/opt/proscan/bin/licenseviewer -s");
    print LOG "[error] setup error\n" if $rc != 0;
    system("/opt/proscan/agent/script/postversion $manager");
} elsif ( $type eq 'schedule' ) {
    open(SC,$file);
    $upd_schedule = <SC>;
    chomp($upd_schedule);
    $scan_schedule = <SC>;
    chomp($scan_schedule);
    $scan_param = <SC>;
    chomp($scan_param);
    close(SC);
    # setup cron
    if ( -f "/etc/cron.d/psmt_agent" ) {
	unlink("/etc/cron.d/psmt_agent");
	print LOG "cron setup /etc/cron.d/psmt_agent\n";
	open(CRON,">/etc/cron.d/psmt_agent");
	if ( $upd_schedule ) {
	    print CRON "$upd_schedule /opt/proscan/agent/script/update $manager > /dev/null 2>&1\n";
	    print LOG "$upd_schedule /opt/proscan/agent/script/update $manager > /dev/null 2>&1\n";
	} else {
	    print CRON "\n";
	    print LOG "\n";
	}
	if ( $scan_schedule ) {
	    $scan_param = "/tmp" unless $scan_param;
	    print CRON qq($scan_schedule /opt/proscan/agent/script/scan $manager "$scan_param" $SCAN_TIMEOUT > /dev/null 2>&1\n);
	    print LOG qq($scan_schedule /opt/proscan/agent/script/scan $manager "$scan_param" $SCAN_TIMEOUT > /dev/null 2>&1\n);
	}
	close(CRON);
    } elsif ( -f "/var/spool/cron/crontabs/root" || -f "/var/spool/cron/root" ) {
	if ( -f "/var/spool/cron/crontabs/root" ) {
	    $CRONTAB = "/var/spool/cron/crontabs/root";
	}
	if ( -f "/var/spool/cron/root" ) {
	    $CRONTAB = "/var/spool/cron/root";
	}
	print LOG "cron setup $CRONTAB\n";
	open(TEMP,">$CRONTAB.tmp");
	open(CRON,"$CRONTAB");
	while(<CRON>) {
	    chomp;
	    next if /\/scan |\/update /;
	    print TEMP "$_\n";
	    print LOG "$_\n";
 	}
	if ( $upd_schedule ) {
	    print TEMP "$upd_schedule /opt/proscan/agent/script/update $manager > /dev/null 2>&1\n";
	    print LOG "$upd_schedule /opt/proscan/agent/script/update $manager > /dev/null 2>&1\n";
	}
	if ( $scan_schedule ) {
	    $scan_param = "/tmp" unless $scan_param;
	    print TEMP qq($scan_schedule /opt/proscan/agent/script/scan $manager "$scan_param" $SCAN_TIMEOUT > /dev/null 2>&1\n);
	    print LOG qq($scan_schedule /opt/proscan/agent/script/scan $manager "$scan_param" $SCAN_TIMEOUT > /dev/null 2>&1\n);
	}
	close(TEMP);
	close(CRON);
	unlink("$CRONTAB");
	rename("$CRONTAB.tmp","$CRONTAB");
	$rc = system("crontab $CRONTAB");
    } elsif ( -d "/var/spool/cron/crontabs" ) {
	print LOG "cron setup /var/spool/cron/crontabs/root\n";
	open(CRON,">/var/spool/cron/crontabs/root");
	if ( $upd_schedule ) {
	    print CRON "$upd_schedule /opt/proscan/agent/script/update $manager > /dev/null 2>&1\n";
	    print LOG "$upd_schedule /opt/proscan/agent/script/update $manager > /dev/null 2>&1\n";
	}
	if ( $scan_schedule ) {
	    $scan_param = "/tmp" unless $scan_param;
	    print CRON qq($scan_schedule /opt/proscan/agent/script/scan $manager "$scan_param" $SCAN_TIMEOUT > /dev/null 2>&1\n);
	    print LOG qq($scan_schedule /opt/proscan/agent/script/scan $manager "$scan_param" $SCAN_TIMEOUT > /dev/null 2>&1\n);
	}
	close(CRON);
	$rc = system("crontab /var/spool/cron/crontabs/root");
    }
    print LOG "[error] setup error\n" if $rc != 0;
}

print LOG "setup complete\n" if $rc == 0;
close(LOG);

exit($rc);
