|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# |
| 4 | +# Copyright (c) 2020 Eaton |
| 5 | +# |
| 6 | +# This file is part of the Eaton 42ity project. |
| 7 | +# |
| 8 | +# This program is free software; you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation; either version 2 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# This program is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License along |
| 19 | +# with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | +# |
| 22 | +#! \file 09-install-hosts-redirect.everytime.sh |
| 23 | +# \brief Complete original /etc/hosts file with redirection addons |
| 24 | + |
| 25 | +HOSTS="/etc/hosts" |
| 26 | +HOSTS_BACKUP="/etc/hosts.ipm.backup" |
| 27 | +HOSTS_ADDONS="/tmp/hosts.ipm.addons" # editable by admin user |
| 28 | + |
| 29 | +RET=0 |
| 30 | + |
| 31 | +if [ ! -f $HOSTS ]; then |
| 32 | + echo "ERROR: $HOSTS not found" |
| 33 | + RET=1 |
| 34 | +elif [ ! -f $HOSTS_ADDONS ]; then |
| 35 | + # hosts exists and no host addons is given |
| 36 | + # restore hosts file from backup (if exists) |
| 37 | + if [ -f $HOSTS_BACKUP ]; then |
| 38 | + cp $HOSTS_BACKUP $HOSTS |
| 39 | + if [ $? -eq 0 ]; then |
| 40 | + echo "INFO: restore $HOSTS from $HOSTS_BACKUP" |
| 41 | + # delete backup (no error) |
| 42 | + rm -f $HOSTS_BACKUP || true |
| 43 | + else |
| 44 | + echo "ERROR: restore $HOSTS from $HOSTS_BACKUP failed" |
| 45 | + RET=1 |
| 46 | + fi |
| 47 | + fi |
| 48 | +else |
| 49 | + # hosts exists and hosts addons is given |
| 50 | + |
| 51 | + # backup hosts file *once* (hosts original, valid) |
| 52 | + if [ ! -f $HOSTS_BACKUP ]; then |
| 53 | + cp $HOSTS $HOSTS_BACKUP |
| 54 | + if [ $? -eq 0 ]; then |
| 55 | + echo "INFO: $HOSTS_BACKUP creation" |
| 56 | + else |
| 57 | + echo "ERROR: $HOSTS_BACKUP creation failed" |
| 58 | + RET=1 |
| 59 | + fi |
| 60 | + fi |
| 61 | + |
| 62 | + if [ $RET -eq 0 ]; then |
| 63 | + # backup is ok |
| 64 | + |
| 65 | + # concat HOSTS_BACKUP & HOSTS_ADDONS in a tmpfile |
| 66 | + TMPFILE="$(mktemp)" |
| 67 | + echo "# Generated by $(basename "$0")" >> $TMPFILE |
| 68 | + echo "# $(date)" >> $TMPFILE |
| 69 | + echo "$(cat $HOSTS_BACKUP)" >> $TMPFILE |
| 70 | + echo "$(cat $HOSTS_ADDONS)" >> $TMPFILE |
| 71 | + |
| 72 | + # replace hosts file by tmpfile (new hosts) |
| 73 | + cp $TMPFILE $HOSTS |
| 74 | + if [ $? -eq 0 ]; then |
| 75 | + echo "INFO: $HOSTS_ADDONS processing succeeded" |
| 76 | + else |
| 77 | + echo "ERROR: $HOSTS_ADDONS processing failed" |
| 78 | + RET=1 |
| 79 | + # restore hosts file from backup (no error) |
| 80 | + cp $HOSTS_BACKUP $HOSTS || true |
| 81 | + fi |
| 82 | + |
| 83 | + # cleanup tmpfile (no error) |
| 84 | + rm -f $TMPFILE || true |
| 85 | + fi |
| 86 | +fi |
| 87 | + |
| 88 | +exit $RET |
0 commit comments