#!/bin/sh
set -e

install_local_dir() {
  if [ ! -d $1 ]; then
    mkdir -p $1
  fi
  if [ -f /etc/staff-group-for-usr-local ]; then
    chown root:staff $1 2> /dev/null || true
    chmod 2775 $1 2> /dev/null || true
  fi
}

install_from_default() {
  if [ ! -f $2 ]; then
    cp -p $1 $2
  fi
}

install_directory() {
  if [ ! -d /$1 ]; then
    mkdir /$1
    chown root:$3 /$1
    chmod $2 /$1
  fi
}

migrate_directory() {
  if [ ! -L $1 ]; then
    rmdir $1
    ln -s $2 $1
  fi
}

if [ ! -e /etc/dpkg/origins/default ]; then
  if [ -e /etc/dpkg/origins/debian ]; then
    ln -sf debian /etc/dpkg/origins/default
  fi
fi

if [ "$1" = "configure" ] && [ "$2" = "" ]; then
  install_from_default /usr/share/base-files/staff-group-for-usr-local \
    /etc/staff-group-for-usr-local
  install_from_default /usr/share/base-files/dot.profile   /root/.profile
  install_from_default /usr/share/base-files/dot.bashrc    /root/.bashrc
  install_from_default /usr/share/base-files/profile       /etc/profile
  install_from_default /usr/share/base-files/motd          /etc/motd
  install_directory srv       755 root
  install_directory opt       755 root
  install_directory etc/opt   755 root
  install_directory var/opt   755 root
  install_directory media     755 root
  install_directory var/mail 2775 mail
  if [ ! -L /var/spool/mail ]; then
    ln -s ../mail /var/spool/mail
  fi
  install_directory run/lock 1777 root
  migrate_directory /var/run /run
  migrate_directory /var/lock /run/lock

  install_local_dir /usr/local
  install_local_dir /usr/local/share
  install_local_dir /usr/local/share/man
  install_local_dir /usr/local/bin
  install_local_dir /usr/local/games
  install_local_dir /usr/local/lib
  install_local_dir /usr/local/include
  install_local_dir /usr/local/sbin
  install_local_dir /usr/local/src
  install_local_dir /usr/local/etc
  ln -sf share/man /usr/local/man

  if [ ! -f /var/log/wtmp ]; then
    echo -n>/var/log/wtmp
  fi
  if [ ! -f /var/log/btmp ]; then
    echo -n>/var/log/btmp
  fi
  if [ ! -f /var/log/lastlog ]; then
    echo -n>/var/log/lastlog
  fi
  chown root:utmp /var/log/wtmp /var/log/btmp /var/log/lastlog
  chmod 664 /var/log/wtmp /var/log/lastlog
  chmod 660 /var/log/btmp
  if [ ! -f /var/run/utmp ]; then
    echo -n>/var/run/utmp
  fi
  chown root:utmp /var/run/utmp
  chmod 664 /var/run/utmp
fi

if [ ! -d /var/lib/dpkg ]; then
  mkdir -m 755 -p /var/lib/dpkg
  chown root:root /var/lib/dpkg
fi
if [ ! -f /var/lib/dpkg/status ]; then
  echo > /var/lib/dpkg/status
  chmod 644 /var/lib/dpkg/status
  chown root:root /var/lib/dpkg/status
fi

if [ ! -f /usr/info/dir ] && [ ! -f /usr/share/info/dir ]; then
  install_from_default /usr/share/base-files/info.dir /usr/share/info/dir
  chmod 644 /usr/share/info/dir
  chown root:root /usr/share/info/dir
fi

if [ "$1" = "configure" ] && [ "$2" != "" ]; then
  if [ -f /etc/profile ]; then
    md5=`md5sum /etc/profile | cut -f 1 -d " "`
    if grep -q "$md5" /usr/share/base-files/profile.md5sums; then
      if ! cmp -s /usr/share/base-files/profile /etc/profile; then
        cp -p /usr/share/base-files/profile /etc/profile
        echo Updating /etc/profile to current default.
      fi
    fi
  fi
fi

if dpkg --compare-versions "$2" lt-nl "6.10"; then
  install_from_default /usr/share/base-files/staff-group-for-usr-local \
    /etc/staff-group-for-usr-local
fi

if dpkg --compare-versions "$2" lt-nl "10.3astra8"; then
  sed -i '\|#deb https://download.astralinux.ru/astra/stable/1.7_x86-64/repository/ 1.7_x86-64  main contrib non-free|d' /etc/apt/sources.list
  sed -i '1i# Astra Linux repository description https://wiki.astralinux.ru/x/0oLiC\n' /etc/apt/sources.list
    for rep in main update
	do
	    if ! grep -q "https://download.astralinux.ru/astra/stable/1.7_x86-64/repository-${rep}/" /etc/apt/sources.list; then
		echo "#deb https://download.astralinux.ru/astra/stable/1.7_x86-64/repository-${rep}/ 1.7_x86-64 main contrib non-free" >> /etc/apt/sources.list
	    fi
	done
echo >> /etc/apt/sources.list
    for rep in base extended
	do
	    if ! grep -q "https://download.astralinux.ru/astra/stable/1.7_x86-64/repository-${rep}/" /etc/apt/sources.list; then
		echo "#deb https://download.astralinux.ru/astra/stable/1.7_x86-64/repository-${rep}/ 1.7_x86-64 main contrib non-free" >> /etc/apt/sources.list
	    fi
	done
fi

if dpkg --compare-versions "$2" lt-nl "10.3.astra11"; then
    if [ -f /etc/nsswitch.conf ]; then
        sed -i 's/compat/files/g' /etc/nsswitch.conf
    fi
fi

if grep -q "https://download.astralinux.ru/astra/stable/1.7_x86-64/uu/last/repository-update/" /etc/apt/sources.list; then
  sed -i "/uu\/last\/repository-update/d" /etc/apt/sources.list
fi

if dpkg --compare-versions "$2" lt-nl "10.3.astra14"; then
  if [ -f /root/.profile ]; then
    sed 's/mesg n$/& || true/' -i /root/.profile
  fi
fi

chmod 750 /root
chmod 750 /etc/skel

# Заполнение /etc/astra_version /etc/issue и /etc/issue.net информацией о версии 
#cat /usr/share/base-files/astra_version > /etc/astra_version
#echo "Astra Linux `cat /usr/share/base-files/astra_version` \\n \\l" > /etc/issue
#echo Astra Linux `cat /etc/astra_version` > /etc/issue.net

# Stable Debian version as base of Astra
echo "10.0" > /etc/debian_version
#no need anymore rm -f /etc/debian_version
#no need anymore ln -s /etc/astra_version /etc/debian_version

