| 1 | {{{testport}}} is a Bash script to test install a specified port in a custom MacPorts location in the {{{/opt}}} directory. |
| 2 | |
| 3 | The default location is {{{/opt/macports-test}}}. |
| 4 | |
| 5 | ---- |
| 6 | |
| 7 | {{{ |
| 8 | |
| 9 | #!/bin/bash |
| 10 | |
| 11 | # testport -- test install a specified port in /opt/macports-test (default) |
| 12 | |
| 13 | # 1. move the /opt/local MacPorts system to ${opt_local_off} to make sure it is not interfering with the custom ${MP_PREFIX} MacPorts build process |
| 14 | # 2. move /usr/local to ${usr_local_off} to make sure it is not interfering with the custom ${MP_PREFIX} MacPorts build process |
| 15 | # 3. install a fresh MacPorts 2.0.4 system to "${MP_PREFIX}" |
| 16 | # 4. install the specified port to the custom ${MP_PREFIX} MacPorts system |
| 17 | |
| 18 | # Small getopts tutorial, |
| 19 | # http://wiki.bash-hackers.org/howto/getopts_tutorial |
| 20 | |
| 21 | # usage: |
| 22 | # testport gawk |
| 23 | # testport -n -l /opt/macports-gnu gawk |
| 24 | |
| 25 | |
| 26 | show_usage() { |
| 27 | echo |
| 28 | echo "$(basename $0) -- test install a specified port in /opt/macports-test (default)" |
| 29 | echo |
| 30 | echo "Usage: $(basename $0) [-d] [-e] [-h] [-n] [-p] [-r] [-s] [-u] [-v] [-l dir] portname" |
| 31 | echo ' |
| 32 | -d: enable debug mode |
| 33 | -e: fetch & extract the distribution files for portname |
| 34 | -h: help |
| 35 | -n: delete /opt/macports-test (default) & perform a new MacPorts install from scratch |
| 36 | -p: print PATH variable value |
| 37 | -r: remove / uninstall specified port before reinstalling it |
| 38 | -s: build and install from source only |
| 39 | -u: update MacPorts system and uprade outdated ports |
| 40 | -v: enable verbose mode |
| 41 | -l dir: specify dir as location of MacPorts system (otherwise defaults to /opt/macports-test) |
| 42 | ' |
| 43 | return 0 |
| 44 | } |
| 45 | |
| 46 | |
| 47 | trapcount=0 |
| 48 | # cf. http://fvue.nl/wiki/Bash:_Error_handling |
| 49 | on_exit() { |
| 50 | #set -xv |
| 51 | trapcount=$((trapcount + 1)) |
| 52 | if [[ $trapcount -eq 1 ]]; then |
| 53 | cd # avoid: sudo: cannot get working directory |
| 54 | #find -x "${tmpDir}" -ls 1>&2 |
| 55 | echo |
| 56 | [[ -d "${tmpDir}" ]] && rm -rf "${tmpDir}" |
| 57 | [[ -d "${usr_local_off}" ]] && sudo mv -iv "${usr_local_off}" /usr/local |
| 58 | [[ -d "${opt_local_off}" ]] && sudo mv -iv "${opt_local_off}" /opt/local |
| 59 | printf "\n\n\n" |
| 60 | echo dscl . -change /Users/macports NFSHomeDirectory "${MP_PREFIX}/var/macports/home" "${dsclHome}" |
| 61 | current_dscl_home="$(dscl . -read /Users/macports NFSHomeDirectory | sed 's/^NFSHomeDirectory: *//')" |
| 62 | if [[ "${current_dscl_home}" != "${dsclHome}" ]]; then |
| 63 | dscl . -change /Users/macports NFSHomeDirectory "${MP_PREFIX}/var/macports/home" "${dsclHome}" |
| 64 | fi |
| 65 | printf '\n\n%s\n\n\n' "export PATH=\"${MP_PREFIX}/bin:${MP_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin\"" |
| 66 | fi |
| 67 | exit |
| 68 | } |
| 69 | |
| 70 | unset CDPATH PATH IFS LC_ALL MP_PREFIX all_new |
| 71 | IFS=$' \t\n' |
| 72 | LC_ALL=C |
| 73 | PATH='/sbin:/usr/bin:/bin:/usr/sbin:/sbin' |
| 74 | export IFS LC_ALL PATH |
| 75 | |
| 76 | |
| 77 | MP_PREFIX="" |
| 78 | all_new=0 |
| 79 | update=0 |
| 80 | remove=0 |
| 81 | verbose=0 |
| 82 | debug=0 |
| 83 | printpath=0 |
| 84 | extract=0 |
| 85 | build_source=0 |
| 86 | clean_all=0 |
| 87 | |
| 88 | |
| 89 | while getopts ":cdehl:nprsuv" opt; do |
| 90 | case "$opt" in |
| 91 | c) |
| 92 | clean_all=1 |
| 93 | ;; |
| 94 | d) |
| 95 | debug=1 |
| 96 | ;; |
| 97 | e) |
| 98 | extract=1 |
| 99 | ;; |
| 100 | h) |
| 101 | show_usage |
| 102 | exit |
| 103 | ;; |
| 104 | l) |
| 105 | MP_PREFIX="$OPTARG" |
| 106 | ;; |
| 107 | n) |
| 108 | all_new=1 |
| 109 | ;; |
| 110 | p) |
| 111 | printpath=1 |
| 112 | ;; |
| 113 | r) |
| 114 | remove=1 |
| 115 | ;; |
| 116 | d) |
| 117 | build_source=1 |
| 118 | ;; |
| 119 | u) |
| 120 | update=1 |
| 121 | ;; |
| 122 | v) |
| 123 | verbose=1 |
| 124 | ;; |
| 125 | \?) |
| 126 | echo "Invalid option: -$OPTARG" 1>&2 |
| 127 | show_usage 1>&2 |
| 128 | exit 1 |
| 129 | ;; |
| 130 | :) |
| 131 | echo "Option -$OPTARG requires an argument." 1>&2 |
| 132 | show_usage 1>&2 |
| 133 | exit 1 |
| 134 | ;; |
| 135 | esac |
| 136 | done |
| 137 | |
| 138 | |
| 139 | shift $((OPTIND-1)) |
| 140 | |
| 141 | if [[ -z "$MP_PREFIX" ]]; then |
| 142 | MP_PREFIX='/opt/macports-test' |
| 143 | fi |
| 144 | |
| 145 | if [[ "$MP_PREFIX" == '/opt/local' ]]; then |
| 146 | echo 'Use of /opt/local is not allowed!' 1>&2 |
| 147 | exit 1 |
| 148 | fi |
| 149 | |
| 150 | if [[ "$MP_PREFIX" == '/' ]]; then |
| 151 | echo 'Use of / is not allowed!' 1>&2 |
| 152 | exit 1 |
| 153 | fi |
| 154 | |
| 155 | if [[ $(dirname "${MP_PREFIX}") != '/opt' ]]; then |
| 156 | echo 'Use: /opt/somedir' 1>&2 |
| 157 | exit 1 |
| 158 | fi |
| 159 | |
| 160 | if [[ ! -d "$MP_PREFIX" ]]; then |
| 161 | all_new=1 |
| 162 | fi |
| 163 | |
| 164 | if [[ $all_new -eq 1 ]]; then |
| 165 | clean_all=0 |
| 166 | remove=0 |
| 167 | update=0 |
| 168 | fi |
| 169 | |
| 170 | |
| 171 | declare -rx MP_PREFIX |
| 172 | |
| 173 | unset PATH |
| 174 | PATH="${MP_PREFIX}/bin:${MP_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin" |
| 175 | export PATH |
| 176 | |
| 177 | if [[ $printpath -eq 1 ]]; then |
| 178 | printf '\n\n%s\n\n\n' "export PATH=\"${MP_PREFIX}/bin:${MP_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin\"" |
| 179 | exit |
| 180 | fi |
| 181 | |
| 182 | if [[ $all_new -ne 1 ]] && [[ ! -x "${MP_PREFIX}/bin/port" ]]; then |
| 183 | echo "No port command found at: ${MP_PREFIX}/bin/port" 1>&2 |
| 184 | exit 1 |
| 185 | fi |
| 186 | |
| 187 | |
| 188 | if [[ $# -eq 0 ]]; then |
| 189 | if [[ $clean_all -eq 1 ]]; then |
| 190 | ( port -f clean --all installed 2>/dev/null ) || true |
| 191 | exit |
| 192 | elif [[ $update -eq 1 ]]; then |
| 193 | port -f clean --all installed |
| 194 | port selfupdate |
| 195 | port outdated |
| 196 | ( port upgrade -R -u outdated 2>/dev/null ) || true |
| 197 | exit |
| 198 | else |
| 199 | echo |
| 200 | echo 'No port to install given!' |
| 201 | show_usage 1>&2 |
| 202 | exit 1 |
| 203 | fi |
| 204 | fi |
| 205 | |
| 206 | |
| 207 | if [[ $(id -u) -ne 0 ]] || [[ "${HOME}" != '/var/root' ]]; then |
| 208 | echo 'This script must be run in a root shell to prevent sudo timeout!' 1>&2 |
| 209 | echo 'Use: sudo -H -i' 1>&2 |
| 210 | exit 1 |
| 211 | fi |
| 212 | |
| 213 | |
| 214 | dsclHome="$(dscl . -read /Users/macports NFSHomeDirectory | sed 's/^NFSHomeDirectory: *//')" |
| 215 | dscl . -change /Users/macports NFSHomeDirectory "${dsclHome}" "${MP_PREFIX}/var/macports/home" |
| 216 | |
| 217 | |
| 218 | # make sure the current working directory exists |
| 219 | pwd -P 1>/dev/null || exit 1 |
| 220 | |
| 221 | |
| 222 | # prevent idle sleep |
| 223 | pmset -a force sleep 0 displaysleep 0 disksleep 0 |
| 224 | |
| 225 | |
| 226 | sleep 1 |
| 227 | unset usr_local_off opt_local_off tmpDir |
| 228 | usr_local_off="/usr/local-off-$(date '+%Y-%m-%d-%H_%M_%S')" |
| 229 | opt_local_off="/opt/local-off-$(date '+%Y-%m-%d-%H_%M_%S')" |
| 230 | |
| 231 | tmpDir="$(mktemp -d /tmp/macports.XXXXXX)" || exit 1 |
| 232 | |
| 233 | declare -rx usr_local_off opt_local_off tmpDir |
| 234 | |
| 235 | trap on_exit EXIT TERM HUP INT QUIT |
| 236 | |
| 237 | echo |
| 238 | |
| 239 | # make sure /usr/local is not interfering with MacPorts build processes for /opt/local |
| 240 | [[ -d '/usr/local' ]] && sudo mv -iv /usr/local "${usr_local_off}" |
| 241 | |
| 242 | # back up the old MacPorts system |
| 243 | [[ -d '/opt/local' ]] && sudo mv -iv /opt/local "${opt_local_off}" |
| 244 | |
| 245 | echo |
| 246 | |
| 247 | # clean up previous "${MP_PREFIX}" directory |
| 248 | if [[ -x "${MP_PREFIX}/bin/port" ]]; then |
| 249 | if [[ $clean_all -eq 1 ]]; then |
| 250 | ( port -f clean --all installed 2>/dev/null ) || true |
| 251 | else |
| 252 | ( port -f clean --work "$@" 2>/dev/null ) || true |
| 253 | fi |
| 254 | fi |
| 255 | |
| 256 | |
| 257 | if [[ $all_new -eq 1 ]]; then |
| 258 | |
| 259 | if [[ -x "${MP_PREFIX}/bin/port" ]]; then |
| 260 | ( port -f uninstall installed 2>/dev/null ) || true |
| 261 | fi |
| 262 | |
| 263 | # since "rm -rf" is a dangerous command, we restrict its use to /opt/somedir |
| 264 | if [[ -d "${MP_PREFIX}" ]] && [[ $(dirname "${MP_PREFIX}") == '/opt' ]]; then |
| 265 | rm -rf "${MP_PREFIX}" |
| 266 | fi |
| 267 | |
| 268 | # get a fresh MacPorts 2.0.4 install in ${MP_PREFIX} |
| 269 | |
| 270 | cd "${tmpDir}" || exit 1 |
| 271 | |
| 272 | # cf. http://guide.macports.org/#installing.macports.source.multiple |
| 273 | unset PATH |
| 274 | export PATH='/bin:/sbin:/usr/bin:/usr/sbin' |
| 275 | curl -L -O https://distfiles.macports.org/MacPorts/MacPorts-2.0.4.tar.bz2 || exit 1 |
| 276 | tar -xjf MacPorts-2.0.4.tar.bz2 |
| 277 | cd MacPorts-2.0.4 || exit 1 |
| 278 | #--with-install-user=$owner --with-install-group=$group --with-directory-mode=$perms" |
| 279 | ./configure --prefix="${MP_PREFIX}" --with-tclpackage="${MP_PREFIX}/tcl" --with-applications-dir="${MP_PREFIX}/Applications" |
| 280 | |
| 281 | make |
| 282 | make install |
| 283 | |
| 284 | # get the Portfiles and update the system |
| 285 | "${MP_PREFIX}/bin/port" selfupdate |
| 286 | |
| 287 | fi # all_new |
| 288 | |
| 289 | |
| 290 | cd "${tmpDir}" || exit 1 |
| 291 | |
| 292 | unset PATH |
| 293 | PATH="${MP_PREFIX}/bin:${MP_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin" |
| 294 | export PATH |
| 295 | |
| 296 | if [[ $update -eq 1 ]]; then |
| 297 | port -f clean --all installed |
| 298 | port selfupdate |
| 299 | port outdated |
| 300 | ( port upgrade -R -u outdated 2>/dev/null ) || true |
| 301 | fi |
| 302 | |
| 303 | |
| 304 | if [[ $remove -eq 1 ]]; then |
| 305 | port -f clean --all "$@" |
| 306 | port -f -v uninstall "$@" |
| 307 | fi |
| 308 | |
| 309 | |
| 310 | if [[ $verbose -eq 1 ]]; then |
| 311 | port -f -v install "$@" |
| 312 | elif [[ $debug -eq 1 ]]; then |
| 313 | port -f -d install "$@" |
| 314 | elif [[ $extract -eq 1 ]]; then |
| 315 | port -f clean --all "$@" |
| 316 | port -f extract "$@" |
| 317 | elif [[ $build_source -eq 1 ]] && [[ $verbose -eq 1 ]]; then |
| 318 | port -f clean --all "$@" |
| 319 | port -f -s -v install "$@" |
| 320 | elif [[ $build_source -eq 1 ]] && [[ $debug -eq 1 ]]; then |
| 321 | port -f -s -d install "$@" |
| 322 | elif [[ $build_source -eq 1 ]]; then |
| 323 | port -f -s install "$@" |
| 324 | else |
| 325 | port -f install "$@" |
| 326 | fi |
| 327 | |
| 328 | |
| 329 | exit 0 |
| 330 | |
| 331 | }}} |