#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2011, Axel Beckert <abe@debian.org>
# Reattaches to screen sessions interactively

if ! command -v screen > /dev/null; then
    echo "screen not found in" "$PATH"
    exit 1
fi

SCREEN_OPTIONS="-r"
ISELECT_OPTIONS=""
[ -f ~/.screen-irrc ] && . ~/.screen-irrc

SELECTION=$(
    screen -ls |
    sed -n 's/^[ \t][ \t]*\([0-9]\)/<s>\1/p' |
    iselect -t "Select screen session to reattach" $ISELECT_OPTIONS
)

[ -n "$SELECTION" ] && exec screen $SCREEN_OPTIONS "$@" "${SELECTION%%.*}"
