1#!/bin/bash
2install_packages=1
3default_install_prefix="/usr"
4install_prefix="$default_install_prefix"
5rebuild=""
6#
7usage()
8{
9    echo "Usage: $0 [Options]"
10    echo "***********************"
11    echo "  --help or -h      : Print usage"
12    echo "  --deps-only       : Just install build dependencies and exit"
13    echo "  --prefix=DIR      : Install to directory DIR (default: $default_install_prefix)"
14    echo "  --no-install      : Don't install generated debian avidemux packages"
15    echo "  --rebuild         : Preserve existing build directories"
16}
17#
18install_deps()
19{
20    echo "This will install all the packages necessary to build avidemux"
21    echo "You will be asked to enter your password because installing build dependencies requires root permissions"
22    # gcc, g++ and make get installed as dependencies of build-essential
23    sudo apt-get update && sudo apt-get install build-essential cmake pkg-config yasm \
24    libsqlite3-dev libfontconfig1-dev libfribidi-dev libxv-dev libvdpau-dev libva-dev libasound2-dev libpulse-dev \
25    qttools5-dev-tools qtbase5-dev \
26    libpng-dev libaften-dev libmp3lame-dev libx264-dev libxvidcore-dev libfaad-dev libfaac-dev libopus-dev libvorbis-dev libogg-dev libdca-dev \
27    || { echo "The installation at least of some of the build dependencies failed. Aborting." && exit 2; }
28    sudo apt-get install libx265-dev \
29    || echo "Warning: libx265-dev cannot be installed using package management. Avidemux won't be able to encode HEVC unless the library and the headers have been installed manually. Continuing anyway." # there are no official libx265 packages for Ubuntu Trusty
30    sudo apt-get install libvpx-dev \
31    || echo "Warning: libvpx-dev cannot be installed using package management. Avidemux won't be able to encode VP9 unless the library and the headers have been installed manually. Continuing anyway."
32    sudo apt-get install libaom-dev \
33    || echo "Warning: libaom-dev cannot be installed using package management. Avidemux won't be able to decode AV1 unless the library and the headers have been installed manually. Continuing anyway." # available from Ubuntu Eoan on
34}
35#
36install_avidemux()
37{
38    echo "Installing avidemux..."
39    cd debs && sudo dpkg -i *
40}
41#
42option_value()
43{
44    echo $(echo $* | cut -d '=' -f 2-)
45}
46#
47option_name()
48{
49    echo $(echo $* | cut -d '=' -f 1 | cut -b 3-)
50}
51#
52dir_check()
53{
54    op_name="$1"
55    dir_path="$2"
56    if [ "x$dir_path" != "x" ]; then
57        if [[ "$dir_path" != /* ]]; then
58            >&2 echo "Expected an absolute path for --$op_name=$dir_path, aborting."
59            exit 1
60        fi
61    else
62        >&2 echo "Empty path provided for --$op_name, aborting."
63        exit 1
64    fi
65    case "$dir_path" in
66      */)
67          echo $(expr "x$dir_path" : 'x\(.*[^/]\)') # strip trailing slashes
68          ;;
69      *)
70          echo "$dir_path"
71          ;;
72    esac
73}
74#
75while [ $# != 0 ]; do
76    config_option="$1"
77    case "$config_option" in
78        -h|--help)
79            usage
80            exit 0
81            ;;
82        --deps-only)
83            install_deps
84            exit 0
85            ;;
86        --prefix=*)
87            install_prefix=$(dir_check $(option_name "$config_option") $(option_value "$config_option")) || exit 1
88            ;;
89        --no-install)
90            install_packages=0
91            ;;
92        --rebuild)
93            rebuild="$config_option"
94            ;;
95        *)
96            echo "unknown parameter $config_option"
97            usage
98            exit 1
99            ;;
100    esac
101    shift
102done
103#
104install_deps
105#
106echo "Compiling avidemux, it will take 20 minutes or so"
107logfile="/tmp/log-bootstrap-$(date +%F_%T).log"
108bash bootStrap.bash --deb --prefix=$install_prefix $rebuild 2>&1 | tee ${logfile}
109if [ ${PIPESTATUS[0]} -ne 0 ]; then
110    echo "Build failed, please inspect ${logfile} and /tmp/logbuild* files."
111    if [ $install_packages -eq 1 ]; then
112        echo "Cancelling installation."
113    fi
114    exit 3
115fi
116#
117if [ $install_packages -eq 1 ]; then
118    install_avidemux
119    exit $?
120fi
121#
122exit 0
123