diff --git a/README.md b/README.md index 66f0bc6..34a394f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,19 @@ # script-wizard -A fully automated installer for python scripts +A fully automated installer for python scripts. + +![witch-hat](https://github.com/zeroSal/script-wizard/assets/38191926/ac7b475d-60c0-4ee2-b24c-05ebcd3c8ed2) + +[Icon by Freepik - Flaticon](https://www.flaticon.com/free-icons/wizard) + +# Requirements +- The shell should be `bash` +- MacOS or Linux +- The system package manager must be `apt` or `yum` for Linux environments and `brew` for MacOS enviroments +- The python script must be a single file inside placed under the `src/` directory of the project + +# Usage +1) Copy the `install.sh` script to your project root directory +2) Edit the script replacing the 3 variables as described below: + - `PROGRAMNAME` is the **full program name** such as "My Project" + - `DEVELOPERNAME` is the **developer name** such as "My Name" + - `SHORTPROGRAMNAME` is the **git like project name** such as "my-project" diff --git a/install.sh b/install.sh index 5eb7d98..783d0a3 100644 --- a/install.sh +++ b/install.sh @@ -1,45 +1,137 @@ #!/bin/bash -if [ "$EUID" -ne 0 ] - then echo "[x] Root privileges required!" - exit -fi - PROGRAMNAME="" DEVELOPERNAME="" SHORTPROGRAMNAME="" +install_python_mac () { + if ! command -v brew > /dev/null; then + echo "[x] Brew package manager not installed." + exit 1 + fi + + brew list | grep "python@3.*" > /dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "[*] Installing the python interpreter on the system..." + if ! brew install -y "python3" > /dev/null 2>&1; then + echo "[x] Cannot install the python interpreter via brew." + exit 1 + fi + else + echo "[*] The python interpreter is already installed." + fi +} + +install_python_linux () { + if [ "$EUID" -ne 0 ] + then echo "[x] Root privileges required!" + exit 1 + fi + + if command -v apt-get > /dev/null; then + install_python_apt + elif command -v yum > /dev/null; then + install_python_yum + else + echo "[x] Neither apt nor yum found as package manager." + exit 1 + fi +} + +install_python_apt () { + if [ $(dpkg-query -W -f='${Status}' python3 > /dev/null 2>&1 | grep -c "ok installed") -eq 0 ]; then + echo "[*] Installing the python interpreter on the system..." + apt update > /dev/null 2>&1; + apt install python3 > /dev/null 2>&1; + fi +} + +install_python_yum () { + if ! yum list installed python3 &>/dev/null; then + echo "[*] Installing the python interpreter on the system..." + yum install -y python3 > /dev/null 2>&1; + fi +} + +check_init_variables () { + if [ -z "$PROGRAMNAME" ]; then + echo "[x] The PROGRAMNAME variable is empty. Please configure the script before running it." + exit 2 + fi + + if [ -z "$DEVELOPERNAME" ]; then + echo "[x] The DEVELOPERNAME variable is empty. Please configure the script before running it." + exit 2 + fi + + if [ -z "$SHORTPROGRAMNAME" ]; then + echo "[x] The SHORTPROGRAMNAME variable is empty. Please configure the script before running it." + exit 2 + fi +} + +create_directory () { + if [ -w /opt ]; then + mkdir /opt/$SHORTPROGRAMNAME > /dev/null 2>&1; + else + sudo mkdir /opt/$SHORTPROGRAMNAME > /dev/null 2>&1; + fi +} + +copy_script () { + if [ -w /opt/$SHORTPROGRAMNAME ]; then + cp src/$SHORTPROGRAMNAME.py /opt/$SHORTPROGRAMNAME/$SHORTPROGRAMNAME.py > /dev/null 2>&1; + else + sudo cp src/$SHORTPROGRAMNAME.py /opt/$SHORTPROGRAMNAME/$SHORTPROGRAMNAME.py > /dev/null 2>&1; + sudo chmod 005 /opt/$SHORTPROGRAMNAME/$SHORTPROGRAMNAME.py + fi +} + +link_executable () { + if [ $(id -u) != 0 ]; then + sudo ln -s /opt/$SHORTPROGRAMNAME/$SHORTPROGRAMNAME.py /usr/bin/$SHORTPROGRAMNAME > /dev/null 2>&1; + else + ln -s /opt/$SHORTPROGRAMNAME/$SHORTPROGRAMNAME.py /usr/bin/$SHORTPROGRAMNAME > /dev/null 2>&1; + fi +} + +### MAIN ################################################################################# + +check_init_variables + echo "Welcome to the $PROGRAMNAME installer By using this software you must agree all below terms: - 1) By using this software, it's the end user’s responsibility to obey all applicable local, state and federal laws. + 1) By using this software, it's the end user's responsibility to obey all applicable local, state and federal laws. 2) The software is provided \"as is\", without warranty of any kind, express or implied. 3) $DEVELOPERNAME isn't responsible for any damage caused by this software. -[?] If you agree, press [Enter] to procede:" +[?] If you agree, press [Enter] to procede..." read junk -# TODO: Implement non-APT environment installation -if [ $(dpkg-query -W -f='${Status}' python3 > /dev/null 2>&1 | grep -c "ok installed") -eq 0 ]; then - # Python3 not installed on the system, just install it! - echo "[*] Installing python interpreter on the system..." - apt update > /dev/null 2>&1; - apt install python3 > /dev/null 2>&1; +if [ "$(uname)" == "Darwin" ]; then + install_python_mac +elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then + install_python_linux +else + echo "[x] Only MacOS and Linux operating system are supported." + exit 1 fi -# Install python requirements echo "[*] Installing python requirements..." pip3 install -r requirements.txt > /dev/null 2>&1; -# Install the script in the opt directory +if [ -d /opt/$SHORTPROGRAMNAME ]; then + echo "[*] The software seems already installed." + exit 0 +fi + echo "[*] Installing the software..." -mkdir /opt/$SHORTPROGRAMNAME > /dev/null 2>&1; -cp src/$SHORTPROGRAMNAME.py /opt/$SHORTPROGRAMNAME > /dev/null 2>&1; -chmod +x /opt/$SHORTPROGRAMNAME/$SHORTPROGRAMNAME.py > /dev/null 2>&1; +create_directory +copy_script -# Create the command for the shell -echo "[*] Setting up the environment..." -ln -s /opt/$SHORTPROGRAMNAME/$SHORTPROGRAMNAME.py /usr/bin/$SHORTPROGRAMNAME > /dev/null 2>&1; +echo "[*] Linking the executable..." +link_executable echo "" echo "[v] Successfully installed $PROGRAMNAME in your system!";