#!/s/std/bin/perl

$xfcfg = "/usr/adm/xdm/xconfig/XF86Config-4";
$newxfcfg = "/usr/adm/xdm/xconfig/XF86Config-4.new";

if (! -f $xfcfg) {
  # Not a linux box with XF86Config file...
  exit 0
}

open(XF86CFG, $xfcfg) || die "Can't open $xfcfg for read\n";
@xfcfg = <XF86CFG>;
close(XF86CFG);

open(NEWXF86CFG, ">$newxfcfg") || die "Can't open $newxfcfg for write\n";

if (grep(/:unscaled/, @xfcfg) || grep (/unix:7100/, @xfcfg) ) {
  $need_unscaled = 0;
} else {
  $need_unscaled = 1;
}

if (!grep(/TrueType-MS/, @xfcfg) && !grep(/unix:7100/, @xfcfg)) {
  push (@storepath, "    FontPath \"/usr/csl/lib/X11/fonts/TrueType-MS/\"\n");
}

if (!grep(/freefont/, @xfcfg) && !grep(/unix:7100/, @xfcfg)) {
  push (@storepath, "    FontPath \"/usr/csl/lib/X11/fonts/freefont/\"\n");
}

if ( !grep(/100dpi\/\"/, @xfcfg) && !grep(/unix:7100/, @xfcfg)) {
  push (@storepath, "    FontPath \"/usr/csl/lib/X11/fonts/100dpi/\"\n");
}

if ( !grep(/75dpi\/\"/, @xfcfg) && !grep(/unix:7100/, @xfcfg)) {
  push (@storepath, "    FontPath \"/usr/csl/lib/X11/fonts/75dpi/\"\n");
}

foreach (@xfcfg) {
  if (/^\s*FontPath\s/) {
    $fontpath = 1;
  } else {
    $fontpath = 0;
  }

  if ($fontpath && $need_unscaled && /[0-9]*dpi/) {
    push (@storepath, $_);
    s/([0-9]*dpi\/)/$1:unscaled/;
  }
  if ($lastfontpath && !$fontpath && @storepath) {
    # time to add in new additions
    foreach $path (@storepath) {
      print NEWXF86CFG $path;
    }
  }
  print NEWXF86CFG $_;
  $lastfontpath = $fontpath;
}

rename($xfcfg, "$xfcfg.old");
rename($newxfcfg, $xfcfg);
