#!/bin/sh -e
#
#
#
#  xctest - shell script to test xclip
#  Copyright (C) 2001 Kim Saunders
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

cleanup() {
    # quietly remove temp files
    rm "$tempi" "$tempo" 2>/dev/null
    # Kill any remaining xclip processes
    killall xclip 2>/dev/null
}
trap cleanup EXIT HUP INT



if sleep 0.1 2>/dev/null; then
    delay=0.1   # seconds to wait before running xclip -o
else
    delay=1
fi

# test to make sure ./xclip exists
if [ ! -x xclip ]; then
    echo "Error: xclip doesn't exist in the current directory."
    exit
fi

checker=""

for param in "$@"; do
    case $param in
        --valgrind) checker="valgrind --num-callers=8";;
    esac
done

echo "Testing whether xclip exits correctly when the selection is lost"
echo "hello" | ./xclip -q -i 2>/dev/null &
sleep "$delay"
echo "goodbye" | ./xclip -i
sleep "$delay"
if ps $! >/dev/null; then
    echo "FAIL: Zombie xclip yet lives! Killing."
    killall xclip
    exit 1
else
    echo "PASS: xclip exited correctly after losing selection"
fi

# temp file names (in and out)
tempi=`mktemp` || exit 1
tempo=`mktemp` || exit 1

# test xclip on different amounts of data (2^fold) to bring out any errors
c=0      # Number of folds completed.
lines=1  # Current file size (2 to the c power).
printf '%s' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_ '  > "$tempi"
printf '%s\n' 'abcdefghijklmnopqrstuvwzyz!@#$%^&*()' >> "$tempi"
for fold in 1 4 7 10 13; do
    # double size of file by two
    while [ "$c" -lt "$fold" ]; do
        cat "$tempi" "$tempi" > "$tempo" && mv "$tempo" "$tempi"
        lines=$(( 2 * lines ))
        c=$(( c + 1 ))
    done

    # test piping the file to xclip, using all selections
    echo "Piping a $lines line file to xclip"
    for sel in primary secondary clipboard buffer; do
        printf '%s' "  Using the $sel selection	"
        cat "$tempi" | $checker ./xclip -sel "$sel" -i
        sleep "$delay"
        $checker ./xclip -sel "$sel" -o > "$tempo"
        if diff "$tempi" "$tempo"; then
            echo "PASS"
        else
            echo "FAIL"
            exit 1
        fi
    done
    echo

    # test xclip reading the file
    echo "Reading a $lines line file with xclip"
    for sel in primary secondary clipboard buffer; do
        printf '%s' "  Using the $sel selection	"
        $checker ./xclip -sel "$sel" -i "$tempi"
        sleep "$delay"
        $checker ./xclip -sel "$sel" -o > "$tempo"
        if diff "$tempi" "$tempo"; then
            echo "PASS"
        else
            echo "FAIL"
            exit 1
        fi
    done
    echo

    # test xclip filtering a file
    echo "Filtering a $lines line file through xclip"
    for sel in primary secondary clipboard buffer; do
        printf '%s' "  Using the $sel selection	"
        $checker ./xclip -sel "$sel" -f < "$tempi" > "$tempo"
        sleep "$delay"
        if diff "$tempi" "$tempo"; then
            echo "PASS"
        else
            echo "FAIL"
            exit 1
        fi
    done
    echo

done

# test xclip on files >1MB to force INCR mode
for i in 1 2 16; do
    dd if=/dev/zero bs=1024 count=$((i*1024)) of="$tempi" >/dev/null 2>&1
    echo "Binary file large enough to force INCR mode ($i MB)"
    for sel in primary secondary clipboard buffer; do
        printf '%s' "  Using the $sel selection	"
        $checker ./xclip -sel "$sel" -i -t image/jpeg < "$tempi"
        sleep "$delay"
        $checker ./xclip -sel "$sel" -o -t image/jpeg > "$tempo"
        if diff "$tempi" "$tempo"; then
            echo "PASS"
        else
            echo "FAIL"
            exit 1
        fi
    done
done

# Kill any remain xclip processes
killall xclip

# quietly remove temp files
rm "$tempi" "$tempo" 2>/dev/null
