Skip to content

How to Contribute

cobalt2727 edited this page May 8, 2023 · 10 revisions

How do I write a script?

Usually the scripts included in the Megascript start off with developer documentation at the repository of the game/application/program that you are trying to automate setup for.

Every script in this repo can be used as reference material for how to write to a file as root, how to prompt the user for input, and many more such techniques.

The available functions

All available functions can be found in the functions.sh file.
A few of the most important functions are detailed below...

error

This function is used to stop a script and write a line of red text to the terminal if a particular command reports an error code (like if a make install fails).
If a script errors, the log files for running that script can be optionally sent to our Discord in a special channel where message history is turned off for everyone except staff for privacy reasons.
Usage is as follows:

sudo apt install exampleprogramname || error "something is wrong with the user's repositories, or their internet dropped out while installing a dependency"
# other stuff would go here
make -j4 || error "cmake failed to complete"

error_user

Like the error function, with the difference being that it's usually for a problem on the end user's side of things and as such should NOT have the error log sent to us.

git clone https://github.com/torvalds/linux || error_user "Your internet isn't working or a firewall is blocking access to GitHub!"

get_system

This function saves the jetson_model and architecture variables for use within the scripts.
Currently it's automatically called for ALL scripts, so you don't need to manually run it, but take note of the variables below that are available to you:

jetson_model contains one of: tegra-2, tegra-3, tegra-4, tegra-x1, tegra-x2, xavier, or an empty string.
Refer to the box64.sh script for a good use of the jetson_model type to change cmake options when configuring box64.

dpkg_architecture (Debian/Ubuntu exclusive) contains the main userspace CPU type: arm64, armhf, i386, amd64, etc
architecture (works across all Linux distros, Fedora included) contains the actual native CPU architecture: aarch64, armv7l, armv6l, x86_64, i386, etc

Refer to the discord.sh script for a good example of the dpkg_architecture and architecture variables in action - note that dpkg_architecture was used on the Debian/Ubuntu side to achieve parity with how Webcord's releases are named by architecture.

package_available

The L4T Megascript primarily targets Ubuntu 18.04, since that's what Nvidia and the Switchroot team both officially support, but if you'll look over the code you'll see we believe it's important to support future Ubuntu revisions going forward. In 18.04, qt5-default is a package that sets Qt5 to be the default Qt version, as well as providing a default configuration for qtchooser. It's needed for a few of our scripts; however, it doesn't exist in Ubuntu versions higher than 20.04. To speed up useless attempts at installing a program that doesn't exist, the ocs-url script uses this function to see if qt5-default is available, then only installs it if it can be installed.

package_available qt5-default
if [[ $? == "0" ]]; then
  sudo apt install -y qt5-default || error "Failed to install dependencies"
else
  echo "qt5-default does not exist on this system"
fi

ppa_installer and ppa_purger

These functions simply automate the yes/no response to adding or removing a PPA (and in the case of removing a PPA, our ppa_purger also removes the PPA's packages with it). For example, instead of

sudo add-apt-repository ppa:theofficialgman/opt-qt-5.15.2-bionic-arm

we use the following (syntax is the same for ppa_purger):

ppa_name="theofficialgman/opt-qt-5.15.2-bionic-arm" && ppa_installer

PPA_check

This tool checks to see if a PPA is compatible with the user's OS version. Use it in scripts with caution. Bear in mind that:

  1. this currently doesn't take into account that PPAs may have broken builds for your particular OS release
  2. packages available (and unbroken!) may not be the same across all architectures - check the PPA first.
if PPA_check; then
  ppa_name="theofficialgman/opt-qt-5.15.2-bionic-arm" && ppa_installer
else
  #this generally should not error, but a workaround should be written up
  echo "PPA not available for your OS, doing other stuff instead..."
  #other stuff
fi

userinput_func

This function prompts the user for input through a GUI (zenity) or CLI (dialog) interface.
Its easiest to explain through an example usecase so one is given below:

description="This is an example of how to get user input.\
\nYou can print to multiple lines as shown here.
\n\n The table below can contain list of desired options that are passed as the variable `output`"
table=("yes" "no" "maybe")
userinput_func "$description" "${table[@]}"
if [[ $output == "yes" ]]; then
  echo "You chose yes"
elif [[ $output == "no" ]]; then
  echo "You chose no"
elif [[ $output == "maybe" ]]; then
  echo "What do you mean maybe? "
fi