#!/usr/local/bin/perl

open(LVDATA,"data/wiener_data.txt") or die "Can't read data/wiener_data.txt!";
@lvlines = <LVDATA>;
close(LVDATA);

if(-e "insert_psu.sql") {`rm insert_psu.sql`;}
if(-e "insert_psu_bin.sql") {`rm insert_psu_bin.sql`;}
open(PSU_DATA,"> insert_psu.sql") or die "Can't open psu_data.txt!";
open(BIN_DATA,"> insert_psu_bin.sql") or die "Can't open psu_bin_data.txt!";
print PSU_DATA "LOCK TABLE psu;\n";
print BIN_DATA "LOCK TABLE psu_bin;\n";
foreach $lvline (@lvlines){
  # extract the information:
  $lvline =~ /^alidcswie(.{3}),alitrdwie.{3},(.{7,15}),(.{7}),1TRDU2Y(.{3})-(.{2}),(.{11,20}),(.{8}),(.{7}),(.{7})$/;
  $host = $1; $ip = $2; $sernr = $3; $rack = $4; $height = $5; $mac = $6; $lhc = $7; $outlet = $8; $firmware = $9;
  $type = 0;
  if(strlen($lhc)>1){
    $str = substr($lhc,3,1);
    if($str=="2") {$type=1;}
    if($str=="3") {$type=2;}
    if($str=="0") {$type=3;}
    if($str=="4" || $str=="5" || $str=="7") {$type=4;}
  }
  print PSU_DATA "INSERT INTO psu VALUES ($sernr,\'$rack\',$height,\'$lhc\',$type,\'$mac\',\'$firmware\',NULL,\'PC\');\n";
  print BIN_DATA "INSERT INTO psu_bin VALUES (\'$rack\',$height,$type,\'$ip\',\'$outlet\',\'PC\');\n";
}
close(PSU_DATA);
close(BIN_DATA);
