sysinfogen.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/bin/sh
  2. # This file is part of Hercules.
  3. # http://herc.ws - http://github.com/HerculesWS/Hercules
  4. #
  5. # Copyright (C) 2014-2015 Hercules Dev Team
  6. #
  7. # Hercules is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. # Base Author: Haru @ http://herc.ws
  20. do_fail() {
  21. echo 'Error writing output file'
  22. exit 1
  23. }
  24. cleanstring() {
  25. if [ "$( echo "x x" | sed -e 's/[[:space:]]//g' )" = "x x" ]; then
  26. # Workaround for non-POSIX-compliant systems that lack [[:space:]] (Solaris)
  27. echo "$@" | sed -e 's/"/ /g' -e 's/[ ][ ]*/ /g' -e 's/^[ ]*//g' -e 's/[ ]*$//g'
  28. else
  29. echo "$@" | sed -e 's/"/ /g' -e 's/[[:space:]][[:space:]]*/ /g' -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*$//g'
  30. fi
  31. }
  32. if [ -z "$1" ]; then
  33. echo 'No output file specified'
  34. exit 1
  35. fi
  36. OUTFILE="$1"
  37. shift
  38. if ! touch "$OUTFILE"; then
  39. echo 'Cannot create output file'
  40. exit 1
  41. fi
  42. YEAR="$(date +%Y)"
  43. cat > "$OUTFILE" << EOF
  44. /**
  45. * This file is part of Hercules.
  46. * http://herc.ws - http://github.com/HerculesWS/Hercules
  47. *
  48. * Copyright (C) 2014-$YEAR Hercules Dev Team
  49. *
  50. * Hercules is free software: you can redistribute it and/or modify
  51. * it under the terms of the GNU General Public License as published by
  52. * the Free Software Foundation, either version 3 of the License, or
  53. * (at your option) any later version.
  54. *
  55. * This program is distributed in the hope that it will be useful,
  56. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  57. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  58. * GNU General Public License for more details.
  59. *
  60. * You should have received a copy of the GNU General Public License
  61. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  62. */
  63. // This file was automatically generated. Any edit to it will be lost.
  64. EOF
  65. [ $? -eq 0 ] || do_fail
  66. HERC_PLATFORM="$( uname -s )"
  67. HERC_CORES="0"
  68. HERC_CPU="Unknown"
  69. case $HERC_PLATFORM in
  70. Linux)
  71. DIST=''
  72. DESCRIPTION=''
  73. REV=''
  74. if type lsb_release >/dev/null 2>&1; then
  75. LSBINFO="$( lsb_release -a 2>/dev/null )"
  76. DIST="$( cleanstring "$( echo "$LSBINFO" | grep '^Distributor ID:' | cut -d: -f2- )" )"
  77. DESCRIPTION="$( echo "$LSBINFO" | grep '^Description:' | cut -d: -f2- | sed 's/Enterprise Linux Enterprise Linux/Oracle Linux/' )"
  78. fi
  79. if [ -z "$DIST" ] || [ "$DIST" = "Gentoo" ]; then
  80. if [ -f /etc/gentoo-release ]; then
  81. # "Gentoo Base System release 2.2"
  82. DIST='Gentoo'
  83. DESCRIPTION="$( head -n 1 /etc/gentoo-release )"
  84. REV=''
  85. elif [ -f /etc/arch-release ]; then
  86. # empty release file
  87. DIST='ArchLinux'
  88. DESCRIPTION=''
  89. REV=''
  90. elif [ -f /etc/vmware-release ]; then
  91. # "VMware ESX Server 3" or "VMware ESX 4.0 (Kandinsky)"
  92. DIST="VMWare"
  93. DESCRIPTION="$( head -n 1 /etc/vmware-release )"
  94. REV=''
  95. elif [ -f /etc/debian_version ]; then
  96. # "wheezy/sid"
  97. DIST="Debian"
  98. DESCRIPTION="Debian GNU/Linux $( head -n 1 /etc/debian_version )"
  99. REV=''
  100. elif [ -f /etc/fedora-release ]; then
  101. # "Fedora release 9 (Sulphur)"
  102. DIST="Fedora"
  103. DESCRIPTION="$( head -n 1 /etc/fedora-release )"
  104. REV=''
  105. elif [ -f /etc/knoppix_version ]; then
  106. # "3.2 2003-04-15"
  107. DIST="Knoppix"
  108. REV="Knoppix GNU/Linux $( head -n 1 /etc/knoppix_version )"
  109. DESCRIPTION=''
  110. elif [ -f /etc/mandriva-release ]; then
  111. # "Mandriva Linux release 2010.1 (Official) for x86_64"
  112. DIST="Mandriva"
  113. DESCRIPTION="$( head -n 1 /etc/mandriva-release )"
  114. REV=''
  115. elif [ -f /etc/mandrake-release ]; then
  116. # "Mandrakelinux release 10.1 (Community) for i586"
  117. DIST="Mandrake"
  118. DESCRIPTION="$( head -n 1 /etc/mandrake-release )"
  119. REV=''
  120. elif [ -f /etc/oracle-release ]; then
  121. # "Oracle Linux Server release 6.3"
  122. DIST="Oracle"
  123. DESCRIPTION="$( head -n 1 /etc/oracle-release )"
  124. REV=''
  125. elif [ -f /etc/centos-release ]; then
  126. # "CentOS Linux release 6.0 (Final)"
  127. DIST="CentOS"
  128. DESCRIPTION="$( head -n 1 /etc/centos-release )"
  129. REV=''
  130. elif [ -f /etc/redhat-release ]; then
  131. # "Red Hat Enterprise Linux Server release 5 (Tikanga)"
  132. DIST="RedHat"
  133. DESCRIPTION="$( head -n 1 /etc/redhat-release )"
  134. REV=''
  135. elif [ -f /etc/slackware-version ]; then
  136. DIST="Slackware"
  137. DESCRIPTION="$( head -n 1 /etc/slackware-version )"
  138. REV=''
  139. elif [ -f /etc/slackware-release ]; then
  140. DIST="Slackware"
  141. DESCRIPTION="$( head -n 1 /etc/slackware-release )"
  142. REV=''
  143. elif [ -f /etc/SuSE-release ]; then
  144. # "SUSE Linux Enterprise Server 11 (x86_64)"
  145. # Note: it may contain several extra lines
  146. DIST="SuSE"
  147. DESCRIPTION="$( head -n 1 /etc/SuSE-release )"
  148. REV=''
  149. elif [ -f /etc/trustix-release ]; then
  150. # "Trustix Secure Linux release 2.0 (Cloud)"
  151. DIST="Trustix"
  152. DESCRIPTION="$( head -n 1 /etc/trustix-release )"
  153. REV=''
  154. else
  155. DIST='Unknown'
  156. DESCRIPTION=''
  157. REV=''
  158. fi
  159. fi
  160. if [ -n "$DESCRIPTION" ]; then
  161. DIST="$DESCRIPTION"
  162. fi
  163. HERC_OSVERSION="$DIST"
  164. HERC_CPU="$( cat /proc/cpuinfo | grep "model name" | head -n 1 | cut -d: -f2- )"
  165. HERC_CORES="$( grep '^processor' /proc/cpuinfo | wc -l )"
  166. ;;
  167. Darwin)
  168. HERC_PLATFORM="Mac OS X"
  169. if type sw_vers >/dev/null 2>&1; then
  170. HERC_OSVERSION="$( sw_vers -productName ) $( sw_vers -productVersion ) $( sw_vers -buildVersion )"
  171. else
  172. HERC_OSVERSION="Unknown"
  173. fi
  174. if type system_profiler >/dev/null 2>&1; then
  175. HWDATA="$( system_profiler SPHardwareDataType )"
  176. HWDATA_CPU="$( echo "$HWDATA" | grep "Processor Name:" | cut -d: -f2- )"
  177. HWDATA_CPUSPEED="$( cleanstring "$( echo "$HWDATA" | grep "Processor Speed:" | cut -d: -f2- )" )"
  178. HERC_CORES="$( echo "$HWDATA" | grep "Total Number of Cores:" | cut -d: -f2- )"
  179. HERC_CPU="${HWDATA_CPU} (${HWDATA_CPUSPEED})"
  180. fi
  181. ;;
  182. SunOS)
  183. HERC_PLATFORM="Solaris"
  184. HERC_OSVERSION="${HERC_PLATFORM} $( uname -r ) ($( uname -p) $(uname -v))"
  185. ;;
  186. AIX)
  187. HERC_OSVERSION="AIX $( oslevel ) ($(`oslevel -r`))"
  188. ;;
  189. CYGWIN*)
  190. HERC_PLATFORM="Cygwin Windows"
  191. HERC_OSVERSION="$( cleanstring "$( uname -s )" )"
  192. HERC_CPU="$( cat /proc/cpuinfo | grep "model name" | head -n 1 | cut -d: -f2- )"
  193. HERC_CORES="$( grep '^processor' /proc/cpuinfo | wc -l )"
  194. ;;
  195. OpenBSD)
  196. HERC_OSVERSION="${HERC_PLATFORM} $( uname -r ) ($( uname -p) $(uname -v))"
  197. HERC_CPU="$( sysctl hw.model | cut -d= -f2- )"
  198. HERC_CORES="$( sysctl hw.ncpu | cut -d= -f2- )"
  199. ;;
  200. FreeBSD)
  201. HERC_OSVERSION="${HERC_PLATFORM} $( uname -r ) ($( uname -p))"
  202. HERC_CPU="$( sysctl hw.model | cut -d: -f2- )"
  203. HERC_CORES="$( sysctl hw.ncpu | cut -d: -f2- )"
  204. ;;
  205. NetBSD)
  206. HERC_OSVERSION="${HERC_PLATFORM} $( uname -r ) ($( uname -p))"
  207. HERC_CPU="$( sysctl hw.model | cut -d= -f2- )"
  208. HERC_CORES="$( sysctl hw.ncpu | cut -d= -f2- )"
  209. ;;
  210. *)
  211. HERC_OSVERSION="Unknown"
  212. ;;
  213. esac
  214. cat >> "$OUTFILE" << EOF
  215. // Platform (uname -s)
  216. #define SYSINFO_PLATFORM "$( cleanstring "${HERC_PLATFORM}" )"
  217. // Operating System version (Platform-dependent)
  218. #define SYSINFO_OSVERSION "$( cleanstring "${HERC_OSVERSION}" )"
  219. // CPU Model (Platform-dependent)
  220. #define SYSINFO_CPU "$( cleanstring "${HERC_CPU}" )"
  221. // CPU Cores (Platform-dependent)
  222. #define SYSINFO_CPUCORES ( $( cleanstring "${HERC_CORES}" ) )
  223. EOF
  224. [ $? -eq 0 ] || do_fail
  225. HERC_ARCH="$( uname -m )"
  226. cat >> "$OUTFILE" << EOF
  227. // OS Architecture (uname -m)
  228. #define SYSINFO_ARCH "$( cleanstring "${HERC_ARCH}" )"
  229. EOF
  230. [ $? -eq 0 ] || do_fail
  231. HERC_CFLAGS="$@"
  232. HERC_CFLAGS="$( echo "${HERC_CFLAGS}" | sed 's/"//g' )"
  233. cat >> "$OUTFILE" << EOF
  234. // Compiler Flags
  235. #define SYSINFO_CFLAGS "$( cleanstring "${HERC_CFLAGS}" )"
  236. EOF
  237. [ $? -eq 0 ] || do_fail
  238. HERC_VCSREV=""
  239. if [ -d .git ]; then
  240. HERC_VCSTYPE="VCSTYPE_GIT"
  241. if type git >/dev/null 2>&1; then
  242. HERC_VCSREV="$( git rev-parse HEAD )"
  243. else
  244. HERC_VCSREV="Unknown"
  245. fi
  246. elif [ -d .svn ]; then
  247. HERC_VCSTYPE="VCSTYPE_SVN"
  248. if type svnversion >/dev/null 2>&1; then
  249. HERC_VCSREV="$( svnversion )"
  250. else
  251. HERC_VCSREV="Unknown"
  252. fi
  253. else
  254. HERC_VCSTYPE="VCSTYPE_NONE"
  255. fi
  256. cat >> "$OUTFILE" << EOF
  257. // VCS Type
  258. #define SYSINFO_VCSTYPE ${HERC_VCSTYPE}
  259. // VCS Revision
  260. #define SYSINFO_VCSREV "$( cleanstring "${HERC_VCSREV}" )"
  261. EOF
  262. [ $? -eq 0 ] || do_fail