#!/bin/sh

# This script will make a best-effort attempt at guessing the needed suite and
# dist values, falling back to sane defaults should the attempt fail.
# No changes will be made if the file already exists.

set -e

case "$1" in
    configure)

    # Assign default values.
    suite="__DSUITE__"
    dists="main"
    devuan="no"

    if [ -f /etc/apt/sources.list ] ; then
       detected="no"
       # detect if devuan repository is already there
       if grep -q '^[^#]*'.devuan.org/merged /etc/apt/sources.list ; then
          devuan="yes"
       fi

       # try to guess suite
       for s in stable ascii beowulf testing sid unstable ; do
          if grep -q '^[^#]*'$s /etc/apt/sources.list ; then
             detected=$s
          fi
       done
       case "$detected" in
          stable|ascii)
             suite="ascii"
             ;;
          testing|beowulf)
             suite="beowulf"
             ;;
          sid|unstable)
             suite="ceres"
             ;;
          *)
             suite="__DSUITE__"
       esac

       # guess components
       for dist in contrib non-free; do
         if grep -q '^[^#]* '$dist /etc/apt/sources.list; then
            dists="$dists $dist"
         fi
      done
    fi
    
    devrep="no"
    if [ -f /etc/apt/sources.lists.d/devuan.list ] ; then
       if grep -q "# autogenerated by devuan-baseconf" /etc/apt/sources.lists.d/devuan.list ; then
          devrep="yes"
       fi
    fi
    if [ x"$devrep" = x"no" ] ; then
       echo "# autogenerated by devuan-baseconf" > /etc/apt/sources.list.d/devuan.list
       echo "# decomment following lines to  enable the developers devuan repository" >> /etc/apt/sources.list.d/devuan.list
       echo "#deb http://packages.devuan.org/devuan $suite $dists" >> /etc/apt/sources.list.d/devuan.list
       echo "#deb-src http://packages.devuan.org/devuan $suite $dists" >> /etc/apt/sources.list.d/devuan.list
    fi

    if [ x"$devuan" = x"no" ] ; then
    
       # Source debconf library.
       . /usr/share/debconf/confmodule
       
       # db_unregister devuan-baseconf/suite

       tmpl="/tmp/devuan-baseconf.template.$$"
       cat > $tmpl << EOF
Template: devuan-baseconf/suite
Type: string
Description: Choose a suite for devuan

Template: devuan-baseconf/components
Type: string
Description: Choose component(s) for devuan

Template: devuan-baseconf/note
Type: note
Description: Enabling Devuan
 Please remove any debian repository from sources.list to avoid package conflicts
 After that, perform apt-get update && apt-get install devuan-keyring.
 Live long and prosper.

EOF

       db_x_loadtemplatefile $tmpl devuan-baseconf
       db_title "Devuan suite"
       #db_fset devuan-baseconf/suite seen false
       db_set devuan-baseconf/suite "$suite" 
       db_input high devuan-baseconf/suite || true
       db_go || true
       db_get devuan-baseconf/suite
       suite=$RET

       db_title "Devuan component(s)"
       #db_fset devuan-baseconf/components seen false
       db_set devuan-baseconf/components "$dists" 
       db_input high devuan-baseconf/components || true
       db_go || true
       db_get devuan-baseconf/components
       dists=$RET

       echo >> /etc/apt/sources.list
       echo "# Devuan repositories" >> /etc/apt/sources.list
       echo "deb http://packages.devuan.org/merged $suite $dists" >> /etc/apt/sources.list
       echo "deb-src http://packages.devuan.org/merged $suite $dists" >> /etc/apt/sources.list

       db_title "Enabling Devuan" 
       db_input high devuan-baseconf/note || true
       db_go || true
      
       rm -f $tmpl

    fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

#DEBHELPER#

exit 0
