#!/bin/bash
#
# NOTE: mymcpu and mygpu are being deprecated for offload-arch

# mymcpu:  Print the mcpu value for the current machine
# mygpu:   Print a gpu value acceptable as gpu-arch for cuda clang
#
# Written by Greg Rodgers Gregory.Rodgers@amd.com

PROGVERSION=5.5.3

# Copyright(C) 2018 Advanced Micro Devices, Inc. All rights reserved.
# 
# AMD is granting you permission to use this software and documentation (if any) (collectively, the 
# Materials) pursuant to the terms and conditions of the Software License Agreement included with the 
# Materials.  If you do not have a copy of the Software License Agreement, contact your AMD 
# representative for a copy.
# 
# You agree that you will not reverse engineer or decompile the Materials, in whole or in part, except for 
# example code which is provided in source code form and as allowed by applicable law.
# 
# WARRANTY DISCLAIMER: THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
# KIND.  AMD DISCLAIMS ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING BUT NOT 
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
# PURPOSE, TITLE, NON-INFRINGEMENT, THAT THE SOFTWARE WILL RUN UNINTERRUPTED OR ERROR-
# FREE OR WARRANTIES ARISING FROM CUSTOM OF TRADE OR COURSE OF USAGE.  THE ENTIRE RISK 
# ASSOCIATED WITH THE USE OF THE SOFTWARE IS ASSUMED BY YOU.  Some jurisdictions do not 
# allow the exclusion of implied warranties, so the above exclusion may not apply to You. 
# 
# LIMITATION OF LIABILITY AND INDEMNIFICATION:  AMD AND ITS LICENSORS WILL NOT, 
# UNDER ANY CIRCUMSTANCES BE LIABLE TO YOU FOR ANY PUNITIVE, DIRECT, INCIDENTAL, 
# INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM USE OF THE SOFTWARE OR THIS 
# AGREEMENT EVEN IF AMD AND ITS LICENSORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH 
# DAMAGES.  In no event shall AMD's total liability to You for all damages, losses, and 
# causes of action (whether in contract, tort (including negligence) or otherwise) 
# exceed the amount of $100 USD.  You agree to defend, indemnify and hold harmless 
# AMD and its licensors, and any of their directors, officers, employees, affiliates or 
# agents from and against any and all loss, damage, liability and other expenses 
# (including reasonable attorneys' fees), resulting from Your use of the Software or 
# violation of the terms and conditions of this Agreement.  
# 
# U.S. GOVERNMENT RESTRICTED RIGHTS: The Materials are provided with "RESTRICTED RIGHTS." 
# Use, duplication, or disclosure by the Government is subject to the restrictions as set 
# forth in FAR 52.227-14 and DFAR252.227-7013, et seq., or its successor.  Use of the 
# Materials by the Government constitutes acknowledgement of AMD's proprietary rights in them.
# 
# EXPORT RESTRICTIONS: The Materials may be subject to export restrictions as stated in the 
# Software License Agreement.
# 

function usage(){
/bin/cat 2>&1 <<"EOF"

   WARNING: mymcpu and mygpu are being deprecated.
   Please use offload-arch instead.

EOF
   exit 1
}

function version(){
   echo $PROGVERSION
   exit 0
}

function getdname(){
   local __DIRN=`dirname "$1"`
   if [ "$__DIRN" = "." ] ; then
      __DIRN=$PWD;
   else
      if [ ${__DIRN:0:1} != "/" ] ; then
         if [ ${__DIRN:0:2} == ".." ] ; then
               __DIRN=`dirname $PWD`/${__DIRN:3}
         else
            if [ ${__DIRN:0:1} = "." ] ; then
               __DIRN=$PWD/${__DIRN:2}
            else
               __DIRN=$PWD/$__DIRN
            fi
         fi
      fi
   fi
   echo $__DIRN
}

TYPERUN=${0##*\/}
MODE=

unknown_value="unknown"
#  process args
while [ $# -gt 0 ] ; do
   case "$1" in
      -n) 	        MODE=num; shift ;;
      -h) 	        usage ;;
      -d) 	        unknown_value=$2; shift ;;
      -help) 	        usage ;;
      --help) 	        usage ;;
      -version) 	version ;;
      --version) 	version ;;
      --) 		shift ; break;;
      *) 		break;echo $1 ignored;
   esac
   shift
done

rc=0
thisdir=$(getdname $0)
oa="$thisdir/offload-arch"
if [ ! -f $oa ] ; then 
  oa=$AOMP/bin/offload-arch
fi
if [ ! -f $oa ] ; then
  oa=$thisdir/bin/amdgpu-arch
fi
if [ ! -f $oa ] ; then
  oa=$AOMP/bin/amdgpu-arch
fi
if [ ! -f $oa ] ; then
  echo "ErrorMissingTool"
  exit 1
fi
if [ "$TYPERUN" == "mygpu" ] ; then
   gpuname=`$oa`
   if [ "$gpuname" == "" ] ; then 
      gpuname="$unknown_value"
      rc=1
   fi
   gpuname=`echo $gpuname | cut -d" " -f1`
   if [ -z "$MODE" ]; then
     echo $gpuname
   else
     echo `echo $gpuname | sed 's/gfx//' | sed 's/sm_//'`
   fi
else
   # This is mymcpu so print the codename
   codename=`$oa -m | cut -d" " -f2`
   if [ "$codename" == "" ] ; then 
      codename="$unknown_value"
      rc=1
   fi
   codename=`echo $codename | cut -d" " -f1`
   echo $codename
fi

exit $rc
