# -*-perl-*-

$force_config = 0;

sub xinstall {
    local ( $config_again );
    local ( $first_config );

    $config_again = 1;
    $first_config = 1;

    do {
	# get X configured
	if ($config_again) {
	    undef $xserver;

	    &mount_bootdisk();
	    if ($first_config && -f "/bootdisk/defaults/xsetup" ) {
		require "/bootdisk/defaults/xsetup";
	    }

	    if ($first_config && defined $xserver) {
		unlink("/etc/X11/X");
		symlink("/usr/X11R6/bin/XF86_$xserver", "/etc/X11/X");
		if (defined $mouseport) {
		    unlink("/dev/mouse");
		    symlink("$mouseport", "/dev/mouse");
		}
		
		open(IN, "</bootdisk/defaults/XF86Config");
		open(OUT, ">/etc/X11/XF86Config");
		while (<IN>) {
		    print OUT;
		}
		close IN;
		close OUT;
	    } else {
		$force_config = 1;
		$ret = &configure_mouse;
		$ret = &select_x_server;
		$force_config = 0;

		$first_config = 0;

		&invoke("/usr/rhs/install/Xconfigurator $xserver $mousetype");
	    }
	}

	&rhs_msgbox ( "X Install",
<<EOM
>
X is about to start.  If it succeeds you will have a few seconds
to press a button on the screen.  If you do not see the button,
X has most likely been misconfigured, and a timeout will occur,
bringing you back to text mode for further instructions.
>
This could take as long as two minutes on a slow network or using
a slow CDROM drive. Please be patient.
>
EOM
		     , 70);

	&invoke('/usr/X11/bin/startx');

	if (! (-f "/tmp/xworked")) {
	    &rhs_menu("X Error",
<<EOM
>
X seems to have failed to configure and/or run properly.
>
What do you want to do?
> 
EOM
		      , 70, 3,
		      "Retry", "Just try X again",
		      "Reconfigure", "Reconfigure X and try again",
		      "text", "Fall back to text based install");
	    if ($dialog_result eq "text") {
		return 0;
	    }
	    if ($dialog_result eq "Retry") {
		$config_again = 0;
	    } else {
		$config_again = 1;
	    }
	} else {
	    # X worked
	    open(FD, "/etc/X11/XF86Config");
	    $fsmount = "/mnt";
	    open(OFD, ">$fsmount/etc/X11/XF86Config");
	    while (<FD>) {
		print OFD $_;
	    }
	    close(FD);
	    close(OFD);

	    unlink("$fsmount/etc/X");
	    unlink("$fsmount/etc/X11/X");
	    symlink("../../usr/X11R6/bin/XF86_$xserver", "$fsmount/etc/X11/X");
	    unlink("$fsmount/usr/X11R6/bin/X");
	    symlink("../../../etc/X11/X", "$fsmount/usr/X11R6/bin/X");
	    
	    &finish_mouse;
	    &finish_pcmcia;
	    &finish_cd;
	}
    } while (! (-f "/tmp/xworked"));

    return 1;
}

1;
