#!/bin/bash

if [ -e "$1" ] && [ -n "$2" ]; then
  # backup the existing config file if it exists
  if [ -e "/etc/credentials/$2" ]; then
    cp "/etc/credentials/$2" "/etc/credentials/$2.bak"
    # make sure owner is root
    chown root.root "/etc/credentials/$2.bak"
    # make it private rw-------
    chmod 600 "/etc/credentials/$2.bak"
  fi
  # copy the temp config file to the final location
  cp "$1" "/etc/credentials/$2"
  # make sure owner is root
  chown root.root "/etc/credentials/$2"
  # make it private rw-------
  chmod 600 "/etc/credentials/$2"
fi
