#!/gnu/store/rib9g2ig1xf3kclyl076w28parmncg4k-bash-minimal-5.1.16/bin/sh

# paperconfig: configuration of system paper name
#
# Copyright (C) 1996, Yves Arrouye <arrouye@debian.org>
#               2001, Adrian Bunk  <bunk@fs.tum.de>
#               2013-2021, Reuben Thomas <rrt@sc3d.org>

# The functions in this file provide support for relocatability of
# shell scripts.  They should be included near the beginning of each
# shell script in a relocatable program, by adding @relocatable_sh@
# and causing the script to be expanded with AC_CONFIG_FILES.  A
# small amount of additional code must be added and adapted to the
# package by hand; see doc/relocatable-maint.texi (in Gnulib) for
# details.
#
# Copyright (C) 2003-2021 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

# Support for relocatability.
func_find_curr_installdir ()
{
  # Determine curr_installdir, even taking into account symlinks.
  curr_executable="$0"
  case "$curr_executable" in
    */* | *\\*) ;;
    *) # Need to look in the PATH.
      if test "${PATH_SEPARATOR+set}" != set; then
        # Determine PATH_SEPARATOR by trying to find /bin/sh in a PATH which
        # contains only /bin. Note that ksh looks also at the FPATH variable,
        # so we have to set that as well for the test.
        PATH_SEPARATOR=:
        (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
          && { (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 \
                 || PATH_SEPARATOR=';'
             }
      fi
      save_IFS="$IFS"; IFS="$PATH_SEPARATOR"
      for dir in $PATH; do
        IFS="$save_IFS"
        test -z "$dir" && dir=.
        for exec_ext in ''; do
          if test -f "$dir/$curr_executable$exec_ext"; then
            curr_executable="$dir/$curr_executable$exec_ext"
            break 2
          fi
        done
      done
      IFS="$save_IFS"
      ;;
  esac
  # Make absolute.
  case "$curr_executable" in
    /* | ?:/* | ?:\\*) ;;
    *) curr_executable=`pwd`/"$curr_executable" ;;
  esac
  # Resolve symlinks.
  sed_dirname='s,/[^/]*$,,'
  sed_linkdest='s,^.* -> \(.*\),\1,p'
  while : ; do
    lsline=`LC_ALL=C ls -l "$curr_executable"`
    case "$lsline" in
      *" -> "*)
        linkdest=`echo "$lsline" | sed -n -e "$sed_linkdest"`
        case "$linkdest" in
          /* | ?:/* | ?:\\*) curr_executable="$linkdest" ;;
          *) curr_executable=`echo "$curr_executable" | sed -e "$sed_dirname"`/"$linkdest" ;;
        esac ;;
      *) break ;;
    esac
  done
  curr_installdir=`echo "$curr_executable" | sed -e 's,/[^/]*$,,'`
  # Canonicalize.
  curr_installdir=`cd "$curr_installdir" && pwd`
}
func_find_prefixes ()
{
  # Compute the original/current installation prefixes by stripping the
  # trailing directories off the original/current installation directories.
  orig_installprefix="$orig_installdir"
  curr_installprefix="$curr_installdir"
  while true; do
    orig_last=`echo "$orig_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'`
    curr_last=`echo "$curr_installprefix" | sed -n -e 's,^.*/\([^/]*\)$,\1,p'`
    if test -z "$orig_last" || test -z "$curr_last"; then
      break
    fi
    if test "$orig_last" != "$curr_last"; then
      break
    fi
    orig_installprefix=`echo "$orig_installprefix" | sed -e 's,/[^/]*$,,'`
    curr_installprefix=`echo "$curr_installprefix" | sed -e 's,/[^/]*$,,'`
  done
}
if test "yes" = yes; then
    prefix="/gnu/store/imfq9cgm2m86zxrl1139h580bx8jvwig-libpaper-2.0.0"
    exec_prefix="${prefix}"
    bindir="${exec_prefix}/bin"
    orig_installdir="$bindir" # see Makefile.am's *_SCRIPTS variables
    func_find_curr_installdir # determine curr_installdir
    func_find_prefixes
    # Relocate the directory variables that we use.
    sysconfdir=`
        echo "${prefix}/etc/" \
        | sed -e "s%^${orig_installprefix}/%${curr_installprefix}/%" \
        | sed -e 's,/$,,'`
    bindir=`
        echo "$bindir/" \
        | sed -e "s%^${orig_installprefix}/%${curr_installprefix}/%" \
        | sed -e 's,/$,,'`
else
    prefix="/gnu/store/imfq9cgm2m86zxrl1139h580bx8jvwig-libpaper-2.0.0"
    exec_prefix="${prefix}"
    bindir="${exec_prefix}/bin"
    sysconfdir="${prefix}/etc"
fi
PAPERSIZE="$sysconfdir/papersize"
PAPERRUNPARTSDIR="$sysconfdir/paper.d"
PAPER_PROGRAM="$bindir/paper"

usage() {
    echo "Usage: `basename $0` [--version|--help|--paper NAME|--choose]"

    cat <<EOF

Options: -V, --version          print version information and exit
         -h, --help             print this help and exit
         -p, --paper NAME       specify the paper to use
         -c, --choose           choose paper interactively
EOF
    exit $1
}

choose=0

if [ "$#" -eq 0 ]; then
    exec "$PAPER_PROGRAM" --no-size
fi

while [ $# -ne 0 ]; do
    case "$1" in
	-V|--version)
	    echo "`basename $0` version 2.0.0" \
		"by Yves Arrouye <arrouye@debian.org>"
	    exit 0
	    ;;
	-h|--help)
	    usage 0
	    ;;
	-p|--paper)
	    test $# -gt 1 || usage
	    paper="$2"
	    shift
	    ;;
	-c|--choose)
	    choose=1
	    ;;
	*)
	    usage 1
	    ;;
    esac
    shift
done

invalidpaper() {
    ! "$PAPER_PROGRAM" --no-size "$1" 2>/dev/null 1>&2
}

setpaper() {
    paperright=`"$PAPER_PROGRAM" --no-size "$1" 2>/dev/null`

    ok=0

    if 2>/dev/null echo "$paperright" >${PAPERSIZE}.new; then
	if 2>/dev/null mv -f ${PAPERSIZE}.new $PAPERSIZE; then
	    if 2>/dev/null chmod 644 $PAPERSIZE; then
		ok=1
	    fi
	fi
    fi

    if [ $ok -eq 0 ]; then
	echo `basename $0`: cannot create $PAPERSIZE
	exit 2
    else
	if [ -d $PAPERRUNPARTSDIR ]; then
	    run-parts $PAPERRUNPARTSDIR
	fi
    fi
}

if [ ! -z "$paper" ]; then
    if invalidpaper $paper; then
	>&2 echo `basename $0`: \"$paper\" is not a known paper name
	exit 3
    fi
    setpaper "$paper"
    exit 0
fi

knownpapers="`"$PAPER_PROGRAM" --all --no-size`"

prompt=
width=72

paper=`paperconf 2>/dev/null || true`
dftpaper=`paperconf`

if [ $choose -eq 1 ]; then
    echo

    LESS="-X -E $LESS"
    export LESS

    (
	cat <<EOF
The system paper can be chosen from many known papers that are
currently recognized:

EOF
	echo "$knownpapers" | sed 's/^/    /'
	echo
    ) | /usr/bin/sensible-pager

    : ${paper:=$dftpaper}

    echo -n "Default paper name? [$paper] "
    read ans
    : ${ans:=$paper}
    paper=$ans

    while invalidpaper "$paper"; do
        if [ -z "$paper" ]; then
	    echo "Please choose a paper from the available papers list."
        else
	    echo "Unknown paper \"$paper\"," \
	        "please choose one from the available papers list."
        fi

        paper=$dftpaper

        echo -n "Default paper name? [$paper] "
        read ans
        paper=$ans
    done
fi

if invalidpaper "$paper"; then
    echo "Invalid paper \"$paper\": not setting the system default."
    exit 4
elif [ "`paperconf 2>/dev/null`" != "$paper" ] || grep -q "[#	]" $PAPERSIZE; then
    setpaper "$paper"
fi
