#!/usr/bin/perl
# -*- Mode: cperl -*-
# Copyright (C) 2002 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 || '/etc/wine/wine.reg';
my $fstab = '/etc/fstab';
my $fattype = 'vfat|ntfs';
my $windir = '/var/lib/wine';

sub change_config {
    local *F, *O;
    my $gotc;
    my $output = `mktemp /tmp/.wine.XXXXXX`;chop($output);
    open O, ">$output"; select O;
    open F, $config;
    while (<F>) {
	$gotc++ if /\[Drive\s*C\]/;
	if ($gotc and /\"Path\"\s*=\s*\"([^\"]+)\"/) {
	    my $w = $1;
	    check_dir($w);
	    s|$w|$windir|; print; undef $gotc; next;
	}
	print;
    }
    close F;
    close O;
    system("cp -f $config $config.bak");
    system("mv $output $config");
}
sub parse_fstab {
    local *F;
    open F, $fstab;
    while (<F>) {
	@_ = split ' ';
	check_dir($_[1]) if $_[2] =~ /$fattype/
    }
    close F;
    return;
}
sub check_dir {
    my $dir = shift;
    $windir = $dir if ( -d "$dir/windows" || -d "$dir/WINDOWS" || -d "$dir/Windows" );
    return;
}

parse_fstab();
change_config();
