#!/bin/bash

# Find packages that have versions in sage-on-gentoo and science
# Algorithm: concatenate, sort, find duplicates

# Load the overlay contents into arrays
science=( $(eix --in-overlay science --only-names) )
sage=( $(eix --in-overlay sage-on-gentoo --only-names) )

# concatenate
concat=( "${science[@]}" "${sage[@]}" )

# sort
readarray -t sorted < <(printf '%s\0' "${concat[@]}" | sort -z | xargs -0n1)

# Iterate and output duplicates:
for (( i=0 ; i < ${#sorted[@]} ; i++ ))
do
    if [ "${sorted[$i]}" = "${sorted[$(($i+1))]}" ] 
    then 
 	echo ${sorted[$i]}
    fi
done
