diff --git a/_MasterScript/lgsm b/_MasterScript/lgsm-core similarity index 94% rename from _MasterScript/lgsm rename to _MasterScript/lgsm-core index 4254ce3c6..3a43a26fd 100755 --- a/_MasterScript/lgsm +++ b/_MasterScript/lgsm-core @@ -154,12 +154,17 @@ fn_runfunction(){ # Load GitHub hashing and updating functions fn_runfunction github_hash.sh -# Process game configs and load variables needed to run script -fn_runfunction game_settings.sh - # Load core functions fn_runfunction core_functions.sh -# Get option from command line and run option parser -getopt=$1 -core_getopt.sh +if [ "${selfname}" == "lgsm-core" ]; +then + 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 diff --git a/functions/core_messages.sh b/functions/core_messages.sh index 939e73889..d92228ec4 100644 --- a/functions/core_messages.sh +++ b/functions/core_messages.sh @@ -6,6 +6,18 @@ lgsm_version="271215" # 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. fn_scriptlog(){ if [ -n "${modulename}" ]; then @@ -145,4 +157,4 @@ fn_printokeol(){ # FAIL for end of line fn_printfaileol(){ echo -e "\e[0;31mFAIL\e[0m\n" -} \ No newline at end of file +} diff --git a/functions/install_lgsm.sh b/functions/install_lgsm.sh new file mode 100644 index 000000000..c8af240b8 --- /dev/null +++ b/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 diff --git a/functions/menu.sh b/functions/menu.sh new file mode 100644 index 000000000..0e7dce75a --- /dev/null +++ b/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 "" [ ] . . . + 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}"