#!/bin/sh 

set -e

CONFFILE="/etc/apt/apt-build.conf"

# remove obsolete config file from apt-build <= 0.12.37
if dpkg-maintscript-helper supports rm_conffile 2>/dev/null ; then
  dpkg-maintscript-helper rm_conffile /etc/apt/sources.list.d/apt-build -- "$@"
fi

if [ "$1" = "configure" ] ; then
	. /usr/share/debconf/confmodule
  
  db_get apt-build/olevel
	case "$RET" in
	  "Light")
		  Olevel="-O1"
		  ;;
	  "Medium")
		  Olevel="-O2"
		  ;;
	  "Strong")
		  Olevel="-O3"
      ;;
	esac
  
  db_get apt-build/build_dir
  build_dir="$RET"
  
  db_get apt-build/repository_dir
  repository_dir="$RET"
  
  db_get apt-build/add_to_sourceslist
  add_to_sourceslist="$RET"
  
  db_get apt-build/options
  options="$RET"
  
  db_get apt-build/make_options
  make_options="$RET"
  
  db_get apt-build/archtype
  #march=-march="$RET"
  mtune=-mtune="$RET"
  
	# Create build_dir
	if [ ! -e "$build_dir" ] ; then
	  mkdir -p "$build_dir"
	fi
  
	# Create repository_dir
	if [ ! -e "$repository_dir" ] ; then
	    mkdir -p "$repository_dir"/dists/apt-build/main
      ln -s ../../.. "$repository_dir"/dists/apt-build/main/binary-$(dpkg --print-architecture)
  fi
  
  # link release file for apt-build distribution (for upgrade from 0.12.41)
  if [ ! -e "$repository_dir"/dists/apt-build/Release ] ; then
    ln -s ../../Release "$repository_dir"/dists/apt-build/Release
  fi


  # prepare sources.list entry
  eval $(apt-config shell sourceslist Dir::Etc::sourcelist/f)
  eval $(apt-config shell sourcesparts Dir::Etc::sourceparts/d)
  aptbuildsource="$sourcesparts"apt-build.list
  debline="deb [trusted=yes] file:$repository_dir apt-build main"
  src_enabled="false"
  
  # run loop to prevent errors if some sources does not exist
  for source in "$sourceslist" "$sourcesparts"*.list ; do
    if [ -e "$source" ] ; then
	    # comment in all sources lists if asked
	    if [ "$add_to_sourceslist" = "false" ] ; then
        sed -i -e "s|^[[:space:]]*$debline.*|#$debline|" "$source"
      
      # check if source entry is already enabled
      elif grep -Eq "^[[:space:]]*$debline" "$source" ; then
        src_enabled="true"
      fi
    fi
  done
  
  # Fixup previous /etc/apt/sources.list.d/apt-build.list to cope
  # without Apt::Get::AllowUnauthenticated=true
  if [ -e "$aptbuildsource" ] ; then
      deblineold="`echo "$debline" | sed -e 's|deb \[trusted=yes\] file|deb file|'`"
      sed -i -e "s|^$deblineold|$debline|" "$aptbuildsource"
  fi

  # do not modify anything if source entry is already enabled
  if [ "$add_to_sourceslist" = "true" ] && [ $src_enabled = "false" ] ; then
    if [ ! -e "$sourcesparts" ] ; then
      mkdir -p "$sourcesparts"
    fi
    
    if [ ! -e "$aptbuildsource" ] ; then
      echo "$debline" > "$aptbuildsource"
    fi
    
    if grep -qF "$debline" "$aptbuildsource" ; then
      # modify only first occurrence of $debline to prevent duplicate entries
      sed -i -e "0,\|^.*$debline|s||$debline|" "$aptbuildsource"
    else
      echo "$debline" >> "$aptbuildsource"
    fi
  fi

	# Remove one-byte Packages file created by old postinst
	if [ -f $repository_dir/Packages.gz ] && [ $(zcat $repository_dir/Packages.gz | wc -c) -eq 1 ] ; then
    rm -f $repository_dir/Packages.gz
  fi
	
	if [ ! -e "$repository_dir/Packages.gz" ] ; then
	  gzip -9 < /dev/null > "$repository_dir/Packages.gz"
	fi
	
  
	# Configuration options
  # if config file does not exist
  if [ ! -e $CONFFILE ] ; then
    echo "build-dir =" > $CONFFILE
    echo "repository-dir =" >> $CONFFILE
    echo "Olevel =" >> $CONFFILE
    #echo "march =" >> $CONFFILE
    echo "mtune =" >> $CONFFILE
    echo "options =" >> $CONFFILE
    echo "make_options =" >> $CONFFILE
  fi
  
  cp -a -f $CONFFILE $CONFFILE.tmp
  
  # (re)add deleted or commented variables
  test -z "build-dir" || grep -Eq '^[[:space:]]*build-dir =' $CONFFILE || \
    echo "build-dir =" >> $CONFFILE
  test -z "repository-dir" || grep -Eq '^[[:space:]]*repository-dir =' $CONFFILE || \
    echo "repository-dir =" >> $CONFFILE
  test -z "Olevel" || grep -Eq '^[[:space:]]*Olevel =' $CONFFILE || \
    echo "Olevel =" >> $CONFFILE
  #test -z "march" || grep -Eq '^[[:space:]]*march =' $CONFFILE || \
  #  echo "march =" >> $CONFFILE
  test -z "mtune" || grep -Eq '^[[:space:]]*mtune =' $CONFFILE || \
    echo "mtune =" >> $CONFFILE
  test -z "options" || grep -Eq '^[[:space:]]*options =' $CONFFILE || \
    echo "options =" >> $CONFFILE
  test -z "make_options" || grep -Eq '^[[:space:]]*make_options =' $CONFFILE || \
    echo "make_options =" >> $CONFFILE
  
  sed -e "s|^[[:space:]]*build-dir =.*|build-dir = $build_dir|" \
      -e "s|^[[:space:]]*repository-dir =.*|repository-dir = $repository_dir|" \
      -e "s|^[[:space:]]*Olevel =.*|Olevel = $Olevel|" \
      -e "s|^[[:space:]]*mtune =.*|mtune = $mtune|" \
      -e "s|^[[:space:]]*options =.*|options = \" $options\"|" \
      -e "s|^[[:space:]]*make_options =.*|make_options = \" $make_options\"|" \
      < $CONFFILE > $CONFFILE.tmp
  
  mv -f $CONFFILE.tmp $CONFFILE
fi

apt-build build-repository

#DEBHELPER#
