#!/bin/sh
# Copyright 1998,1999,2000,2001,2002,2003,2004,2005,2006,
#           2007 Free Software Foundation, Inc.
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.  This file is
# distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY, to the extent permitted by law; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

. $srcdir/defs.inc || exit 3

# This is the first test run by "make check".  First kill a possible
# gpg-agent process from a previous test run.
if $GPG_AGENT --quiet; then
    echo "$pgmname: killing leftover gpg-agent process" >&2
    $GPG_CONNECT_AGENT killagent /bye >/dev/null
    sleep 2
fi


info "Deleting old files"
if [ -f Makefile -a -f $srcdir/decrypt-dsa.test ]; then
  :
else
  fatal "not running in the test directory"
  exit 1
fi
if [ -d private-keys-v1.d ]; then
    rm private-keys-v1.d/* 2>/dev/null || true
    rmdir private-keys-v1.d
fi
for i in pubring.gpg pubring.gpg~ trustdb.gpg trustdb.gpg~ ; do
  [ -f "$i" ] && rm "$i"
done

# Now start the agent right away, so that there is only one place
# where starting the agent may fail.  To speed up key generation we
# create a faked random seed file.  Note that we need to set the
# agent-program so that gpg-connect-agent is able to start the agent
# we are currently testing and not an already installed one.
# The "|--debug-quick-random" is a hack to start gpg-agent with
# that option on the command line.
info "Starting the agent"
$MKTDATA 600 >random_seed
if $GPG_CONNECT_AGENT -v \
    --agent-program="${GPG_AGENT}|--debug-quick-random" /bye; then
    :
else
    error "starting the gpg-agent failed"
    exit 1
fi


info "Creating sample data files"
for i in 500 9000 32000 80000; do
    $MKTDATA  $i >data-$i
done

info "Unpacking samples"
$GPG --dearmor < $srcdir/plain-1o.asc > ./plain-1
$GPG --dearmor < $srcdir/plain-2o.asc > ./plain-2
$GPG --dearmor < $srcdir/plain-3o.asc > ./plain-3
$GPG --dearmor < $srcdir/plain-largeo.asc > ./plain-large

info "Storing private keys"
for i in 50B2D4FA4122C212611048BC5FC31BD44393626E \
         7E201E28B6FEB2927B321F443205F4724EBE637E \
         13FDB8809B17C5547779F9D205C45F47CE0217CE \
         343D8AF79796EE107D645A2787A9D9252F924E6F \
         8B5ABF3EF9EB8D96B91A0B8C2C4401C91C834C34 \
         0D6F6AD4C4C803B25470F9104E9F4E6A4CA64255 \
         FD692BD59D6640A84C8422573D469F84F3B98E53 \
         76F7E2B35832976B50A27A282D9B87E44577EB66 \
         A0747D5F9425E6664F4FFBEED20FBCA79FDED2BD \
         00FE67F28A52A8AA08FFAED20AF832DA916D1985 \
         1DF48228FEFF3EC2481B106E0ACA8C465C662CC5 \
         A2832820DC9F40751BDCD375BB0945BA33EC6B4C \
         ADE710D74409777B7729A7653373D820F67892E0 \
         CEFC51AF91F68A2904FBFF62C4F075A4785B803F; do
   $GPG --dearmor < $srcdir/privkeys/$i.asc > private-keys-v1.d/$i.key
done

info "Importing public demo and test keys"
$GPG --yes --import $srcdir/pubdemo.asc $srcdir/pubring.asc
$GPG --dearmor < $srcdir/pubring.pkr.asc | $GPG --yes --import


info "Preset passphrases"
# one@example.com
$GPG_PRESET_PASSPHRASE --preset -P def 50B2D4FA4122C212611048BC5FC31BD44393626E
$GPG_PRESET_PASSPHRASE --preset -P def 7E201E28B6FEB2927B321F443205F4724EBE637E
# alpha@example.net
$GPG_PRESET_PASSPHRASE --preset -P abc 76F7E2B35832976B50A27A282D9B87E44577EB66
$GPG_PRESET_PASSPHRASE --preset -P abc A0747D5F9425E6664F4FFBEED20FBCA79FDED2BD


# Note: secring.asc and secring.skr.asc are the original secrings for
# our test files.  We don't support this as storage format anymore but
# keep the files here for reference.  The actual keys have been
# extracted and put in gpg-agent's format unter privkeys/.  Because
# the current gpg's import feature does not support storing of
# unprotected keys in the new gpg-agent format, we had to resort to
# some trickery to convert them.


info "Printing the GPG version"
$GPG --version | awk '{print "    > " $0}'

#fixme: check that the output is as expected
