#!/bin/sh
#
# linbo_mountcache [-r] | [help]
# thomas@linuxmuster.net
# 20250425
#


#### functions begin ####

# read common shell functions
source /usr/share/linbo/shell_functions
echo "### $timestamp $(basename "$0") ###"


usage(){
  local RC="$1"
  echo
  echo "Mounts the cache partition. \"-r\" mounts/remounts readonly."
  echo
  echo "Usage: linbo_mountcache [-r] | [help]"
  echo
  exit "$RC"
}


# find cache partition
findcache(){
  local item
  local dev
  mkdir -p /tmp/mnt
  for item in /dev/disk/by-id/*part*; do
    dev="$(readlink "$item" | sed 's|../../|/dev/|')"
    if mount "$dev" /tmp/mnt &> /dev/null; then
      if [ -s /tmp/mnt/linbo64 ]; then
        export cache="$dev"
        umount /tmp/mnt &> /dev/null
        break
      fi
      umount /tmp/mnt &> /dev/null
    fi
  done
}

#### functions end ####


# print help
[ "$1" = "help" ] && usage 0

# get cache partition, otherwise use environment variable
if [ -b "$1" ]; then
  cache="$1"
  shift
fi
[ -z "$cache" -a -s /conf/linbo ] && source /conf/linbo
[ -n "$cache" ] && realcache="$(get_realdev $cache)"
[ -n "$realcache" ] && cache="$realcache"
[ -z "$cache" ] && findcache
[ -b "$cache" ] || usage 1

# get readonly option
[ "$1" = "-r" ] && ro="$1"

RC="0"

# Avoid duplicate mounts by just preparing read/write mode
mount_opts="$(grep " /cache " /proc/mounts | awk '{ print $4 }')"
if [ -n "$mount_opts" ]; then
  RW=""
  echo "$mount_opts" | grep -q ".*rw.*" && RW="true"
  case "$ro" in
    -r) [ -n "$RW" ] && mount -o remount,ro /cache ; RC="$?" ;;
    *) [ -n "$RW" ] || mount -o remount,rw /cache ; RC="$?" ;;
  esac
  exit "$RC"
fi
case "$cache" in
  *:*) # NFS
    server="${cache%%:*}"
    dir="${cache##*:}"
    echo "Mounting cache partition per nfs from $cache ..."
    # -o nolock is EXTREMELY important here, otherwise mount.nfs will timeout waiting for portmap
    mount $ro -t nfs4 -o nolock,relatime,rsize=1048576,wsize=1048576,hard,intr "$cache" /cache
    RC="$?"
    ;;
  \\\\*\\*|//*/*) # CIFS/SAMBA
    server="${cache%\\*}";  server="${server%/*}"; server="${server#\\\\}"; server="${server#//}"
    echo "Mounting cache partition per cifs from $cache ..."
    # unix extensions have to be disabled
    echo 0 > /proc/fs/cifs/LinuxExtensionsEnabled 2>/dev/null
    # mount.cifs (3) pays attention to $PASSWD
    # this does not work: $RSYNC_PASSWORD is not available and mount.cifs does not pay attention to $PASSWD
    #export PASSWD="$RSYNC_PASSWORD"
    #mount $2 -t cifs -o username=linbo,nolock "$cache" /cache 2>/dev/null
    # temporary workaround for password
    [ -s /tmp/linbo.passwd ] && PASSWD="$(cat /tmp/linbo.passwd 2>/dev/null)"
    [ -z "$PASSWD" -a -s /tmp/rsyncd.secrets ] && PASSWD="$(grep ^linbo /tmp/rsyncd.secrets | awk -F\: '{ print $2 }' 2>/dev/null)"
    mount $ro -t cifs -o username=linbo,password="$PASSWD",nolock "$cache" /cache
    RC="$?"
    if [ "$RC" != "0" ]; then
      echo "Authentication failure."
      mount $ro -t cifs -o nolock,guest,sec=none "$cache" /cache
      RC="$?"
      if [ "$RC" != "0" ]; then
        echo "Guest access does not work also."
      fi
    fi
    ;;
  /dev/*) # cache
    echo "Mounting cache partition $cache ..."
    linbo_mount "$cache" /cache $ro ; RC="$?"
    ;;
  *) # Yet unknown
    echo "Unknown source for cache partition: $cache." >&2
    ;;
esac

[ "$RC" = "0" ] || echo "Failed to mount cache partition $cache!" >&2

exit "$RC"
