#!/bin/sh -e

##########################################################################
#   Enable SSL for pkg_add/pkgin on preexisting pkgsrc trees installed
#   via auto-pkgsrc-setup before it enabled SSL by default.
#
#   This script may also work on other pkgsrc trees, but that will
#   depend on how they are configured.  Try it at your own risk!
#
#   If this script fails, reinstall pkgin and libfetch via
#
#       cd $pkgsrc/pkgtools/pkgin
#       bmake clean
#       bmake clean-depends
#       bmake install
##########################################################################

if [ `uname` = Linux ]; then
    sed=sed
else
    sed=gsed
fi

bmake=`which bmake`

# Fix bug in bmake wrapper from old auto-pkgsrc-setup
$sed -i'' -e 's|bmake.bin $@|bmake.bin "$@"|g' $bmake || true

prefix=${bmake%%/bin/*}
pkgsrc=`echo $prefix | sed -e 's|pkg|pkgsrc|g'`

# Minimum base compilers for 2017Q3 and later
pkg_add gcc48 gcc5

# Rebuild libfetch and pkgin with openssl support
pkg_delete -f libfetch pkgin
cd $pkgsrc/pkgtools/pkgin
bmake clean
bmake clean-depends
bmake install PKG_OPTIONS.libfetch="inet6 openssl"
$sed -i'' -e 's|http:|https:|g' $prefix/etc/pkgin/repositories.conf
grep http $prefix/etc/pkgin/repositories.conf
pkgin avail|wc
