#!/bin/sh -e

set -e

#####################################
# Setting up the default locale list:

CACHEDIR=/var/cache/localepurge
DEFAULTLIST=$CACHEDIR/defaultlist
LOCALELIST=$CACHEDIR/localelist

if [ ! -f $LOCALELIST ]; then
  cp $DEFAULTLIST $LOCALELIST
else
  mv $LOCALELIST $LOCALELIST-old
  sort -u $LOCALELIST-old $DEFAULTLIST > $LOCALELIST
fi

##############################################
# Now the debconf routine creating $CONFIGFILE
 
CONFIGFILE=/etc/locale.nopurge

. /usr/share/debconf/confmodule

cat <<EOF > $CONFIGFILE
####################################################
# This is the configuration file for localepurge(8).
#
#      DO NOT EDIT!
#
# localepurge(8) uses debconf for storing all its 
# configuration. Manual entries to this file will be 
# irreversibly lost after upgrade or reinstallation 
# of localepurge. Configuration should only be done
# using the command
#
#       dpkg-reconfigure localepurge
#
####################################################

####################################################
# Uncommenting this string enables removal of localized 
# man pages based on the configuration information for
# locale files defined below:

EOF

db_get localepurge/mandelete

if [ "$RET" = "true" ]; then
    echo MANDELETE >> $CONFIGFILE
else
    echo '#MANDELETE' >> $CONFIGFILE
fi

cat <<EOF >> $CONFIGFILE

####################################################
# Uncommenting this string causes localepurge to simply delete
# locales which have newly appeared on the system without
# bothering you about it:

EOF

db_get localepurge/dontbothernew

if [ "$RET" = "true" ]; then
    echo '#DONTBOTHERNEWLOCALE' >> $CONFIGFILE
else
    echo DONTBOTHERNEWLOCALE >> $CONFIGFILE
fi

cat <<EOF >> $CONFIGFILE

####################################################
# Uncommenting this string enables display of freed disk
# space if localepurge has purged any superfluous data:

EOF

db_get localepurge/showfreedspace

if [ "$RET" = "true" ]; then
    echo SHOWFREEDSPACE >> $CONFIGFILE
else
    echo '#SHOWFREEDSPACE' >> $CONFIGFILE
fi

cat <<EOF >> $CONFIGFILE

#####################################################
# Commenting out this string enables faster but less
# accurate calculation of freed disk space:

EOF

db_get localepurge/quickndirtycalc

if [ "$RET" = "true" ]; then
    echo '#QUICKNDIRTYCALC' >> $CONFIGFILE
else
    echo QUICKNDIRTYCALC >> $CONFIGFILE
fi

cat <<EOF >> $CONFIGFILE

#####################################################
# Commenting out this string disables verbose output:

EOF

db_get localepurge/verbose

if [ "$RET" = "true" ]; then
    echo VERBOSE >> $CONFIGFILE
else
    echo '#VERBOSE' >> $CONFIGFILE
fi
 
db_get localepurge/nopurge

if [ "$RET" = "" ] || [ "$RET" = "PURGE_ALL" ]; then
    db_get localepurge/none_selected
    if [ "$RET" = "false" ]; then
    db_set localepurge/nopurge NEEDSCONFIGFIRST
      cat <<EOF >> $CONFIGFILE

#################################################################
# Unless configured, the system's locale directories won't be
# touched. Execute "dpkg-reconfigure localepurge" to reconfigure 
# localepurge to make it actually start functioning.
#
# WARNING: Only commenting out the marker line "NEEDSCONFIGFIRST"
#          without having defined *any* locale files to keep below
#          will result in deletion of *all* locale files present
#          on the system. So *first* define which locale files you 
#          want to keep before commenting out the following line.

EOF
    elif [ "$RET" = "yes" ]; then
      db_set localepurge/nopurge PURGE_ALL
      cat <<EOF >> $CONFIGFILE

#################################################################
# It has explicitly been requested to delete *all* locale files 
# present on the system and this will result in the definite loss 
# of all locale data in "/usr/share/locale/". You should know what
# you are doing or reconfigure "localepurge" as described above.
#################################################################

EOF
    fi
else
    cat <<EOF >> $CONFIGFILE

#####################################################
# Following locales won't be deleted from this system
# after package installations done with apt-get(8):

EOF
fi

db_get localepurge/nopurge
echo "$RET" | grep -v PURGE_ALL | sed 's/,//g' | tr ' ' '\n' >> $CONFIGFILE
