#!/bin/sh
# Copy a cd directly from one disk to another


# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

#grafburn stores it's profile information in ~/.config/grafburnrc
PROFILE=$HOME/.config/grafburnrc
#our preferences
. $PROFILE

if [ ! -f ~/.config/grafburnrc ];then
  gxmessage -center -title "Grafburn" -bg "#dcdad0" -buttons "GTK_STOCK_OK" \
   "Grafburnは現在、未設定です。設定ダイアログを起動します。"
  $APPDIR/set_prefs
  gxmessage -center -title "Grafburn" -bg "#dcdad0" -buttons "GTK_STOCK_OK" \
   "Grafburnにようこそ！"
fi

CDRDAO="`which cdrdao`"
if [ "$CDRDAO" = "" ];then
 Xdialog --title "Grafburn: copy cd" --icon $APPDIR/icons/big/drive-optical.png \
  --left --msgbox "cdrdaoプログラムが見つかりません。\n\
Grafburnは少々限界です。もっと厳密に言うと\n\
始めにPbcdripperなどのプログラムを使って\n\
音楽CDをリッピングしておかないとコピー\n\
できません。\n\
\n\
この機能が必要なら、パッケージマネージャ\n\
を使ってcdrdaoをインストールして下さい。\n\
データディスクをコピーするためにまた元気\n\
を出しましょう。でもこの作業は少し遅く\n\
もっと面倒です。" 0 0
fi

# List drives
CDSCSIDRIVES="`cdrecord -scanbus 2>&1 | grep -i cd-*rom | grep '[0-9],[0-9],[0-9]' | grep -v '*$' | tr '|' " " | tr '\t' '|' | cut -f 2,3 -d '|' | tr -s " " | tr "'" '_' | tr " " '_' | tr -s '_'`" #' 
ATAPIDRIVES="`probedisk | grep '|cdrom|' | grep '/dev/hd' | cut -f 1,3 -d '|' | tr " " '_' | tr -s '_'`"
DVDSCSIDRIVES="`probedisk | grep '/dev/scd' | cut -f 1,3 -d '|' | tr " " '_' | tr -s '_'`"
DRIVES="$CDSCSIDRIVES
$ATAPIDRIVES
$DVDSCSIDRIVES"
DRIVESELECTIONS="`echo "$DRIVES" | tr '|' " " | tr "\n" " "`"

# This step different depending on whther we have cdrdao or not
if [ ! "$CDRDAO" = "" ];then
 INDRIVE="`Xdialog --left --stdout --icon $APPDIR/icons/big/drive-optical.png \
  --title "Grafburn: copy cd" --no-cancel --menubox "Choose the drive you will \
be using to copying from." 0 0 5 $DRIVESELECTIONS 2> /dev/null`"

 OUTDRIVE="`Xdialog --left --stdout --icon $APPDIR/icons/big/drive-optical.png \
  --title "Grafburn: copy cd" --no-cancel --menubox "Choose the drive you will \
be using\nto burn the disk. It is possible to use\nthe same drive so long as \
you are\nburning a data disk, but this is not possible\nwith an audio cd." \
  0 0 5 $DRIVESELECTIONS 2> /dev/null`"
else
 INDRIVE="`Xdialog --left --stdout --icon $APPDIR/icons/big/drive-optical.png \
  --title "Grafburn: CDのコピー" --no-cancel --menubox \
"コピーするために使うドライブを選択して下さい。" \
  0 0 5 $DRIVESELECTIONS 2> /dev/null`"
fi
 
# Case 1 - two different drives
if [ ! "$INDRIVE" = "$OUTDRIVE" ];then
 if [ ! "$CDRDAO" = "" ];then
  Xdialog --title "Grafburn: CDのコピー" --icon $APPDIR/icons/big/drive-optical.png \
   --stdout --left --ok-label OK --cancel-label Cancel --yesno \
"コピーしたいディスクを $INDRIVE に入れて下さい。\n\
ブランクCD/DVDを $OUTDRIVE に入れて下さい。\n\
（あなたがまだ、そうしてなかったなら）" 0 0
  [ ! $? -eq 0 ] && exit
  rxvt -geometry 80x10 -fg green -bg black -title "Grafburn: copying disk" \
   -e cdrdao copy --on-the-fly --source-device $INDRIVE --device $OUTDRIVE
 else
  Xdialog --title "Grafburn: CDのコピー" --icon $APPDIR/icons/big/drive-optical.png \
   --stdout --left --ok-label OK --cancel-label Cancel --yesno \
"コピーしたいディスクを $INDRIVE に入れて下さい。\n\
（あなたがまだ、そうしてなかったなら）\n\
あなたがcdrdaoを使っていないので、これは2段階で\n\
実行されます。" 0 0
  [ ! $? -eq 0 ] && exit
  CANTREAD="`file -s -L $INDRIVE | grep '(Input/output error)'`"
  if [ ! "$CANTREAD" = "" ];then
   gxmessage -center -bg red -buttons "GTK_STOCK_DIALOG_ERROR" -wrap -title \
    "Grafburn: ディスクが読めません" "エラー！ Grafburnは $INDRIVE の \
ディスクが読めません。３つの原因が考えられます。１つはディスクが音楽CDの場合、 \
または空白ディスクの場合、あるいはまだディスクを挿入していない場合。Grafburn \
を終了します。"
   exit 1
  fi
  gxmessage -center -buttons "GTK_STOCK_OK" -title "Grafburn: ファイルのコピー" \
  -bg "#dcdad0" "ファイルをコピーしています。お待ち下さい..." &
  dd if=$INDRIVE of="$OUTDIR"tmp.iso
  killall gxmessage
  $APPDIR/burn_iso "$OUTDIR"tmp.iso
  rm -f $OUTDIRtmp.iso
  exit 0
 fi
else
 Xdialog --title "Grafburn: CDのコピー" --icon $APPDIR/icons/big/drive-optical.png \
  --stdout --left --ok-label OK --cancel-label Cancel --yesno \
"コピーしたいディスクを $INDRIVE に入れて下さい。\n\
（あなたがまだ、そうしてなかったなら）\n\
あなたはコピーと書き込みに同じドライブを使うので\n\
これは２段階で実行されます。" 0 0
 [ ! $? -eq 0 ] && exit
 CANTREAD="`file -s -L $INDRIVE | grep '(Input/output error)'`"
 if [ ! "$CANTREAD" = "" ];then
  gxmessage -center -bg red -buttons "GTK_STOCK_DIALOG_ERROR" -wrap -title \
   "Grafburn: ディスクが読めません" "エラー！ Grafburnは $INDRIVE の \
ディスクが読めません。３つの原因が考えられます。１つはディスクが音楽CDの場合、 \
または空白ディスクの場合、あるいはまだディスクを挿入していない場合。Grafburn \
を終了します。"
  exit 1
 else
   gxmessage -center -buttons "GTK_STOCK_OK" -title "Grafburn: ファイルのコピー" \
   -bg "#dcdad0" "ファイルをコピーしています。お待ち下さい..." &
   dd if=$INDRIVE of="$OUTDIR"tmp.iso
   killall gxmessage
   $APPDIR/burn_iso "$OUTDIR"tmp.iso
   rm -f $OUTDIRtmp.iso
   exit 0
 fi
fi

