#!/usr/bin/perl

# Konqueror Plus Compile ver.0.1.0
# 
# Copyright (c) 1999-2000 Japan KDE Users' Group
# 
# All rights reserved.
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, and/or sell copies of the Software, and to permit persons
# to whom the Software is furnished to do so, provided that the above
# copyright notice(s) and this permission notice appear in all copies of
# the Software and that both the above copyright notice(s) and this
# permission notice appear in supporting documentation.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
# OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
# INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

require "$ENV{'KDEDIR'}/lib/konqplus.pl";

$appname = "コンパイル";
$target = $ARGV[0];

$make = $KPRC{"Make"};
$make = "make" if $make eq "";

# ファイルを指定せずに実行 → ヘルプ表示
if($target eq ""){
  for(;;){
    $msg  = "「$appname」は右クリックメニューで実行します。\n";
    $msg .= "\n";
    $msg .= "対象: Makefile, TeX, PO, Java\n";
    $msg .= "\n";
    $msg .= "コンパイルします。\n";
    $msg .= "TeX のコンパイル後には dvi ファイルを開きます。\n";
    $msg .= "\n";
    $msg .= "make: $make";
    $ret = `keasydlg -t "$appname ヘルプ" -n "$msg" -1 "閉じる" -2 "make 変更"`;
    exit if $ret eq "0";
    $ret = `keasydlg --input \"$make\" "make に使用するコマンドを入力してください。"`;
    if($ret ne "" && $ret !~ /^-/){
      $make = $ret;
      $KPRC{"Make"} = $make;
      &writekprc;
    }
  }
}

# 初期設定
$path = &dirname($target);
$file = &basename($target);
$file2 = &rmext($file);
$ext = &getext($file);
chdir $path;

# make
if($file =~ /^[Mm]akefile/){
  `kp_exec -n $make -f $file`;
  exit;
}

# Java
if($ext eq "java"){
  `kp_exec -n javac $file`;
  exit;
}

# TeX
if($ext eq "tex"){
  `kp_exec -n platex $file`;
  # dvi ファイルが生成されていれば xdvi 起動
  `xdvi $file2.dvi` if -f "$file2.dvi";
  exit;
}

# PO
if($ext eq "po" || $ext eq "pot"){
  `msgfmt $file -o $file2.mo`;
  exit;
}

# エラー
`keasydlg -t "$appname" -w "ファイルタイプが不明です。"`;
exit 1;
