#!/bin/csh
# Backup script for use on RAPID cruises
# Data is copied to an external connected to main workstation
# Drive mounted in /media
#
# This version by DAS Updated Tue 28th 2017.  Edited previous verions used 
# for remote backup on RAPID cruises
# 1)  syncs data from "cruise" directory, and creates a bakup of all changed files
# 2)  syncs data from "rapid/osnap"  directory, and creates a bakup of all changed files
# 3)  syncs data from "Desktop"  directory, and creates a bakup of all changed files
# When change for a new cruise need to change
# - drive names
# - $root
# - $cru

# Variables for pathnames
set cru  = jc145
set CRU = JC145
set root = /local/users/pstar/rpdmoc/
set rapiddir = rapid   # Could be osnap
set usersdir = users

# Which drive are we using and is it there?
echo ' '
echo `date` ' Starting backup'
echo ' '
echo -n 'Which disk are you using?   Enter '1', '2', or '0': '
set drq = $<
echo $drq
if ($drq == 1) then
  set drive_name = ${CRU}_back_1
else if  ($drq == 2) then
  set drive_name = ${CRU}_back_22
else if  ($drq == 0) then
  set drive_name = ${CRU}_back
else
  echo 'Input not recognised'
  exit
endif

# Drive pathname
set drive_path = '/media/'${drive_name}

if (-e ${drive_path}) then
   echo Drive connected
else
   echo ${drive_path} ' Does not seem to be there'
#   exit
endif 

# Create a logfile for the backup
set now = `date +%Y%m%d%H%M%S`
set backuplog = ${drive_path}/backup_logs/backup_log_${now}
if (-e $backuplog) then
  echo $backuplog exists
  exit
else
  touch $backuplog
endif

# -----------------------------------------------------------------
# First backup the cruise directory
set root_path_cru = ${root}${cru}
set sync_cru =  ${drive_path}/cruise_${cru}_sync_$drq
set back_cru =  ${drive_path}/cruise_${cru}_back_$drq/backup_${now}

# Call the main routine
# ----------------------------------------------------
backup_core_local $root_path_cru $sync_cru $back_cru $backuplog
# ----------------------------------------------------

# --------------------------------------------------------------------
# Sync all of the RAPID directory
set now = `date +%Y%m%d%H%M%S`
set root_path_rap = $root$rapiddir
set sync_rap =  ${drive_path}/${rapiddir}_${cru}_sync_$drq
set back_rap =  ${drive_path}/${rapiddir}_${cru}_back_$drq/backup_${now}

# Call the main routine
# ----------------------------------------------------
backup_core_local $root_path_rap $sync_rap $back_rap $backuplog
# ----------------------------------------------------

# --------------------------------------------------------------------
# Sync all of the RAPID directory
set now = `date +%Y%m%d%H%M%S`
set root_path_users = $root$usersdir
set sync_users =  ${drive_path}/${usersdir}_${cru}_sync_$drq
set back_users =  ${drive_path}/${usersdir}_${cru}_back_$drq/backup_${now}

# Call the main routine
# ----------------------------------------------------
backup_core_local $root_path_users $sync_users $back_users $backuplog
# ----------------------------------------------------

# --------------------------------------------------------------------
# Sync all of the Desktop/dy054 directory
set now = `date +%Y%m%d%H%M%S`
set deskdir = '../Desktop'
set root_path_desk = $root$deskdir
set sync_desk = ${drive_path}/desk_${cru}_sync_$drq
set back_desk = ${drive_path}/desk_${cru}_back_$drq/backup_${now}

# Call the main routine
# ----------------------------------------------------
backup_core_local $root_path_desk $sync_desk $back_desk $backuplog
# ----------------------------------------------------

# ------------------------------------------------------------------
# And finally copy log of backup to oceanus
set olog = /backup_logs

# Don't want to time out on password so check ok now
echo 'Press return to copy:' $backuplog $root_path_desk$olog
set qqq = $<
scp $backuplog $root_path_cru$olog
echo ' '
echo `date` - Backup finished