#!/bin/rc
#//////////////////////////////////////////////////////////////////////////////
#
# Copyright (c) 2007-2022 Daniel Adler <dadler@uni-goettingen.de>, 
#                         Tassilo Philipp <tphilipp@potion-studios.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#//////////////////////////////////////////////////////////////////////////////

# --- configure settings ------------------------------------------------------

PACKAGE=dyncall


# --- message helpers ----------------------------------------------------
fn error {
  echo error: $*
  exit 1
}
fn warning {
  echo warning: $*
}
fn info {
  echo $*
}


# --- print usage -------------------------------------------------------------

fn usage {
  echo $PACKAGE' configuration shell-script'
  echo 'Usage:'
  echo '  configure [ options ]'
  echo
  echo 'Options:'
  echo '  --help'
  echo '   -h                print this page'
  #echo
  #echo '  --target-x86       build for x86 architecture platform'
  #echo '  --target-x64       build for x64 architecture platform'
  #echo '  --target-ppc32     build for ppc32 architecture platform'
  #echo '  --target-arm-arm   build for ARM architecture platform (ARM mode)'
  #echo '  --target-arm-thumb build for ARM architecture platform (THUMB mode)'
  #echo
  #echo '  --tool-pcc         use Portable C Compiler'
  #echo 
  #echo '  --asm-xa           use Plan9 Assemblers (0a, 1a, 2a, etc.)'
  echo
  exit 0
}


# --- guess os, arch, ... -------------------------------------------------

CONFIG_OS=()
fn guess_os {
  CONFIG_OS='plan9'
  info 'guess os '$CONFIG_OS
}

CONFIG_ARCH=()
fn guess_arch {
  CONFIG_ARCH='x86'
  switch($cputype) {
    case 386
      CONFIG_ARCH='x86'
    case amd64
      CONFIG_ARCH='x64'
    case arm
      CONFIG_ARCH='arm32_arm'
    case power
      CONFIG_ARCH='ppc32'
    case mips
      CONFIG_ARCH='mips32'
    case *
      warning 'unknown architecture '$cputype' - using default (x86)'
  }
  info 'guess arch '$CONFIG_ARCH
}

CONFIG_TOOL=()
fn guess_tool {
  #CONFIG_TOOL=`{grep CC /$objtype/mkfile | sed s/CC.//}
  CONFIG_TOOL='pcc'
  info 'guess tool '$CONFIG_TOOL
}

CONFIG_ASM=()
fn guess_asm {
  CONFIG_ASM=`{grep AS /$objtype/mkfile | sed s/AS.//}
  info 'guess assembler '$CONFIG_ASM
}

fn guess {
  if(~ $#CONFIG_OS 0) {
    guess_os
    if(~ $#CONFIG_OS 0) {
      error 'unsupported operating system '$OS
    }
  }

  if(~ $#CONFIG_ARCH 0) {
    guess_arch 
    if(~ $#CONFIG_ARCH 0) {
      error 'unsupported architecture '$ARCH
    }
  }

  if(~ $#CONFIG_TOOL 0) {
    guess_tool
    if(~ $#CONFIG_TOOL 0) {
      error 'no tool'
    }
  }

  if(~ $#CONFIG_ASM 0) {
    guess_asm
    if(~ $#CONFIG_ASM 0) {
      error 'no assembler tool'
    }
  }
}


# --- process arguments ------------------------------------------------------

fn args {
  while(! ~ $#* 0 && ~ $1 -*) {
    OPT=$1
    shift

    switch($OPT) {
      case --help -h
        usage
      #case --target-x86
        #CONFIG_ARCH='x86'
      #case --target-x64
        #CONFIG_ARCH='x64'
      #case --target-ppc32
        #CONFIG_ARCH='ppc32'
      #case --target-arm-arm
        #CONFIG_ARCH='arm32_arm'
      #case --target-arm-thumb
        #CONFIG_ARCH='arm32_thumb'
      #case --tool-pcc
        #CONFIG_TOOL=pcc
        #CONFIG_ASM=2a
      #case --tool-xa
        #CONFIG_ASM=2a
      case *
        warning 'unknown option '$OPT
    }
  }
}



args $*
info '* configure package '$PACKAGE
guess

CONFIG_HOST=$CONFIG_OS
BUILD_HOST=$CONFIG_HOST
if(! ~ $CONFIG_HOST $CONFIG_OS) {
  BUILD_HOST=$CONFIG_HOST'_'$CONFIG_OS
}

CONFFILE=Makefile.config
echo '#auto-generated by '$PACKAGE'/configure' >$CONFFILE
echo 'CONFIG_PACKAGE='$PACKAGE >>$CONFFILE
echo 'CONFIG_HOST='$CONFIG_HOST >>$CONFFILE
echo 'CONFIG_OS='$CONFIG_OS >>$CONFFILE
echo 'CONFIG_ARCH='$CONFIG_ARCH >>$CONFFILE
echo 'CONFIG_TOOL='$CONFIG_TOOL >>$CONFFILE
echo 'CONFIG_ASM='$CONFIG_ASM >>$CONFFILE
echo '' >>$CONFFILE

