#!/usr/bin/perl

# Konqueror Plus Extract 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 = "展開";
$target = $ARGV[0];

$extdir = $KPRC{"ExtractDir"};
if($extdir eq ""){
  $home = &homedir;
  $extdir = "$home/Desktop";
}

# ファイルを指定せずに実行 → ヘルプ表示
if($target eq ""){
  for(;;){
    $msg  = "「$appname」は右クリックメニューで実行します。\n";
    $msg .= "\n";
    $msg .= "対象: (TAR+)GZ, (TAR+)BZ2, (TAR+)Z, TAR, ZIP, LZH, RPM, JAR\n";
    $msg .= "\n";
    $msg .= "アーカイブを以下のディレクトリに展開します。\n";
    $msg .= $extdir;
    $ret = `keasydlg -t "$appname ヘルプ" -n "$msg" -1 "閉じる" -2 "展開先変更"`;
    exit if $ret eq "0";
    $ret = `keasydlg --dir $extdir`;
    if($ret ne ""){
      $ret =~ s/\/$//;
      $extdir = $ret;
      $KPRC{"ExtractDir"} = $extdir;
      &writekprc;
    }
  }
}

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

# 拡張子別処理
chdir $path;
if    ($ext eq "tar.gz"  || $ext eq "tgz"){
  $command = "gunzip -c $target | tar xvf -";
}elsif($ext eq "tar.bz2" || $ext eq "tbz"){
  $command = "bunzip2 -c $target | tar xvf -";
}elsif($ext eq "tar.z"   || $ext eq "taz"){
  $command = "uncompress -c $target | tar xvf -";
}elsif($ext eq "tar"){
  $command = "tar xvf $target";
}elsif($ext eq "zip"){
  $command = "unzip $target";
}elsif($ext eq "lzh"){
  $command = "lha e $target";
}elsif($ext eq "rpm"){
  $command = "rpm2cpio $target | cpio -ivd";
}elsif($ext eq "jar"){
  $command = "jar xvf $target";
}elsif($ext eq "z"){
  $err = 1 if system "uncompress -c $file > $file2";
}elsif($ext eq "gz"){
  $err = 1 if system "gunzip -c $file > $file2";
}elsif($ext eq "bz2"){
  $err = 1 if system "bunzip2 -c $file > $file2";
}else{
  `keasydlg -t "$appname" -w "ファイルタイプが不明です。\n\n$target"`;
  exit 1;
}
if($command eq ""){
  exit if $err == 0;
  # 失敗
  `keasydlg -t "$appname" -w "解凍中にエラーが発生しました。"`;
  exit 1;
}

# 解凍用フォルダ → $work
# 展開先ディレクトリに対象ファイルから拡張子を除いたディレクトリを作成
# 例: abc.tar.gz → $extdir/abc
$work = "$extdir/$file2";

# 解凍用のフォルダを作る
mkdir($work, 0777);
if(!-d $work){
  # 失敗
  $msg  = "解凍用のフォルダを作成できません。\n";
  $msg .= "デスクトップに対象ファイルから拡張子を抜いたものと\n";
  $msg .= "同名のファイルが存在します。\n";
  $msg .= "移動するか削除するかしてから再度お試しください。";
  `keasydlg -t "$appname" -w "$msg"`;
  exit 1;
}

# 解凍用フォルダのアイコンを変更
open(FILE, ">$work/.directory");
print FILE "[Desktop Entry]\n";
print FILE "Icon=folder_red\n";
close(FILE);

# 解凍
chdir $work;
`kp_exec -n exec \"$command\"`;

# Konqueror を開く
&openKonq($work);
