#!/usr/bin/env bash ##################### ##### DEFAULTS ###### ##################### setDefaults() { runDebug "${FUNCNAME[0]}" [[ -z $_debug ]] && _debug="true" # Set to true to enable debugging by default [[ -z $_builddir ]] && _builddir="$PWD" [[ -z $_filesroot ]] && _filesroot="$_builddir/files/" # Global default packages for all profiles declare -a _packages=("luci" "nano" "htop" "tcpdump" "diffutils") # If no profile is specified, use the TP-Link Archer C7 v2 [[ -z $_profile ]] && _profile="tplink_archer-c7-v2" # PROFILES # TP-Link Archer C7 v2 if [[ "$_profile" == "tplink_archer-c7-v2" ]]; then [[ -z $_version ]] && _version="19.07.2" [[ -z $_target ]] && _target="ath79/generic" # Raspberry Pi 4 elif [[ "$_profile" == "rpi-4" ]]; then [[ -z $_version ]] && _version="snapshot" [[ -z $_target ]] && _target="bcm27xx/bcm2711" packages+=("kmod-usb-net-asix-ax88179" "luci-app-upnp" "luci-app-wireguard" \ "luci-app-vpn-policy-routing" "-dnsmasq" "dnsmasq-full") fi } ##################### ##### FUNCTIONS ##### ##################### printHelpAndExit () { runDebug "${FUNCNAME[0]}" cat <<-'EOF' USAGE: buildOpenWRT [[OPTION] [VALUE]]... OPTIONS --version, -v OPENWRT_VERSION --target, -t TARGET --profile, -p PROFILE --builddir, -b PATH --ssh-backup SSH path Example: root@192.168.1.1 --debug, -d --help, -h EOF # Exit using passed exit code [[ -z $1 ]] && exit 0 || exit "$1" } parseInput () { runDebug "${FUNCNAME[0]}" if _input=$(getopt -o +v:t:p:b:dh -l version:,target:,profile:,builddir:,ssh-backup:debug,help -- "$@"); then eval set -- "$_input" while true; do case "$1" in --version|-v) shift && _version="$1" ;; --target|-t) shift && _target="$1" ;; --profile|-p) shift && _profile="$1" ;; --builddir|-b) shift && _builddir="$1" ;; --ssh-backup) shift && _ssh_backup="$1" ;; --debug|-d) echo "Debugging on" _debug="true" ;; --help|-h) _printHelpAndExit 0 ;; --) shift break ;; esac shift done else echo "Incorrect options provided" printHelpAndExit 1 fi } runDebug () { [[ "$_debug" == "true" ]] && echo "Running: " "$@" ; } installPrerequisites () { runDebug "${FUNCNAME[0]}" getOS () { runDebug "${FUNCNAME[0]}" if [[ -f /etc/os-release ]]; then source /etc/os-release else echo "Cannot detect OS!" exit 1 fi } getOS if [[ "$ID" == "fedora" ]]; then if ! sudo dnf -y install @c-development @development-tools @development-libs zlib-static elfutils-libelf-devel gawk unzip file wget python3 python2 > /dev/null 2>&1; then echo "Warning: Problem installing prerequisites" fi elif [[ "$ID" =~ ^(debian|ubuntu)$ ]]; then if ! sudo apt-get -y install build-essential libncurses5-dev libncursesw5-dev zlib1g-dev gawk git gettext libssl-dev xsltproc wget unzip python > /dev/null 2>&1; then echo "Warning: Problem installing prerequisites" fi fi } acquireImageBuilder () { runDebug "${FUNCNAME[0]}" local _url if [[ "$_version" == "snapshot" ]]; then _filename="openwrt-imagebuilder-${_target//\//-}.Linux-x86_64.tar.xz" _url="https://downloads.openwrt.org/snapshots/targets/$_target/$_filename" else _filename="openwrt-imagebuilder-$_version-${_target//\//-}.Linux-x86_64.tar.xz" _url="https://downloads.openwrt.org/releases/$_version/targets/$_target/$_filename" fi # Make sources directory if it does not exist [[ ! -d "$_builddir/sources" ]] && mkdir -p "$_builddir/sources" if [[ ! -f "$_builddir/sources/$_filename" ]]; then echo "Downloading $_url to $_builddir/sources" if ! wget -q -P "$_builddir/sources" "$_url"; then echo "Could not download Image Builder" exit 1 fi else echo "Image builder already exists, skipping download..." fi } extractImageBuilder () { runDebug "${FUNCNAME[0]}" if [[ -f "$_builddir/sources/$_filename" ]]; then if ! tar -xf "$_builddir/sources/$_filename" -C "$_builddir/sources/"; then echo "Extraction failed" exit 1 fi fi } makeImage () { runDebug "${FUNCNAME[0]}" # move to extracted source directory if ! pushd "$_builddir/sources/${_filename%.tar.xz}" > /dev/null 2>&1; then exit 1 fi # Make bin dir [[ ! -d "$_builddir/bin" ]] && mkdir -p "$_builddir/bin" # build image if ! make -j4 image BIN_DIR="$_builddir/bin/$_profile/" PROFILE="$_profile" \ PACKAGES="${_packages[*]}" FILES="$_filesroot"; then echo "Make image failed!" exit 1 fi if ! popd > /dev/null 2>&1; then exit 1 fi } #sshBackup () { # # runDebug "${FUNCNAME[0]}" # # # Make files directory if it does not exist # [[ ! -d "$_filesroot" ]] && mkdir -p "$_filesroot" # # for fd in "${_backedup[@]}"; do # _dir="${fd%/*}/" # [[ ! -d "$_filesroot$_dir" ]] && mkdir -p "$_filesroot/$_dir" # if ! scp -rp "$_ssh_backup:$fd" "$_filesroot/$_dir"; then # echo "Did not successfully backup files from --ssh-backup" # echo "Exiting now to prevent data loss!" # exit 1 # fi # done #} # TODO #flashImage () { # # if ! scp -rp "$_bin_path" "$_ssh_backup:/tmp/$_bin_name"; then # echo "Could not copy update file to device!" # exit 1 # fi # #3 # shellcheck disable=SC2029 # if ! ssh "$_ssh_backup" "sysupgrade -v /tmp/$_bin_name"; then #3 echo "sysupgrade failed!" # exit 1 # fi #} __main () { parseInput "$@" setDefaults getOS installPrerequisites acquireImageBuilder extractImageBuilder [[ -n $_ssh_backup ]] && sshBackup makeImage #flashImage # TODO exit 0 } __main "$@"