77# or:
88# 'wget -qO- https://get.haskellstack.org/ | sh'
99#
10+ # By default, this installs 'stack' to '/usr/local/bin'.
11+ #
12+ # Arguments (use `... | sh -s - ARGUMENTS`)
13+ #
14+ # -q: reduce script's output
15+ # -f: force over-write even if 'stack' already installed
16+ # -d DESTDIR: change destination directory
17+ #
1018# Make pull requests at:
1119# https://github.com/commercialhaskell/stack/blob/master/etc/scripts/get-stack.sh
1220#
1321
1422HOME_LOCAL_BIN=" $HOME /.local/bin"
15- USR_LOCAL_BIN=" /usr/local/bin"
23+ DEFAULT_DEST=" /usr/local/bin/stack"
24+ DEST=" "
1625QUIET=" "
1726FORCE=" "
1827STACK_TEMP_DIR=
@@ -108,7 +117,7 @@ print_bindist_notice() {
108117 fi
109118}
110119
111- # Adds a ` sudo` prefix if sudo is available to execute the given command
120+ # Adds a ' sudo' prefix if sudo is available to execute the given command
112121# If not, the given command is run as is
113122sudocmd () {
114123 $( command -v sudo) " $@ "
@@ -454,15 +463,20 @@ install_from_bindist() {
454463 if ! tar xzf " $STACK_TEMP_DIR /$1 .bindist" -C " $STACK_TEMP_DIR /$1 " ; then
455464 die " Extract bindist failed"
456465 fi
457- if ! sudocmd install -c -o 0 -g 0 -m 0755 " $STACK_TEMP_DIR /$1 " /* /stack " $USR_LOCAL_BIN /stack" ; then
458- die " Install to $USR_LOCAL_BIN /stack failed"
466+ STACK_TEMP_EXE=" $STACK_TEMP_DIR /$( basename " $DEST " ) "
467+ mv " $STACK_TEMP_DIR /$1 " /* /stack " $STACK_TEMP_EXE "
468+ # First attempt to install 'stack' as current user, then try with sudo if it fails
469+ if ! install -c -m 0755 " $STACK_TEMP_EXE " " $( dirname " $DEST " ) " 2> /dev/null; then
470+ if ! sudocmd install -c -o 0 -g 0 -m 0755 " $STACK_TEMP_EXE " " $( dirname " $DEST " ) " ; then
471+ die " Install to $DEST failed"
472+ fi
459473 fi
460474
461475 post_install_separator
462- info " Stack has been installed to: $USR_LOCAL_BIN /stack "
476+ info " Stack has been installed to: $DEST "
463477 info " "
464478
465- check_usr_local_bin_on_path
479+ check_dest_on_path
466480}
467481
468482install_arm_binary () {
@@ -631,20 +645,26 @@ check_home_local_bin_on_path() {
631645}
632646
633647# Check whether /usr/local/bin is on the PATH, and print a warning if not.
634- check_usr_local_bin_on_path () {
635- if ! on_path " $USR_LOCAL_BIN " ; then
636- info " WARNING: '$USR_LOCAL_BIN ' is not on your PATH."
648+ check_dest_on_path () {
649+ if ! on_path " $( dirname $DEST ) " ; then
650+ info " WARNING: '$( dirname $DEST ) ' is not on your PATH."
637651 info " "
638652 fi
639653}
640654
641655# Check whether Stack is already installed, and print an error if it is.
642656check_stack_installed () {
643- if [ " $FORCE " != " true" ] && has_stack ; then
644- die " Stack $( stack_version) already appears to be installed at:
657+ if has_stack ; then
658+ if [ " $FORCE " = " true" ] ; then
659+ [ " $DEST " != " " ] || DEST=" $( stack_location) "
660+ else
661+ die " Stack $( stack_version) already appears to be installed at:
645662 $( stack_location)
646663Use 'stack upgrade' or your OS's package manager to upgrade,
647- or pass --force to this script to install anyway."
664+ or pass '-f' to this script to over-write the existing binary."
665+ fi
666+ else
667+ [ " $DEST " != " " ] || DEST=" $DEFAULT_DEST "
648668 fi
649669}
650670
@@ -662,6 +682,10 @@ while [ $# -gt 0 ]; do
662682 FORCE=" true"
663683 shift
664684 ;;
685+ -d|--dest)
686+ DEST=" $2 /stack"
687+ shift 2
688+ ;;
665689 * )
666690 echo " Invalid argument: $1 " >&2
667691 exit 1
0 commit comments