install-ubuntu-packages.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. # This script must be run as root
  8. if [[ $EUID -ne 0 ]]
  9. then
  10. echo "This script must be run as root (sudo)"
  11. exit 1
  12. fi
  13. #
  14. # Make sure we are installing on a supported ubuntu distro
  15. #
  16. lsb_release -c >/dev/null 2>&1
  17. if [ $? -ne 0 ]
  18. then
  19. echo This script is only supported on Ubuntu Distros
  20. exit 1
  21. fi
  22. # Read from the package list and process each package
  23. PACKAGE_FILE_LIST=package-list.ubuntu-$(. /etc/os-release && echo $UBUNTU_CODENAME).txt
  24. echo Reading package list $PACKAGE_FILE_LIST
  25. apt-get update
  26. # Read each line (strip out comment tags)
  27. for PREPROC_LINE in $(cat $PACKAGE_FILE_LIST | sed 's/#.*$//g')
  28. do
  29. LINE=$(echo $PREPROC_LINE | tr -d '\r\n')
  30. PACKAGE=$(echo $LINE | awk -F / '{$1=$1;print $1}')
  31. if [ "$PACKAGE" != "" ] # Skip blank lines
  32. then
  33. PACKAGE_VER=$(echo $LINE | awk -F / '{$2=$2;print $2}')
  34. if [ "$PACKAGE_VER" == "" ]
  35. then
  36. # Process non-versioned packages
  37. INSTALLED_COUNT=$(apt list --installed 2>/dev/null | grep ^$PACKAGE/ | wc -l)
  38. if [ $INSTALLED_COUNT -eq 0 ]
  39. then
  40. echo Installing $PACKAGE
  41. apt-get install $PACKAGE -y
  42. else
  43. INSTALLED_VERSION=$(apt list --installed 2>/dev/null | grep ^$PACKAGE/ | awk '{print $2}')
  44. echo $PACKAGE already installed \(version $INSTALLED_VERSION\)
  45. fi
  46. else
  47. # Process versioned packages
  48. INSTALLED_COUNT=$(apt list --installed 2>/dev/null | grep ^$PACKAGE/ | wc -l)
  49. if [ $INSTALLED_COUNT -eq 0 ]
  50. then
  51. echo Installing $PACKAGE \( $PACKAGE_VER \)
  52. apt-get install $PACKAGE=$PACKAGE_VER -y
  53. else
  54. INSTALLED_VERSION=$(apt list --installed 2>/dev/null | grep ^$PACKAGE/ | awk '{print $2}')
  55. if [ "$INSTALLED_VERSION" != "$PACKAGE_VER" ]
  56. then
  57. echo $PACKAGE already installed but with the wrong version. Purging the package
  58. apt purge --auto-remove $PACKAGE -y
  59. fi
  60. echo $PACKAGE already installed \(version $INSTALLED_VERSION\)
  61. fi
  62. fi
  63. fi
  64. done