# Test that the zeekctl install command copies files, symlinks, and
# subdirectories (in the case of duplicates, only the first one is copied)
# from directories specified in the sitepolicypath zeekctl option, and that
# empty and nonexistent directories in sitepolicypath are ignored.
#
# @TEST-EXEC: bash %INPUT

. zeekctl-test-setup

echo "sitepolicypath=$ZEEKCTL_INSTALL_PREFIX/doesnotexist:$ZEEKCTL_INSTALL_PREFIX/test_site:$ZEEKCTL_INSTALL_PREFIX/emptytestdir:$ZEEKCTL_INSTALL_PREFIX/test_site2" >> $ZEEKCTL_INSTALL_PREFIX/etc/zeekctl.cfg

# Create directories and files specified in sitepolicypath
mkdir $ZEEKCTL_INSTALL_PREFIX/emptytestdir

mkdir -p $ZEEKCTL_INSTALL_PREFIX/test_site/emptysubdir
mkdir -p $ZEEKCTL_INSTALL_PREFIX/test_site/testsubdir
echo "my file" > $ZEEKCTL_INSTALL_PREFIX/test_site/testfile
echo "my subfile" > $ZEEKCTL_INSTALL_PREFIX/test_site/testsubdir/testsubfile
ln -s testsubdir/testsubfile $ZEEKCTL_INSTALL_PREFIX/test_site/zeek-test-package
ln -s testsubfile $ZEEKCTL_INSTALL_PREFIX/test_site/testsubdir/testsymlink

mkdir -p $ZEEKCTL_INSTALL_PREFIX/test_site2/testsubdir
echo "my file2" > $ZEEKCTL_INSTALL_PREFIX/test_site2/testfile
echo "other file" > $ZEEKCTL_INSTALL_PREFIX/test_site2/testfile2
echo "my subfile2" > $ZEEKCTL_INSTALL_PREFIX/test_site2/testsubdir/testsubfile
echo "other subfile" > $ZEEKCTL_INSTALL_PREFIX/test_site2/testsubdir/testsubfile2

# Verify that the files aren't already installed
test ! -e spool/installed-scripts-do-not-touch/site

zeekctl install

# Verify that the files and subdirectories were installed
while read line; do
    test -e $ZEEKCTL_INSTALL_PREFIX/$line
done << EOF
spool/installed-scripts-do-not-touch/site/testfile
spool/installed-scripts-do-not-touch/site/testfile2
spool/installed-scripts-do-not-touch/site/emptysubdir
spool/installed-scripts-do-not-touch/site/testsubdir/testsubfile
EOF

# Verify that the symlinks were installed as symlinks
while read line; do
    test -h $ZEEKCTL_INSTALL_PREFIX/$line
done << EOF
spool/installed-scripts-do-not-touch/site/zeek-test-package
spool/installed-scripts-do-not-touch/site/testsubdir/testsymlink
EOF

# Verify that for duplicate files, only the first one was installed
while read line; do
    ! grep -q 2 $ZEEKCTL_INSTALL_PREFIX/$line
done << EOF
spool/installed-scripts-do-not-touch/site/testfile
spool/installed-scripts-do-not-touch/site/testsubdir/testsubfile
EOF

# Verify that for duplicate subdirs, only the first one was installed
test ! -e $ZEEKCTL_INSTALL_PREFIX/spool/installed-scripts-do-not-touch/site/testsubdir/testsubfile2
