#!/usr/bin/perl
#***************************************************************************
#                               kimport
#                          -------------------
#    begin                : 4th October 2000
#    copyright            : (C) 2000 by The KDevelop Team
#    email                : kdevelop-team@fara.cs.uni-potsdam.de
# ***************************************************************************

#***************************************************************************
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU General Public License as published by  *
# *   the Free Software Foundation; either version 2 of the License, or     *
# *   (at your option) any later version.                                   *
# *                                                                         *
# ***************************************************************************

use Cwd;
use File::Find;
use File::Basename;
use Sys::Hostname;

sub processDir($);
sub addSource($);
sub addHeader($);
sub addData($);
sub addGeneral();

$version            = "v0.2";
$debug              = 0;
$thisProg           = "$0";     # This programs name
$topdir             = "";
$outfile            = "/dev/stdout";
$binfile            = "";
$binreldir          = "";
@all_Makefiles      = ();
$add_kidl_group     = 0;
$add_desktop_group  = 0;


while (defined ($ARGV[0]))
{
    $_ = shift;
    if (/^-v$|^--version$/)
    {
        print STDOUT "\n";
        print STDOUT basename($thisProg), " $version\n",
                "John Birch <jbb\@kdevelop.org>\n",
                "  Based on shell code by Ulrich Eckhardt(doomster\@knuut.de)\n\n";
        exit 0;
    }
    elsif (/^-d$|^--debug$/)
    {
        $debug = 1;
    }
    elsif (/^-b=(.+)$|^--binfile=(.+)$/)
    {
        $binfile = $1 if ($1);
        $binfile = $2 if ($2);
        print STDERR "Binfile is $binfile\n" if ($debug);
    }
    elsif (/^-o=(.+)$|^--output=(.+)$/)
    {
        $outfile = $1 if ($1);
        $outfile = $2 if ($2);
        print STDERR "Creating project file $outfile\n" if ($debug);
    }
    elsif (/^-h$|^--help$/)
    {
        print STDOUT "Usage $thisProg [OPTION] ... \n",
                "\n",
                "Create a kdevelop project file\n",
                "\n",
                "  -o=file, --output=file   output to project file\n",
                "  -b=file, --binfile=file  binary filename of project\n",
                "  -d, --debug              debug output\n",
                "  -h, --help               print this help, then exit\n",
                "  -v,--version             print version number, then exit\n";
	
        exit 0;
    }
    else
    {
        # The user can specify the starting directory.
        # (the default is the current directory)
        $topdir = $_;
        chdir($topdir) or die ("Invalid directory entered\n");
    }
}

die ("Must enter an output filename\n")    if (!$outfile);

$topdir       = cwd();
$binfile      = basename($topdir)    if (!$binfile);

find (\&buildDirList, ('.'));

open (FILEOUT, "> $outfile") or die ("Could not open $outfile: $!\n");
print FILEOUT "# KDE Config File\n";

foreach $file (@dirs)
{
  processDir($file);
}

addGeneral();

close FILEOUT;

0;

#-----------------------------------------------------------------------------

sub buildDirList
{
  # add restrictions here for which dirs to process
  return if (!-d);
  return if ($File::Find::name =~ /CVS$|.deps$|.libs$/);

  push (@dirs, $File::Find::name);
}

#-----------------------------------------------------------------------------

sub processDir($)
{
    my ($reldir)=@_;
    my @subdirs=();
    my @files=();
    my $file="";
    my $type="normal";
    my $subdirs="";

    $reldir =~ s/^\.\///;
    $reldir = "$reldir/"    if ($reldir);
    $type="po" if ($reldir =~ /^po\/$/);
    print "$reldir\n";

    opendir (DIR, $reldir) or die ("Can't open dir <$reldir>: $!");

    push @all_Makefiles, $reldir;

    while ( defined ($file = readdir DIR))
    {
        # restrict which files are added to the project file.
        # Note: this restricts the subdir line as well.
        next if ($file =~ /^CVS$|Makefile|^\.|\.lo$|\.closure$|\.o$|\.a$|\.la$|\.P$|\.moc$|\.moc\.(.*)$|meta_unload.cpp$/);

        if (-d "$reldir$file")
        {
            push @subdirs, $file;
            print STDERR  "<$file> is a dir\n"    if  ($debug);
            next;
        }

        print STDERR  "<$file>\n"    if ($debug);
        $test = quotemeta($binfile);
        if ($file =~ /^$test$/)
        {
          if ($reldir)
          {
            $binreldir=$reldir;
          }
          else
          {
            $binreldir="./";
          }
          $type="prog_main";
          next;
        }

        $add_kidl_group     = 1   if ($file =~ /\.kidl$/); 
        $add_desktop_group  = 1   if ($file =~ /\.desktop$/); 

        push @files, $file;
        addSource("$reldir$file"), next
            if ($file =~ /\.cpp$|\.cc$|\.cxx$|\.inl$|\.F$|\.ftn$|\.f77$|\.C$|\.c$|\.ui$|\.CPP$|\.CC$|\.CXX\+\+$/);
        addHeader("$reldir$file"), next
            if ($file =~ /\.h$|\.tlh$|\.H$|\.hh$|\.hxx$|\.h$|\.hpp$|\.HPP$|\.HH$|\.HXX\+\+$/);
        addData("$reldir$file"), next;
    }

    close DIR;

    print FILEOUT "[",$reldir,"Makefile.am]\n";
    print FILEOUT "files=$reldir",join(",$reldir",@files),",\n"   if (@files);
    print FILEOUT "type=$type\n";
    $subdirs = join(",",@subdirs).","       if (@subdirs);
    print FILEOUT "sub_dirs=$subdirs\n";

    @root_subdirs = @subdirs    if (!$reldir);
#    if (@subdirs)
#    {
#        foreach $subdir (@subdirs)
#        {
#            chdir($subdir) or die ("Failed to change dir to $subdir:$!\n");
#            processDir();
#            chdir("..");
#        }
#    }
    return 0;
}

#-----------------------------------------------------------------------------

sub addSource($)
{
    my ($file) = @_;
    print FILEOUT "[$file]\n";
    print FILEOUT "install_location=\n";
    print FILEOUT "dist=true\n";
    print FILEOUT "install=false\n";
    print FILEOUT "type=SOURCE\n";
} 

#-----------------------------------------------------------------------------

sub addHeader($)
{
    my ($file) = @_;
    print FILEOUT "[$file]\n";
    print FILEOUT "install_location=\n";
    print FILEOUT "dist=true\n";
    print FILEOUT "install=false\n";
    print FILEOUT "type=HEADER\n";
}

#-----------------------------------------------------------------------------

sub addData($)
{
    my ($file) = @_;
    print FILEOUT "[$file]\n";
    print FILEOUT "install_location=\n";
    print FILEOUT "dist=true\n";
    print FILEOUT "install=false\n";
    print FILEOUT "type=DATA\n";
}

#-----------------------------------------------------------------------------

sub addGeneral()
{
    my $groups = "Headers,Sources,GNU,Translations,User Interface";
    $groups .= ",Desktop"   if ($add_desktop_group);
    $groups .= ",KIDL"      if ($add_kidl_group);

    print FILEOUT "[General]\n";
    print FILEOUT "makefiles=",join("Makefile.am,",@all_Makefiles),"Makefile.am,\n";
    print FILEOUT "version_control=CVS\n";
    print FILEOUT "project_type=normal_empty\n";
    print FILEOUT "author=",$ENV{'USER'},"\n";
    # print FILEOUT "sub_dir=",join(",",@root_subdirs),",\n";
    print FILEOUT "sub_dir=$binreldir\n";
    print FILEOUT "lfv_open_groups=\n";
    print FILEOUT "workspace=1\n";
    print FILEOUT "project_name=",basename($topdir),"\n";
    print FILEOUT "version=\n";
    print FILEOUT "email=",$ENV{'USER'},"@",hostname(),"\n";
    print FILEOUT "kdevprj_version=1.3\n";
 
    print FILEOUT "[Config for BinMakefileAm]\n";
    print FILEOUT "ldflags=\n";
    print FILEOUT "addcxxflags=\n";
    print FILEOUT "ldadd=\n";
    print FILEOUT "bin_program=$binfile\n";
    print FILEOUT "cxxflags=\n";

    print FILEOUT "[LFV Groups]\n";
    print FILEOUT "GNU=AUTHORS,COPYING,ChangeLog,INSTALL,README,TODO,NEWS,\n";
    print FILEOUT "Others=*,\n";
    print FILEOUT "groups=$groups,Others\n";
    print FILEOUT "Sources=*.cpp,*.c,*.cc,*.C,*.cxx,*.ec,*.ecpp,*.lxx,*.l++,*.ll,*.l,\n";
    print FILEOUT "Headers=*.h,*.hxx,*.hpp,*.H,\n";
    print FILEOUT "Translations=*.ts,*.po,\n";
    print FILEOUT "User Interface=*.ui,*.kdevdlg,*.rc,\n";
    print FILEOUT "KIDL=*.kidl,\n"          if ($add_kidl_group);
    print FILEOUT "Desktop=*.desktop,\n"    if ($add_desktop_group);
}

#-----------------------------------------------------------------------------

