# -*- shell-script -*-
FILENAME=lha-test9
message 'testing overriding existing files (this test need the Ruby interpreter http://www.ruby-lang.org/)'

pty()
{
  count=$1; shift
  input=$1; shift  # y, n, a or s

  ruby <<EOF
    require 'pty'

    inp, out, fd = PTY.getpty("$*")

    th = Thread.start {
      $count.times {
	out.puts "$input"
      }
    }

    loop {
      begin
	print inp.gets
      rescue Errno::EIO
      rescue PTY::ChildExited
        if \$!.status & 0xff != 0
	  STDERR.puts [\$!, \$!.status, \$?].inspect
	end
	exit \$!.status >> 8
      end
    }
EOF
}

mkdir test-tmp &&
echo foo > test-tmp/test-a &&
echo bar > test-tmp/test-b &&
echo baz > test-tmp/test-c &&
cp -pr test-tmp test-tmp2  &&
$lha c test-tmp.lzh test-tmp
							check $? $LINENO
$lha v test-tmp.lzh &&
$lha v test-1.lzh
							check $? $LINENO

# skip file
rm -rf test-tmp &&
cp -pr test-tmp2 test-tmp &&
pty 3 n  $lha xw=test-tmp test-1.lzh
							check $? $LINENO
diff -r test-tmp test-tmp2
							check $? $LINENO
# overwrite
rm -rf test-tmp &&
cp -pr test-tmp2 test-tmp &&
pty 3 y  $lha xw=test-tmp test-1.lzh
							check $? $LINENO
diff -r test-1 test-tmp
							check $? $LINENO
# overwrite all
rm -rf test-tmp &&
cp -pr test-1 test-tmp &&
pty 1 a  $lha x test-tmp.lzh
							check $? $LINENO
diff -r test-tmp test-tmp2
							check $? $LINENO
# skip all
rm -rf test-tmp &&
cp -pr test-tmp2 test-tmp &&
pty 1 s	 $lha xw=test-tmp test-1.lzh
							check $? $LINENO
diff -r test-tmp test-tmp2
							check $? $LINENO
