#!/bin/sh -e

#set -x

. /usr/share/debconf/confmodule

# first get all possible locales and create a full list of locale values:

TEMPFILE=$(tempfile).$$
LOCALELIST=/var/cache/localepurge/localelist

# for the purpose of presenting a ready made preselection at the very first
# configuration, include already configured locales from locales package:

LOCALEGEN=$(tempfile).locale.gen
touch "$LOCALEGEN"

if [ -f /etc/locale.gen ]; then
    grep ^[a-z] /etc/locale.gen | cut -d" " -f1 > "$LOCALEGEN"
    # add double character locale names from underscore variations
	# for later preselection:
    cut -d"_" -f1 "$LOCALEGEN" | sort -u >> "$LOCALEGEN"
fi

# if it already exists, include locales from /etc/locale.nopurge:

if [ -f /etc/locale.nopurge ]; then
    grep ^[a-z] /etc/locale.nopurge | cut -d" " -f1 >> "$LOCALEGEN"
fi

# add values from "$LOCALEGEN" to "$TEMPFILE":

cat "$LOCALEGEN" >> "$TEMPFILE"

# include locales supported by the locales package:

if [ -f /usr/share/i18n/SUPPORTED ]; then
    grep ^[a-z] /usr/share/i18n/SUPPORTED | cut -d' ' -f1 | sort -u >> "$TEMPFILE"
fi

# include locales from our previous localelist if it already exists:

if [ -f "$LOCALELIST" ]; then
    cat "$LOCALELIST" >> "$TEMPFILE"
fi

# include locales from newly added locales:
NEWLOCALELIST="$LOCALELIST"-new

if [ -f "$NEWLOCALELIST" ] && \
   [ $(ps w -p "$PPID" | grep -c dpkg-reconfigure) = "1" ]; then
    cat "$NEWLOCALELIST" >> "$TEMPFILE"
    rm -f "$NEWLOCALELIST"
fi

# creating double character locale names from underscore variations:

cut -d"_" -f1 "$TEMPFILE" | sort -u >> "$TEMPFILE"

# save full list of locale values into locale list:

if [ ! -f "$LOCALELIST" ]; then
    if [ ! -d /var/cache/localepurge ]; then
	    mkdir -m 755 /var/cache/localepurge
    fi
    sort -u "$TEMPFILE" > "$LOCALELIST"
   else
    mv "$LOCALELIST" "$LOCALELIST"-old
    sort -u "$TEMPFILE" > "$LOCALELIST"
fi 

# finally sort and create full list of all collected locale names

LOCALES=$(sort -u "$TEMPFILE" | tr '\n' ' ' \
         | sed 's/\ /,\ /g' | sed 's/,\ $//g')

# sort and create preselection values from "$LOCALEGEN"

PRESELECT=$(sort -u "$LOCALEGEN" | tr '\n' ' ' \
         | sed 's/\ /,\ /g' | sed 's/,\ $//g')

# deleting temporary files not needed anymore:

rm -f "$TEMPFILE" "$LOCALEGEN"

#############################################################
# now that all locale data is in place let debconf take over:

db_subst localepurge/nopurge locales "$LOCALES"

# uncomment for debugging:
#echo "$LOCALES" > /tmp/locales.list

db_get localepurge/nopurge
if [ "$RET" = "" ] && [ "$RET" != "PURGE_ALL" ] \
   || [ "$RET" = "NEEDSCONFIGFIRST" ]; then
       db_set localepurge/nopurge "$PRESELECT"
       db_fset localepurge/nopurge seen false
       # uncomment for debugging:
       #echo "$PRESELECT" > /tmp/preselect.list
fi

db_input high localepurge/nopurge || true
db_go

db_get localepurge/nopurge
if [ "$RET" = "" ] || [ "$RET" = "PURGE_ALL" ]; then
    db_input high localepurge/none_selected || true
    db_go

    db_get localepurge/none_selected
    if [ "$RET" = "false" ]; then
        db_input high localepurge/remove_no || true
	    db_go
    fi
fi

db_input medium localepurge/mandelete || true
db_go

db_input medium localepurge/dontbothernew || true
db_go

db_input low localepurge/showfreedspace || true
db_go

db_get localepurge/showfreedspace
if [ "$RET" = "true" ]; then
    db_input low localepurge/quickndirtycalc || true
    db_go
fi

db_input low localepurge/verbose || true
db_go
