Browse Source

Added center print and horizontal line displays to core_messages. Added menu processor that can use ncurses with bash builtin fallback. Updated master script with self-installation feature.

pull/553/head
Jared Ballou 10 years ago
parent
commit
f4003da967
  1. 17
      _MasterScript/lgsm-core
  2. 14
      functions/core_messages.sh
  3. 45
      functions/install_lgsm.sh
  4. 90
      functions/menu.sh

17
_MasterScript/lgsm → _MasterScript/lgsm-core

@ -154,12 +154,17 @@ fn_runfunction(){
# Load GitHub hashing and updating functions # Load GitHub hashing and updating functions
fn_runfunction github_hash.sh fn_runfunction github_hash.sh
# Process game configs and load variables needed to run script
fn_runfunction game_settings.sh
# Load core functions # Load core functions
fn_runfunction core_functions.sh fn_runfunction core_functions.sh
# Get option from command line and run option parser if [ "${selfname}" == "lgsm-core" ];
getopt=$1 then
core_getopt.sh fn_runfunction install_lgsm.sh
else
# Process game configs and load variables needed to run script
fn_runfunction game_settings.sh
# Get option from command line and run option parser
getopt=$1
core_getopt.sh
fi

14
functions/core_messages.sh

@ -6,6 +6,18 @@ lgsm_version="271215"
# Description: Defines on-screen messages such as [ OK ] and how script logs look. # Description: Defines on-screen messages such as [ OK ] and how script logs look.
# Print text center-aligned
fn_print_center() {
columns="$(tput cols)"
line="$@"
printf "%*s\n" $(( (${#line} + columns) / 2)) "$line"
}
# Print horizontal line
fn_print_horizontal(){
char="${1:-=}"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' "${char}"
}
# Date and servicename for log files. # Date and servicename for log files.
fn_scriptlog(){ fn_scriptlog(){
if [ -n "${modulename}" ]; then if [ -n "${modulename}" ]; then
@ -145,4 +157,4 @@ fn_printokeol(){
# FAIL for end of line # FAIL for end of line
fn_printfaileol(){ fn_printfaileol(){
echo -e "\e[0;31mFAIL\e[0m\n" echo -e "\e[0;31mFAIL\e[0m\n"
} }

45
functions/install_lgsm.sh

@ -0,0 +1,45 @@
#!/bin/bash
# LGSM install_lgsm function
# Author: Jared Ballou
# Website: http://gameservermanagers.com
lgsm_version="200116"
# Description: Display menu of available games and install the one selected
# Perform installation
fn_runfunction menu.sh
# Listing of available games
gamelist="gamedata/__game_list"
# Installation path
installpath=$(cd ~ && pwd)
# Get game list
fn_getgithubfile $gamelist
# Display installer menu
fn_menu result "Linux Game Server Manager" "Select game to install" "${lgsmdir}/${gamelist}"
# If we have a selection, do the install
if [ -n "${result}" ]; then
# Confirm path for installation
read -p "Select path to install ${result} [${installpath}]: " input
installpath=${input:-$installpath}
scriptpath="${installpath}/${result}"
# If file exists, confirm overwrite
if [ -e "${scriptpath}" ]; then
read -p "WARNING! ${scriptpath} already exists! OVERWRITE!? [y/N]: " input
if [ "${input}" != "y" ] && [ "${input}" != "Y" ]; then exit; fi
fi
# Install script
echo -ne "Installing to ${scriptpath}... \c"
# Create directory if missing. TODO: Gravefully handle errors like giving a file as the install dir
if [ ! -e $(dirname "${scriptpath}") ]; then
mkdir -p $(dirname "${scriptpath}")
fi
# Copy script and set executable
cp "${BASH_SOURCE[0]}" "${scriptpath}"
chmod 0755 "${scriptpath}"
if [ $? ]; then
fn_colortext green "Done"
echo "Script deployed to ${scriptpath}"
else
fn_colortext red "FAIL"
fi
fi

90
functions/menu.sh

@ -0,0 +1,90 @@
#!/bin/bash
# LGSM fn_messages function
# Author: Jared Ballou
# Website: http://gameservermanagers.com
lgsm_version="200116"
# Description: Display menus and return selection
# Display simple Bash menu
fn_menu_bash() {
local resultvar=$1
title=$2
caption=$3
options=$4
fn_print_horizontal
fn_print_center $title
fn_print_center $caption
fn_print_horizontal
menu_options=()
while IFS='' read -r line || [[ -n "$line" ]]; do
menu_options+=( "${line}" )
done < $options
menu_options+=( "Cancel" )
select option in "${menu_options[@]}"; do
if [ -n "${option}" ] && [ "${option}" != "Cancel" ]; then
eval "$resultvar=\"${option/%\ */}\""
fi
break
done
}
# Draw menu using Whiptail
fn_menu_whiptail() {
local menucmd=$1
local resultvar=$2
title=$3
caption=$4
options=$5
height=${6:-40}
width=${7:-80}
menuheight=${8:-30}
#whiptail --title "<menu title>" --menu "<text to show>" <height> <width> <menu height> [ <tag> <item> ] . . .
menu_options=()
while read -r key val; do
menu_options+=( ${key//\"} "${val//\"}" )
done < $options
OPTION=$($menucmd --title "${title}" --menu "${caption}" $height $width $menuheight "${menu_options[@]}" 3>&1 1>&2 2>&3)
if [ $? = 0 ]; then
eval "$resultvar=\"${OPTION}\""
else
eval "$resultvar="
fi
}
# Show menu and receive input. We try to see if whiptail/dialog is available
# for a pretty ncurses menu. If not, use Bash builtins.
fn_menu() {
local resultvar=$1
local selection=""
title=$2
caption=$3
options=$4
# If this is a list of options as a string, dump it to a file so we can process it
if [ ! -e $options ]; then
echo -ne "{$options}\n" > "${cachedir}/menu.options"
options="${cachedir}/menu.options"
fi
# Get menu command
for menucmd in whiptail dialog bash; do
if [ -x $(which $menucmd) ]; then
menucmd=$(which $menucmd)
break
fi
done
case "$(basename $menucmd)" in
whiptail|dialog)
fn_menu_whiptail "${menucmd}" selection "${title}" "${caption}" "${options}" 40 80 30
;;
*)
fn_menu_bash selection "${title}" "${caption}" "${options}"
;;
esac
eval "$resultvar=\"${selection}\""
}
# Example usage:
# This will display a menu of available games to install
#fn_menu result "Linux Game Server Manager" "Select game to install" "../gamedata/__game_list"
#echo "result is ${result}"
Loading…
Cancel
Save