1 #! /bin/sh 2 3 # $NetBSD: grab-version.sh,v 1.3 2026/07/10 21:54:30 kre Exp $ 4 5 # Free unlicensed public-domain code. 6 # Found under a bush behind an outdoor dunny in Wagga Wagga (mid 1800's). 7 # Your risk entirely if you use it. 8 9 case "$#" in 10 1) ;; 11 *) printf >&2 'Usage: %s NETBSDSRCDIR\n' "${0##*/}"; exit 1;; 12 esac 13 14 # GEN='$NetBSD: grab-version.sh,v 1.3 2026/07/10 21:54:30 kre Exp $' 15 # GEN=${GEN#'$NetBSD: '} 16 # GEN=${GEN%' Exp $'} 17 18 VERSION_FILE="${1}/sys/sys/param.h" 19 20 : ${SED:=sed} 21 : ${DATE:=date} 22 23 if ( O=$(eval "printf %s $'X\tY'") && [ "$O" = 'X Y' ] ) >/dev/null 2>&1 24 then 25 # Use this when possible, it is more obvious what is happening. 26 VERS_MATCH=$'^#define[ \t][ \t]*__NetBSD_Version__[ \t][ \t]*[1-9]' 27 IFS=$' \t' 28 else 29 # The same as above, avoiding use of $'...' (for old shells) 30 VERS_MATCH='^#define[ ][ ]*__NetBSD_Version__[ ][ ]*[1-9]' 31 IFS=' ' 32 fi 33 34 VL=$( "${SED}" -n \ 35 -e "/${VERS_MATCH}/ {" \ 36 -e p \ 37 -e q \ 38 -e } "${VERSION_FILE}" 39 ) || 40 { 41 printf >&2 '%s: No __NetBSD_Version__ in %s\n' \ 42 "${0##*/}" "${VERSION_FILE}" 43 exit 1 44 } 45 46 # See IFS setting above (this is the sole occasion it is used) 47 read -r def nbv version comment <<!EOF! 48 ${VL} 49 !EOF! 50 51 format() 52 { 53 printf >&2 '%s: Badly formatted version line from %s:\n\t%s\n' \ 54 "${0##*/}" "${VERSION_FILE}" "${VL}" 55 exit 1 56 } 57 58 if [ "${def}" != '#define' ] || [ "${nbv}" != __NetBSD_Version__ ] 59 then 60 format 61 fi 62 63 comment=${comment#* } 64 comment=${comment% *} 65 case "${comment}" in 66 *[/*]*) format ;; 67 'NetBSD '[1-9]*) comment="${comment#NetBSD }";; 68 *) format ;; 69 esac 70 71 case "${version}" in 72 *[!0-9]*) format ;; 73 [1-9]*[0-9]) ;; 74 *) format ;; 75 esac 76 77 printf "'%s'\n" '-DUSER_OSTYPE_VALUE="NetBSD"' 78 printf "'%s'\n" "-DUSER_OSREVISION_VALUE=${version}" 79 printf "'-DUSER_OSRELEASE_VALUE=\"%s\"'\n" "${comment}" 80 printf "'-DUSER_BUILDTIME_VALUE=\"%s%s\"'\n" \ 81 "$( TZ=UTC "${DATE}" -u \ 82 ${MKREPRO_TIMESTAMP:+-r "${MKREPRO_TIMESTAMP}"} \ 83 '+%Y-%m-%d %T UTC' )" \ 84 "${MKREPRO_TIMESTAMP:+ !}" 85