#!/bin/sh
#*=====================================================================*/
#*    serrano/prgm/project/bigloo/bigloo/autoconf/oscharset            */
#*    -------------------------------------------------------------    */
#*    Author      :  Manuel Serrano                                    */
#*    Creation    :  Thu Jan  1 05:48:37 2009                          */
#*    Last change :  Mon Jul 10 13:56:13 2023 (serrano)                */
#*    Copyright   :  2009-23 Manuel Serrano                            */
#*    -------------------------------------------------------------    */
#*    Test the current OS filesystem charset.                          */
#*=====================================================================*/

#*---------------------------------------------------------------------*/
#*    flags                                                            */
#*---------------------------------------------------------------------*/
cflags=

#*---------------------------------------------------------------------*/
#*    We parse the arguments                                           */
#*---------------------------------------------------------------------*/
while : ; do
  case $1 in
    "")
      break;;

    --cflags=*|-cflags=*)
      cflags="`echo $1 | sed 's/^[-a-z]*=//'`";;

    -*)
      echo "Unknown option \"$1\", ignored" >&2;;
  esac
  shift
done

file=$TMP/actest$USER
aout=$TMP/Xactest$USER

host_locale=""

#*---------------------------------------------------------------------*/
#*    compile                                                          */
#*---------------------------------------------------------------------*/
compile="$CC $cflags $file.c -o $aout >/dev/null"

#*---------------------------------------------------------------------*/
#*    The test C file                                                  */
#*---------------------------------------------------------------------*/
if test -f $file.c; then
   rm -f $file.c || exit $?
fi

#*---------------------------------------------------------------------*/
#*    Test                                                             */
#*---------------------------------------------------------------------*/
cat > $file.c <<EOF
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[], char *envp[])
{
   char *locale;
   locale = getenv("LOCALE");
   if (locale)
     printf("%s", locale);
   return 0;
}
EOF

#*---------------------------------------------------------------------*/
#*    Compilation test                                                 */
#*---------------------------------------------------------------------*/
if eval "$BUILDSH $compile"; then
   \rm -f $file.*
   host_locale=`eval "$HOSTSH $aout"`
   res=$?;

   rm -rf $aout*

   if [ $res != 0 ]; then
     exit 1
   fi
else
   ret_code=$?
   \rm -f $file.*
   exit $ret_code
fi

if [ "$host_locale " != " " ]; then
  case $LOCALE in
    *.utf8)
      echo "UTF-8";;
    *)
      echo $LOCALE;;
  esac
else
  case $HOSTOS in
    linux)
      echo "C";;
    cygwin)
      echo "C";;
    mingw)
      echo "C";;
    *)
      echo "C";;
  esac
fi
