#!/usr/bin/perl -w
# -*- Mode: cperl -*-
# Copyright (C) 2000 by Chmouel Boudjnah <chmouel@mandrakesoft.com>,
# MandrakeSoft
# Redistribution of this file is permitted under the terms of the GNU 
# Public License (GPL)
use strict;

my $config = shift or die "Need a config file in argument\n";
my $fstab = "/etc/fstab";
my (%partition, %mount);
my @fat_partitions;

local *F;
open F, $fstab or die "Can't open $fstab\n";

#let's got all information from /etc/fstab
while(<F>) {
    @_ = split ' ';

    #windows
    if ($_[2] =~ /vfat/) {
	if ( -d "$_[1]/windows" || -d "$_[1]/Windows" ) {
	    $partition{windows} = $_[0] unless defined($partition{windows});
	    $mount{windows}  = $_[1] unless defined($mount{windows});
	}
    }

    #floppy
    if ($_[0] =~ m|/dev/fd0|) {
	$partition{floppy} = $_[0];
	$mount{floppy} = $_[1];
    }

    #floppy supermount type
    if ($_[0] =~ m|/mnt/floppy|) {
	if ($_[3] =~ m|dev=([^, \t]+)|) {
	    $partition{floppy} = $1;
	    $mount{floppy} = $_[1];
	}
    }
    
    #cdrom
    if ($_[0] =~ m|/dev/cdrom|) {
	$partition{cdrom} = $_[0];
	$mount{cdrom} = $_[1];
    }

    #cdrom supermount type
    if ($_[0] =~ m|/mnt/cdrom|) {
	if ($_[3] =~ m|dev=([^, \t]+)|) {
	    $partition{cdrom} = $1;
	    $mount{cdrom} = $_[1];
	}
    }
    
}
close F;

$mount{windows} = "/var/lib/wine" unless defined($mount{windows});

$/ = undef;
my $output = `mktemp /tmp/.wine.XXXXXX`;chop($output);
local *O;
open O, ">$output"; select O;
open F, $config;
while (<F>) {
	$_ =~ s|(\[Drive A\].*?Path\" = \")[^"]+|${1}$mount{floppy}|s;
    	$_ =~ s|(\[Drive A\].*?Device\" = \")[^"]+|${1}$partition{floppy}|s;
    	$_ =~ s|(\[Drive C\].*?Path\" = \")[^"]+|${1}$mount{windows}|s;
    	$_ =~ s|(\[Drive D\].*?Path\" = \")[^"]+|${1}$mount{cdrom}|s;
    	$_ =~ s|(\[Drive D\].*?Device\" = \")[^"]+|${1}$partition{cdrom}|s;
    print;
}
close F;
close O;
system("mv -f $output $config");
