#!/bin/bash
# Copyright (C) 2017 Canonical, Ltd.
# Author: Emily Ratliff <emily.ratliff@canonical.com>
# License: GPLv3
#
# This script attempts to retrieve the package list from the latest
# Ubuntu Core snap.
#
TMPSNAPDIR=$(mktemp -d -t snap-XXXXXX)
outfile=$TMPSNAPDIR/ubuntu-core-supported.txt
cd $TMPSNAPDIR
cat > "$outfile" << EOM
# Source packages on this list are used to build Ubuntu Core.
# This package list is generated by 
# $ \$UCT/scripts/get-core-package-list
EOM

snap download core
unsquashfs -d $TMPSNAPDIR -f core_*.snap -ef /usr/share/snappy/dpkg.list
#grep "^ii" $TMPSNAPDIR/usr/share/snappy/dpkg.list | awk '{print $2}' >> $TMPSNAPDIR/tmp-ubuntu-core-supported.txt
grep "^ii" $TMPSNAPDIR/usr/share/snappy/dpkg.list | while read _ binpkg _ _ 
do
   echo "binary package: $binpkg"
   dpkg-query --show -f '${source:Package}\n' $binpkg >> $TMPSNAPDIR/tmp-outfile.txt
done
sort $TMPSNAPDIR/tmp-outfile.txt | uniq >> $outfile
echo "You can find your package list in $outfile"
echo "Any package failures that were noted on screen should be resolved by hand."

