require ("flush.pl");
use File::Copy;

$templatedir = "/usr/local/share/apps/kdevelop/templates";
$kde_icondir = "/usr/local/share/icons";

my %changings;

my $homedirectory = $ENV{HOME};
my $local_icondir = $homedirectory . "/.kde/share/icons/";
my $local_datadir = $homedirectory . "/.kde/share/apps/";
my $kde_miniicondir = $kde_icondir . "/mini";
my $local_miniicondir = $local_icondir ."/mini";

printflush (STDOUT,"Starting with installation\n");

#open file "entries" for reading the parameters from kAppWizard and put it in a hash
open (PROCESSLIST,$homedirectory . "/.kde/share/apps/kdevelop/entries") 
  || die "can not open file \"entries\" in your local app-data-dir:\n  $!";
while ( defined ($name = <PROCESSLIST> )) {
  chomp ($name);
  $process = <PROCESSLIST>;
  chomp ($process);
  $processes{$name} = $process;
}
close (PROCESSLIST);

$nameLittle = $processes{NAME};
$nameLittle =~ tr/A-Z/a-z/;
$nameBig = $processes{NAME};
$nameBig =~ tr/a-z/A-Z/;
$name =  $processes{NAME};

if ($processes{VSSUPPORT} eq "none") {
	$overDirectory = $processes{DIRECTORY} . "/" . $processes{PRJVSLOCATION} . "/";
	$underDirectory = $overDirectory . $nameLittle;
}
else {
	$overDirectory = $homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/";
	$underDirectory = $overDirectory . $nameLittle;
}
	
	
#create the projectdirectory 
printflush (STDOUT, "change directory...\n");
chdir ($overDirectory);
$date = `date`;
chomp($date);
@time = localtime();
$year = 1900 + $time[5];

setupDefaultChangings();

printflush (STDOUT,"change directory...\n");
chdir ($overDirectory);

if ($processes{APPLICATION} eq "kdenormal") {
  changeNormalApp();  
}
elsif ($processes{APPLICATION} eq "kdemini") {
  changeMiniApp();
}
elsif ($processes{APPLICATION} eq "cpp") {
  changeTerminalCPPApp();
}
elsif ($processes{APPLICATION} eq "c") {
  changeTerminalCApp();
}
elsif ($processes{APPLICATION} eq "qtnormal") {
  changeQtApp();
}
elsif ($processes{APPLICATION} eq "customproj") {
  changeCustomApp();
}

if ($processes{VSSUPPORT} eq "cvs") {
	chdir ($homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/");
  $copysrc = $ENV{KDEDIR} . "/share/apps/kdevelop/templates/cvsignore_template";
	$copydes = $homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/.cvsignore";
  system ("cp $copysrc $copydes");

  chdir ($homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/" . $nameLittle . "/templates/");
  $copydes = $homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/" . $nameLittle . "/templates/.cvsignore";
  system ("cp $copysrc $copydes");

 	chdir ($homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/" . $nameLittle . "/");
	$copydes = $homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/" . $nameLittle . "/.cvsignore";
	system ("cp $copysrc $copydes");

  if ($processes{APPLICATION} ne "customproj") {
   	chdir ($homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/" . $nameLittle . "/docs/");
		$copydes = $homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/" . $nameLittle . "/docs/.cvsignore";
		system ("cp $copysrc $copydes");

   	chdir ($homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/" . $nameLittle . "/docs/en/");
   	$copydes = $homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/" . $nameLittle . "/docs/en/.cvsignore";
   	system ("cp $copysrc $copydes");
  			
  	if ($processes{APPLICATION} eq "kdemini" || $processes{APPLICATION} eq "kdenormal") {	
			chdir ($homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/" . $nameLittle . "/api/");				
			$copydes = $homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/" . $nameLittle . "/api/.cvsignore";
			system ("cp $copysrc $copydes");
				
    	chdir ($homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/po/");
			$copydes = $homedirectory . "/.kde/share/apps/kdevelop/kdeveloptemp/po/.cvsignore";
			system ("cp $copysrc $copydes");
    }
  }
}

exit;

# this subroutine sets up the changing hash
sub setupDefaultChangings
{
    $changings{"|AUTHOR|"}=$processes{AUTHOR};
    $changings{"|EMAIL|"}=$processes{EMAIL};
    $changings{"|VERSION|"}=$processes{VERSION};
    $changings{"|DATE|"}=$date;
    $changings{"|YEAR|"}=$year;
    $changings{"|SUBDIR|"}= $nameLittle;
    $changings{"|PRJNAME|"}=$name;
    $changings{"|NAME|"}= $name;
    $changings{"|NAMELITTLE|"}= $nameLittle;
    $changings{"|NAMEBIG|"}= $nameBig;

}

#the subroutine for replacing words in files
sub replaceValuesInFile {

  my $hashref = shift;
  my $oldfile = shift;
  my $newfilename = shift || "";

  my $where = 0;
  my $wordlength = 0;
  my $word = "";
  my $replace = "";
  my $newfile = $overDirectory . "/newfile.txt";

  open (INPUT,$oldfile) || die "cannot open file: $oldfile";
  open (OUTPUT,">$newfile");

  # setup filename replacement...
  $hashref->{"|FILENAME|"}=$oldfile;
  $hashref->{"|FILENAME|"}=$newfilename if ($newfilename);

  while ( defined ($line = <INPUT> ))
  {
    # scan the hash
    foreach $word (keys %$hashref)
    {

      $where=0;
      $wordlength = length($word);
      $replace="";
      $replace=$hashref->{$word} if ($hashref->{$word});

      while ($where != -1)
      {
        $where = index($line,$word,$where);
        if ($where != -1)
        {
	 substr($line,$where,$wordlength) = $replace;
	 ++$where;
        }
      }
    }

    print OUTPUT $line;
  }
  close (INPUT);
  close (OUTPUT);
  unlink ($oldfile);
  rename ($newfile,$oldfile);
  unlink ($newfile);
}


sub unpackFiles {

    my ( $tarfile ) = @_;

    chdir ($overDirectory);
    printflush (STDOUT, "unzip file...\n");
    system ("gunzip $tarfile.gz");
    printflush (STDOUT, "untar file...\n");
    system ("tar -xf $tarfile");
    unlink $tarfile;

}


sub condCopyTemplateFiles {

    chdir ($underDirectory);
    mkdir ("templates", 0777);

    #copying the templates in the templatedirectoy
    if ($processes{CPP} eq "no" and $processes{HEADER} eq "no") {}
    else {
	if ($processes{HEADER} eq "yes") {
	    copy ("$local_datadir/kdevelop/header", 
		  "$underDirectory/templates/header_template");
	}
	
	if ($processes{CPP} eq "yes") {
	    copy ("$local_datadir/kdevelop/cpp",
		  "$underDirectory/templates/cpp_template");
	}
    }

}


sub condCopyGNUFiles {

    #if GNU-Files was chosen in kAppWizard
    if ($processes{GNU} eq "yes") {
    
    	#copying the GNU-Files and renamed these
    	chdir ($templatedir);
    	copy ("AUTHORS_template",   "$overDirectory/AUTHORS"  );
    	copy ("COPYING_template",   "$overDirectory/COPYING"  );
    	copy ("ChangeLog_template", "$overDirectory/ChangeLog");
    	copy ("INSTALL_template",   "$overDirectory/INSTALL"  );
    	copy ("README_template",    "$overDirectory/README"   );
    	copy ("TODO_template",      "$overDirectory/TODO"     );
        chdir($overDirectory);
        # replaced AUTHOR and EMAIL
        replaceValuesInFile(\%changings, "AUTHORS");
    }
}


sub condCopyLsmFile {

    #if LSM-Files was chosen in kAppWizard
    if ($processes{LSM} eq "yes") {

	#copying, rename and replace in the lsm-template
	chdir ($templatedir);
	copy ("lsm_template", "$overDirectory/$nameLittle.lsm");
	chdir ($overDirectory);
        replaceValuesInFile(\%changings, "$nameLittle.lsm");
    }
}


sub condCopyUserDocFiles {	

    #if USER-Docs was chosen in kAppWizard
    if ($processes{USER} eq "yes") {
    
	#copying, rename and replace in the handbook-en-template
	chdir ($templatedir);
	copy ("handbook_en_template", "$underDirectory/docs/en/index.sgml");
	chdir ("$underDirectory/docs/en");
        replaceValuesInFile(\%changings, "index.sgml");
    }
  
    #if USER-Docs was not chosen in kAppWizard
    if ($processes{USER} eq "no") {
            my %emptyIT = (
		"$nameLittle/docs/Makefile" => "",
		"$nameLittle/docs/en/Makefile" => "",
#                        "SUBDIRS = docs" => ""		do you really need this???
                        );
#	chdir ($underDirectory);
#	replaceValuesInFile(\%emptyIT, "Makefile.am");
	chdir ($overDirectory);
	replaceValuesInFile(\%emptyIT, "configure.in");

    }
}


sub condCopyKdelnkFile {

    #if .kdelnk-file was chosen in kAppWizard
    if ($processes{KDELNK} eq "yes") {
	
	#copying, rename and replace in the kdelnk-file
	chdir ($templatedir);
	copy ("kdelnk_template", "$underDirectory/$nameLittle.kdelnk");
	chdir ($underDirectory);
    	replaceValuesInFile(\%changings, "$nameLittle.kdelnk");
    }
}


sub condCopyProgIcons {

    #if no ProgIcon was chosen in kAppWizard
    if ($processes{PROGICON} eq "no") {} 
    
    #if the default ProgIcon was chosen in kAppWizard
    elsif ($processes{PROGICON} eq "(null)") {
	chdir ($kde_icondir);
	copy ("edit.xpm", "$underDirectory/$nameLittle.xpm");
	chdir ($underDirectory);
	chmod (0666, "$nameLittle.xpm");
    }
    
    #if a new ProgIcon was chosen in kAppWizard
    else {
	$icon = $kde_icondir."/".$processes{PROGICON};
	$copying = copy ($icon, "$underDirectory/$nameLittle.xpm");
	if ($copying)  {
	    $icon = $local_icondir."/".$processes{PROGICON};
	    copy ($icon, "$underDirectory/$nameLittle.xpm");
	}
	chdir ($underDirectory);
	chmod (0666, "$nameLittle.xpm");
    }
    
    #if no MiniIcon was chosen in kAppWizard
    if ($processes{MINIICON} eq "no") {}
    
    #if the default MiniIcon was chosen in kAppWizard
    elsif ($processes{MINIICON} eq "(null)") {
	chdir ($kde_miniicondir);
	copy ("application_settings.xpm", "$underDirectory/mini-$nameLittle.xpm");
	chdir ($underDirectory);
	chmod (0666, "mini-$nameLittle.xpm");
    }
    
    #if a new MiniIcon was chosen in kAppWizard
    else {
	$icon = $kde_miniicondir."/".$processes{MINIICON};
	$copying = copy ($icon, "$underDirectory/mini-$nameLittle.xpm");
	if ($copying)  {
	    $icon = $local_miniicondir."/".$processes{MINIICON};
	    copy ($icon, "$underDirectory/mini-$nameLittle.xpm");
	}
	chdir ($underDirectory);
	chmod (0666, "mini-$nameLittle.xpm");
    }
    
}


sub condCreateApiDoc {

    #if API-Files was chosen in kAppWizard
    if ($processes{API} eq "yes") {
	
	#create the API-documentation
	chdir ($underDirectory);
	mkdir ("api", 0777);
	system ("kdoc -d ./api/ $name *.h");

    }

}


sub processCppTemplate {

    my ( $file ) = @_;
  
    copy ("$local_datadir/kdevelop/cpp", $underDirectory);
    chdir ($underDirectory);
    replaceValuesInFile(\%changings, "cpp", $file);

    open (INPUT,"$file") || die "cannot open file: $file; $!";
    open (OUTPUT,">>cpp");
    while ( defined ($line = <INPUT> )) {
	print OUTPUT $line;
    }
    close (INPUT);
    close (OUTPUT);
    rename ("cpp" , $file);
    unlink ("cpp");

}


sub processHeaderTemplate {

    my ( $file ) = @_;
  
    copy ("$local_datadir/kdevelop/header", $underDirectory);
    chdir ($underDirectory);
    replaceValuesInFile(\%changings, "header", $file);

    open (INPUT,"$file") || die "cannot open file: $file; $!";
    open (OUTPUT,">>header");
    while ( defined ($line = <INPUT> )) {
	print OUTPUT $line;
    }
    close (INPUT);
    close (OUTPUT);
    rename ("header" , $file);
    unlink ("header");

}


sub changeNormalApp {

    unpackFiles("normal.tar");
    
    #renamed the directory
    printflush (STDOUT, "change files...\n");
    chdir ($overDirectory);
    rename ("skel", $nameLittle);
    
    #create the templatedirectory
    condCopyTemplateFiles();

    #Replacements in overDirectory  
    chdir ($overDirectory);
    replaceValuesInFile(\%changings, "Makefile.am");
    replaceValuesInFile(\%changings, "configure.in");

#    replaceOldFile("SUBDIRS = |NAMELITTLE|", "SUBDIRS = $nameLittle.po",
#		   "Makefile.am");  			not needed anymore!!
#    							the template should have
#							SUBDIRS = |NAMELITTLE|.po
#							but in this case it's made
#							by the binary itself
#    replaceOldFile("$nameLittle/Makefile", "$nameLittle/Makefile po/Makefile",
#		   "configure.in");    now fixed entry in template of normal.tar.gz

    #Replacements and name changing in underDirectory
    chdir ($underDirectory);

    rename ("kbase.cpp",     $nameLittle.".cpp");
    rename ("kbase.h",       $nameLittle.".h");
    rename ("kbasedoc.cpp",  $nameLittle."doc.cpp");
    rename ("kbasedoc.h",    $nameLittle."doc.h");
    rename ("kbaseview.cpp", $nameLittle."view.cpp");
    rename ("kbaseview.h",   $nameLittle."view.h");

    replaceValuesInFile(\%changings, "Makefile.am");
    replaceValuesInFile(\%changings, "main.cpp");
    replaceValuesInFile(\%changings, $nameLittle.".cpp");
    replaceValuesInFile(\%changings, $nameLittle.".h");
    replaceValuesInFile(\%changings, $nameLittle."doc.cpp");
    replaceValuesInFile(\%changings, $nameLittle."view.cpp");
    replaceValuesInFile(\%changings, $nameLittle."doc.h");
    replaceValuesInFile(\%changings, $nameLittle."view.h");
    replaceValuesInFile(\%changings, "resource.h");

    #Replacements in documentation directory
    chdir ($underDirectory."/docs/en");
    replaceValuesInFile(\%changings, "Makefile.am");

    condCopyGNUFiles();
    condCopyLsmFile();
    condCopyUserDocFiles();
    
    if ($processes{CPP} eq "yes") {
        processCppTemplate("main.cpp");
	processCppTemplate("$nameLittle.cpp");
	processCppTemplate("${nameLittle}view.cpp");
	processCppTemplate("${nameLittle}doc.cpp");
    }
    
    if ($processes{HEADER} eq "yes") {
	processHeaderTemplate("$nameLittle.h");
	processHeaderTemplate("${nameLittle}view.h");
	processHeaderTemplate("${nameLittle}doc.h");
	processHeaderTemplate("resource.h");
    }
    
    condCopyKdelnkFile();
    condCopyProgIcons();
    condCreateApiDoc();
    
}


sub changeMiniApp {

    unpackFiles("mini.tar");
    
    #renamed the directory
    printflush (STDOUT, "change files...\n");
    chdir ($overDirectory);
    rename ("skel", $nameLittle);
    
    #create the templatedirectory
    condCopyTemplateFiles();

    #Replacements in overDirectory  
    chdir ($overDirectory);
    replaceValuesInFile(\%changings, "Makefile.am");
    replaceValuesInFile(\%changings, "configure.in");

#    replaceOldFile("SUBDIRS = |NAMELITTLE|", "SUBDIRS = $nameLittle.po",
#		   "Makefile.am");  			not needed anymore!!
#    							the template should have
#							SUBDIRS = |NAMELITTLE|.po
#							but in this case it's made
#							by the binary itself
#    replaceOldFile("$nameLittle/Makefile", "$nameLittle/Makefile po/Makefile",
#		   "configure.in");    now fixed entry in template of mini.tar.gz

    #Replacements and name changing in underDirectory
    chdir ($underDirectory);

    rename ("skel.cpp", $nameLittle.".cpp");
    rename ("skel.h",   $nameLittle.".h");

    replaceValuesInFile(\%changings, "Makefile.am");
    replaceValuesInFile(\%changings, "$nameLittle.cpp");
    replaceValuesInFile(\%changings, "$nameLittle.h");
    replaceValuesInFile(\%changings, "main.cpp");

    chdir ($underDirectory."/docs/en");
    replaceValuesInFile(\%changings, "Makefile.am");

    condCopyGNUFiles();
    condCopyLsmFile();
    condCopyUserDocFiles();
    
    if ($processes{CPP} eq "yes") {
        processCppTemplate("main.cpp");
	processCppTemplate("$nameLittle.cpp");
    }
    
    if ($processes{HEADER} eq "yes") {
	processHeaderTemplate("$nameLittle.h");
    }

    condCopyKdelnkFile();
    condCopyProgIcons();
    condCreateApiDoc();
    
}


sub changeTerminalCPPApp {

    unpackFiles("cpp.tar");
    
    #renamed the directory
    printflush (STDOUT, "change files...\n");
    chdir ($overDirectory);
    rename ("skel", $nameLittle);
    
    #create the templatedirectory
    condCopyTemplateFiles();
    
    #replaced skel with the projectname in different files
    chdir ($overDirectory);
    replaceValuesInFile(\%changings, "Makefile.am");
    replaceValuesInFile(\%changings, "configure.in");

    chdir ($underDirectory);
    replaceValuesInFile(\%changings, "Makefile.am");
    replaceValuesInFile(\%changings, "main.cpp");

    chdir ($underDirectory . "/docs/en");
    replaceValuesInFile(\%changings, "Makefile.am");

    condCopyGNUFiles();
    condCopyLsmFile();
    condCopyUserDocFiles();
    
    if ($processes{CPP} eq "yes") {
        processCppTemplate("main.cpp");
    }
    
    condCopyKdelnkFile();
    condCopyProgIcons();
    condCreateApiDoc();
    
}


sub changeTerminalCApp {

    unpackFiles("c.tar");

    #renamed the directory
    printflush (STDOUT, "change files...\n");
    chdir ($overDirectory);
    rename ("skel", $nameLittle);

    #create the templatedirectory
    condCopyTemplateFiles();

    #replaced skel with the projectname in different files
    chdir ($overDirectory);
    replaceValuesInFile(\%changings, "Makefile.am");
    replaceValuesInFile(\%changings, "configure.in");

    chdir ($underDirectory);
    replaceValuesInFile(\%changings, "Makefile.am");
    replaceValuesInFile(\%changings, "main.c");

    chdir ($underDirectory . "/docs/en");
    replaceValuesInFile(\%changings, "Makefile.am");

    condCopyGNUFiles();
    condCopyLsmFile();
    condCopyUserDocFiles();

    if ($processes{CPP} eq "yes") {
        processCppTemplate("main.c");
    }

    condCopyKdelnkFile();
    condCopyProgIcons();
    condCreateApiDoc();

}

sub changeQtApp {

    unpackFiles("qt.tar");
    
    #renamed the directory
    printflush (STDOUT, "change files...\n");
    chdir ($overDirectory);
    rename ("skel", $nameLittle);
    
    #create the templatedirectory
    condCopyTemplateFiles();

    #Replacements in overDirectory  
    chdir ($overDirectory);
    replaceValuesInFile(\%changings, "Makefile.am");
    replaceValuesInFile(\%changings, "configure.in");

    #Replacements and name changing in underDirectory
    chdir ($underDirectory);

    rename ("bank.cpp",     $nameLittle.".cpp");
    rename ("bank.h",       $nameLittle.".h");
    rename ("bankdoc.cpp",  $nameLittle."doc.cpp");
    rename ("bankdoc.h",    $nameLittle."doc.h");
    rename ("bankview.cpp", $nameLittle."view.cpp");
    rename ("bankview.h",   $nameLittle."view.h");
    
    replaceValuesInFile(\%changings, "Makefile.am");
    replaceValuesInFile(\%changings, "main.cpp");
    replaceValuesInFile(\%changings, "$nameLittle.cpp");
    replaceValuesInFile(\%changings, "$nameLittle.h");
    replaceValuesInFile(\%changings, "${nameLittle}doc.cpp");
    replaceValuesInFile(\%changings, "${nameLittle}view.cpp");
    replaceValuesInFile(\%changings, "${nameLittle}doc.h");
    replaceValuesInFile(\%changings, "${nameLittle}view.h");
    replaceValuesInFile(\%changings, "resource.h");

#    replaceOldFile($name."AppDoc",  $name."Doc",   "skel.cpp");
#    replaceOldFile($name."AppDoc",  $name."Doc",   "skel.h");
#    replaceOldFile($name."AppView", $name."View",  "skel.cpp");
#    replaceOldFile($name."AppView", $name."View",  "skel.h");
#
#    should be inserted in the template as used with
#    |NAME|Doc, |NAME|View
#

    chdir ($underDirectory."/docs/en");
    replaceValuesInFile(\%changings, "Makefile.am");

    condCopyGNUFiles();
    condCopyLsmFile();
    condCopyUserDocFiles();
    
    if ($processes{CPP} eq "yes") {
        processCppTemplate("main.cpp");
	processCppTemplate("$nameLittle.cpp");
	processCppTemplate("${nameLittle}view.cpp");
	processCppTemplate("${nameLittle}doc.cpp");
    }
    
    if ($processes{HEADER} eq "yes") {
	processHeaderTemplate("$nameLittle.h");
	processHeaderTemplate("${nameLittle}view.h");
	processHeaderTemplate("${nameLittle}doc.h");
	processHeaderTemplate("resource.h");
    }
    
    condCopyKdelnkFile();
    condCopyProgIcons();
    condCreateApiDoc();
    
}


sub changeCustomApp {

    mkdir ($nameLittle, 0777);

    #create the templatedirectory
    condCopyTemplateFiles();

    condCopyGNUFiles();
    condCopyLsmFile();
    
}












