#!/usr/bin/perl

# Konqueror Plus Execute ver.0.2.1
# 
# 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 = "ターミナルで実行";

# 終了確認ダイアログを省略するか (-n)
$opt = "";
if($ARGV[0] eq "-su"){
  $opt = "-su";
  shift @ARGV;
}
if($ARGV[0] eq "-n"){
  $opt .= " " if $opt ne "";
  $opt .= "-n";
  shift @ARGV;
}

$target = shift(@ARGV);
$opts = join(" ", @ARGV);

# ファイルを指定せずに実行 → ヘルプ表示
if($target eq ""){
  $msg  = "「$appname」は右クリックメニューで実行します。\n";
  $msg .= "\n";
  $msg .= "対象: 実行ファイル, Java Object\n";
  $msg .= "\n";
  $msg .= "ターミナルを開いて実行します。\n";
  $msg .= "デバッグやターミナルベースのソフトの実行に便利です。";
  `keasydlg -t "$appname ヘルプ" -n "$msg"`;
  exit;
}

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

# KTerm で呼び出し
chdir $path;
if($ext eq "class"){
  # Java Virtual Machine 上で実行
  $command = "java $file2 $opts";
}elsif($file eq "exec"){
  $command = $opts;
}elsif($path ne "."){
  $command  = "./$file";
  $command .= " $opts" if $opts ne "";
}else{
  $command  = $file;
  $command .= " $opts" if $opts ne "";
}
`konsole --nowelcome -e kp_exec1 /$opt /\"$command\"`;
