nanorc/install.sh

270 lines
6.4 KiB
Bash
Raw Normal View History

#!/bin/sh
# IMPROVED NANO SYNTAX HIGHLIGHTING FILES
# Get nano editor better to use and see.
# Copyright (C) 2014+ Anthony Scopatz et al.
# 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/>.
# https://spdx.org/licenses/GPL-3.0-or-later.html
2019-11-05 00:42:09 +00:00
# Help:
2019-11-05 14:18:03 +00:00
# Sed: http://www.grymoire.com/Unix/Sed.html
2019-11-05 00:42:09 +00:00
# Bash Variables
# Ativate or not the erros (1=activated)
OPTERR=1
# Separator (useful for simulate arrays)
2019-11-03 23:22:28 +00:00
G_IFS=" "
# Global Variables
2019-11-14 12:05:41 +00:00
G_VERSION="2019.10.17"
G_DEPS="unzip sed"
2019-11-24 00:15:08 +00:00
G_FILE="${HOME}/.nanorc"
2019-11-14 12:05:41 +00:00
G_REPO_MASTER="https://github.com/scopatz/nanorc/archive/master.zip"
G_REPO_RELEASE="https://github.com/scopatz/nanorc/archive/${G_VERSION}.zip"
unset G_LITE G_UNSTABLE G_VERBOSE G_DIR G_THEME
2019-11-02 21:15:04 +00:00
# Exit Values Help
# 0 - OK
2019-11-14 12:05:41 +00:00
# 1 - Big problem
2019-11-02 21:15:04 +00:00
# Functions
2019-11-02 21:15:04 +00:00
# Show the usage/help
f_menu_usage(){
echo "Usage: $0 [ -h|-l|-u|-v|-w ] [ -d DIR ] [ -t THEME ]"
2019-11-14 12:05:41 +00:00
echo
2019-11-02 21:15:04 +00:00
echo "IMPROVED NANO SYNTAX HIGHLIGHTING FILES"
echo "Get nano editor better to use and see."
echo
2019-11-14 12:05:41 +00:00
echo "-h Show help or usage."
2019-11-03 23:22:28 +00:00
echo "-l Activate lite installation."
2019-11-14 12:05:41 +00:00
echo " We will take account your existing .nanorc files."
echo "-u Use the unstable branch (master)."
2019-11-03 23:22:28 +00:00
echo "-v Show version, license and other info."
echo "-w Turn the script more verbose, often to tests."
echo
echo "-d DIR"
echo " Give other directory for installation."
echo " Default: ~/.nano/nanorc/"
echo
echo "-t THEME"
echo " Give other theme for installation."
echo " Default: scopatz"
echo " Options: nano, tpro"
2019-11-14 12:05:41 +00:00
exit 1
2019-11-02 21:15:04 +00:00
}
# Show version, license and other file.
f_menu_version(){
echo "IMPROVED NANO SYNTAX HIGHLIGHTING FILES"
echo "Version ${G_VERSION}"
echo
echo "Copyright (C) 2014+ Anthony Scopatz et al."
echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>."
echo "This is free software: you are free to change and redistribute it."
echo "There is NO WARRANTY, to the extent permitted by law."
echo
echo "Written by Anthony Scopatz and others."
2019-11-03 23:22:28 +00:00
echo
echo "For bugs report, please fill an issue at https://github.com/scopatz/nanorc"
2019-11-02 21:15:04 +00:00
exit 0
}
# Check dependencies
f_check_deps(){
DEPS_MISSED=""
# If there isn't the dependency the $DEPS_MISSED will be populated.
for DEP in $G_DEPS; do
if [ ! "$(command -v "$DEP")" ]; then
DEPS_MISSED="${DEP} ${DEPS_MISSED}"
fi
done
# Error if $DEPS_MISSED is populated.
if [ "$DEPS_MISSED" = "" ]; then
return 0
else
for DEP in $DEPS_MISSED; do
echo "The '${DEP}' program is required but was not found. Install '${DEP}' first and then run this script again." >&2
done
return 1
fi
}
2019-11-03 23:22:28 +00:00
# Set IFS
f_set_ifs(){
temp=$IFS
IFS=$G_IFS
G_IFS=temp
}
2019-11-14 12:05:41 +00:00
# Set Variable
# Receives two parameters:
# 1. the variable name
# 2. a value
# Sources: https://unix.stackexchange.com/questions/23111/what-is-the-eval-command-in-bash
f_set_variable(){
varname=$1
2019-11-24 00:15:08 +00:00
varvalue=""
2019-11-14 12:05:41 +00:00
shift
2019-11-24 00:15:08 +00:00
# Because 'sh' do not recognize indirect expansion "${!#}" like bash.
# Alert! The backslash "\" is needed!
eval varvalue="\$${varname}"
2019-11-14 12:05:41 +00:00
if [ -z "${varvalue}" ]; then
2019-11-24 00:15:08 +00:00
eval "$varname=${*}"
2019-11-14 12:05:41 +00:00
else
echo "Error: ${varname} already set."
2019-11-14 12:05:41 +00:00
usage
fi
}
2015-12-14 22:02:50 -05:00
2019-11-14 12:05:41 +00:00
_update_nanorc(){
2019-06-24 12:21:11 +02:00
touch ~/.nanorc
2019-10-03 23:06:15 +01:00
# add all includes from ~/.nano/nanorc if they're not already there
2017-12-28 01:29:50 -05:00
while read -r inc; do
if ! grep -q "$inc" "${NANORC_FILE}"; then
2017-12-28 01:29:50 -05:00
echo "$inc" >> "$NANORC_FILE"
fi
done < ~/.nano/nanorc
}
_update_nanorc_lite(){
sed -i '/include "\/usr\/share\/nano\/\*\.nanorc"/i include "~\/.nano\/*.nanorc"' "${NANORC_FILE}"
}
2015-12-14 22:02:50 -05:00
# Install
# Sources: https://www.cyberciti.biz/faq/download-a-file-with-curl-on-linux-unix-command-line/
f_install(){
temp="temp.zip"
begin="# BEGIN"
end="# END"
theme="${G_DIR}/themes/${G_THEME}/"
2019-11-24 00:15:08 +00:00
if [ ! cd "$HOME" ]; then
echo "Error: Cannot open or access ${HOME} directory."
exit 1
fi
mkdir -p "$G_DIR"
if [ ! -d "$G_DIR" ]; then
echo "Error: ${G_DIR} is not a directory or cannot be accessed or created."
usage
fi
if [ "$G_UNSTABLE" = true ]; then
curl -L -o $temp $G_REPO_MASTER
else
curl -L -o $temp $G_REPO_RELEASE
fi
unzip -u $temp
rm $temp
if [ "$G_UNSTABLE" = true ]; then
2019-11-24 00:15:08 +00:00
mv "nanorc-master/*" "$G_DIR"
rm -rf "nanorc-master"
else
2019-11-24 00:15:08 +00:00
mv "nanorc-${G_VERSION}" "$G_DIR"
rm -rf "nanorc-${G_VERSION}"
fi
if [ ! -d "$theme" ]; then
echo "Error: ${G_THEME} is not a theme or cannot be accessed."
usage
fi
touch "$G_FILE"
2019-11-24 00:15:08 +00:00
{ echo "$begin";
echo "";
echo "$end";
} >> $G_FILE
if [ "$G_LITE" = true ]; then
sed -n -i.bkp '/'"$begin"'/,/'"$end"'/ {
/'"$begin"'/n
/'"$end"'/ !{
s/*//
r
}
# r theme
# write the includes
}' $G_FILE
_update_nanorc_lite
else
_update_nanorc
fi
}
# update.
# write comments, options, gui, rebindings, includes highlights (according theme)
# lite = maintains the nano nanorc files'
# get the list of nano's files and include only ours (exclude).
# big change: update all nanorc
# big change: install "only" the themed nanorc files and .nanorc
2019-11-24 22:22:18 +00:00
# next: more shellcheck and get file and write it
2019-11-02 21:15:04 +00:00
2019-11-03 23:22:28 +00:00
# ============================
#
# MAIN / Init of script
#
# =============================
2019-11-02 21:15:04 +00:00
2019-11-14 12:05:41 +00:00
# Pre-check
2019-11-03 23:22:28 +00:00
f_set_ifs
2019-11-14 12:05:41 +00:00
f_check_deps && exit 1
2019-11-02 21:15:04 +00:00
2019-11-14 12:05:41 +00:00
# Menu
# Getopts: https://www.shellscript.sh/tips/getopts/
2019-11-23 22:37:49 +00:00
while getopts "d:hlt:uvw" c; do
2019-11-03 23:22:28 +00:00
case $c in
2019-11-24 00:15:08 +00:00
d) f_set_variable G_DIR "$OPTARG" ;;
2019-11-14 12:05:41 +00:00
h) f_menu_usage ;;
l) f_set_variable G_LITE true ;;
2019-11-24 00:15:08 +00:00
t) f_set_variable G_THEME "$OPTARG" ;;
u) f_set_variable G_UNSTABLE true ;;
2019-11-03 23:22:28 +00:00
v) f_menu_version ;;
w) f_set_variable G_VERBOSE true ;;
2019-11-14 12:05:41 +00:00
*) f_menu_usage ;;
2019-11-03 23:22:28 +00:00
esac
done
# Set defaults if there is not.
2019-11-24 00:15:08 +00:00
[ -z "$G_DIR" ] && G_DIR="${HOME}/.nano/nanorc/"
[ -z "$G_THEME" ] && G_THEME="scopatz"
2019-11-14 12:05:41 +00:00
# Set verbose
if [ "$G_VERBOSE" = true ]; then
set -x
fi
# Install
f_install
2019-11-03 23:22:28 +00:00
2019-11-14 12:05:41 +00:00
# Post-check
2019-11-03 23:22:28 +00:00
f_set_ifs
2019-11-14 12:05:41 +00:00
exit 0