#!/bin/sh

CNFREPO="/tmp/.cnfrepo"
REGION_SELECT="/tmp/.cocon.wifireg"
REGION_ADD_SET="/tmp/.cocon.wifireg_repoadd"

# set-region : select or read country/region,
# and set to iw region.

if [ ! -e $CNFREPO ];
then
  exit
fi

# Read repository
eval "$( coconcnf_get -b -m "$CNFREPO" | grep "^COCON_REGION=" )"

# If COCON_REGION is not set, show select dialog
if [ -z "$COCON_REGION" ];
then
  # Show select dialog
  sysctl kernel.printk="2 4 1 2"
  cocon-region-select
  COCON_REGION="$( cat "$REGION_SELECT" )"
  rm -f "$REGION_SELECT"
  sysctl kernel.printk="7 4 1 7"

  # Save to repository
  echo "COCON_REGION=$COCON_REGION" > $REGION_ADD_SET
  coconcnf_parse -b -c "$REGION_ADD_SET" -m "$CNFREPO"
  rm -f "$REGION_ADD_SET"
fi

# Set Wireless region setting
iw reg set "$COCON_REGION"

exit 0
