1#!/bin/sh
2
3# Public domain notice for all NCBI EDirect scripts is located at:
4# https://www.ncbi.nlm.nih.gov/books/NBK179288/#chapter6.Public_Domain_Notice
5
6# determine current platform
7platform=""
8osname=`uname -s`
9cputype=`uname -m`
10case "$osname-$cputype" in
11  Linux-x86_64 )           platform=Linux ;;
12  Darwin-x86_64 )          platform=Darwin ;;
13  Darwin-*arm* )           platform=Silicon ;;
14  CYGWIN_NT-* | MINGW*-* ) platform=CYGWIN_NT ;;
15  Linux-*arm* )            platform=ARM ;;
16  * )                      platform=UNSUPPORTED ;;
17esac
18
19canBuildSilicon=false
20
21gv=$( go version )
22case "$gv" in
23  # only build for Apple Silicon if using Go compiler 1.16 or later
24  *go1.16* | *go1.17* | *go1.18* )
25    canBuildSilicon=true
26    ;;
27  * )
28    ;;
29esac
30
31crossCompileAll=false
32install=false
33cleanup=true
34target=""
35
36# process optional command-line arguments
37while [ "$#" -ne 0 ]
38do
39  case "$1" in
40    -install | install )
41      # install native executables on development machine
42      install=true
43      # default executable path
44      target="$HOME/Misc/scripts/"
45      shift
46      ;;
47    -desktop | desktop )
48      # place native executables on desktop
49      install=true
50      target="$HOME/Desktop/"
51      shift
52      ;;
53    -silicon | silicon )
54      # coerce platform to create Silicon executables
55      platform=Silicon
56      install=true
57      target="$HOME/Desktop/"
58      # but do not remove existing native binaries on deskop
59      cleanup=false
60      shift
61      ;;
62    -distrib | -distribute | distrib | distribute )
63      # cross-compile all versions for ftp distribution
64      crossCompileAll=true
65      install=true
66      # default distribution path
67      target="$HOME/goxtract/"
68      shift
69      ;;
70    * )
71      if [ -n "$1" ]
72      then
73        # allow override of default target path
74        install=true
75        target="$1"
76      fi
77      # break out of loop
78      break
79      ;;
80  esac
81done
82
83# create module files
84if [ ! -f "go.mod" ]
85then
86  go mod init edirect
87  # add explicit location to find local helper package
88  echo "replace eutils => ../eutils" >> go.mod
89fi
90if [ ! -f "go.sum" ]
91then
92  go mod tidy
93fi
94
95# erase any existing executables in current directory
96for plt in Darwin Silicon Linux CYGWIN_NT ARM
97do
98  rm -f *.$plt
99done
100
101# platform-specific compiler environment variable values
102mods="darwin amd64 Darwin \
103      darwin arm64 Silicon \
104      linux amd64 Linux \
105      windows 386 CYGWIN_NT \
106      linux arm ARM"
107
108# build all executables for each selected platform
109echo "$mods" |
110xargs -n 3 sh -c 'echo "$0 $1 $2"' |
111while read os ar pl
112do
113  if [ "$pl" != "$platform" ] && [ "$crossCompileAll" = false ]
114  then
115    continue
116  fi
117  if [ "$pl" = "Silicon" ] && [ "$canBuildSilicon" = false ]
118  then
119    continue
120  fi
121  for exc in xtract rchive transmute
122  do
123    env GOOS="$os" GOARCH="$ar" go build -o "$exc.$pl" "$exc.go"
124  done
125done
126
127if [ "$install" = true ] && [ -n "$target" ]
128then
129  if [ "$cleanup" = true ]
130  then
131    # remove old executables from target
132    for plt in Darwin Silicon Linux CYGWIN_NT ARM
133    do
134      rm -f $target/*.$plt
135    done
136  fi
137  # copy new executables to target
138  for plt in Darwin Silicon Linux CYGWIN_NT ARM
139  do
140    for exc in xtract rchive transmute
141    do
142      if [ -f "$exc.$plt" ]
143      then
144        mv -f "$exc.$plt" "$target"
145      fi
146    done
147  done
148fi
149
150# erase any remaining executables after compiling
151for plt in Darwin Silicon Linux CYGWIN_NT ARM
152do
153  rm -f *.$plt
154done
155