%
% This m-file generates bash shell scripts for syncing between PC & SCC folders
%

disp('This utility will generate two shell scripts: .bash_profile and .aliases with built-in file-syncing macros')
disp('.bash_profile is a configuration file automatically invoked at login')
disp('.aliases file allows user to short-hand (oar alias) lengthy one-liner commands')
disp('.aliases is invoked within .bash_profile')
disp(' ')
%disp('Once successful in running this script, you will be able to use the below')
%disp('1. cp2scc will sync from your  PC matlab folder to your SCC matlab folder')
%disp('2. cp2pc  will sync from your SCC matlab folder to your  PC matlab folder')
disp(' ')
disp('Next, you will be asked for the paths on PC and SCC for which syncing applies.')
disp(' ')

PCpath = input('Your MATLAB folder on the PC is (e.g., /drives/c/windows/matlab):','s');
SCCpath = input('Your MATLAB folder on the SCC is (e.g., /project/XYZ/matlab):','s');
userID = input('Your userID is:','s');

% Generate .bash_profile . . .
fd = fopen('.bash_profile','w');
fprintf(fd, 'PATH=$PATH:$HOME:$HOME/bin\n');
fprintf(fd, '\n');
fprintf(fd, 'source .aliases\n');
fclose(fd);

% Generate .aliases file
fd = fopen('.aliases','w');
fprintf(fd, '# run lt at system prompt gives long list of the top 10 most recently used files\n');
fprintf(fd, 'alias lt=''ls --color -tl | head''\n');
fprintf(fd, '# run ll generates long list of all files in folder\n');
fprintf(fd, 'alias ll=''ls --color -l''\n');
fclose(fd);
%fprintf(fd, '# run cp2scc in /home/mobaxterm to sync SCC folder with what is on PC\n');
%fprintf(fd, 'alias cp2scc=''scp -rp %s %s@scc1.bu.edu:%s''\n', PCpath, userID, SCCpath);
%fprintf(fd, '# run cp2pc in /home/mobaxterm to sync PC folder with what is on SCC\n');
%fprintf(fd, 'alias cp2pc=''scp -rp %s@scc1.bu.edu:%s %s''\n', userID, SCCpath, PCpath);
fd = fopen('cp2scc','w');
fprintf(fd, '#!/bin/sh\n');
fprintf(fd, 'echo "SYNC:  [PC] --> [SCC]  ? (y or n)"\n');
fprintf(fd, 'read -s -r answer\n');
fprintf(fd, 'if [[ "$answer" = [Yy] ]]; then\n');
fprintf(fd, '   echo "Proceed with copy to SCC (from PC)"\n');
fprintf(fd, '   scp -rp %s %s@scc1.bu.edu:%s\n', PCpath, userID, SCCpath);
fprintf(fd, 'else\n');
fprintf(fd, '   echo "Copy cancelled"\n');
fprintf(fd, 'fi\n');
fclose(fd);

fd = fopen('cp2pc','w');
fprintf(fd, '#!/bin/sh\n');
fprintf(fd, 'echo "SYNC:  [SCC] --> [PC]  ? (y or n)"\n');
fprintf(fd, 'read -s -r answer\n');
fprintf(fd, 'if [["$answer" = [Yy] ]]; then\n');
fprintf(fd, '   echo "Proceed with copy to PC (from SCC)"\n');
fprintf(fd, '   scp -rp %s@scc1.bu.edu:%s %s\n', userID, SCCpath, PCpath);
fprintf(fd, 'else\n');
fprintf(fd, '   echo "Copy cancelled"\n');
fprintf(fd, 'fi\n');
fclose(fd);

fd = fopen('move2bin','w');
fprintf(fd, '#!/bin/sh\n');
fprintf(fd, '# Give these shell scripts execute access\n');
fprintf(fd, 'chmod +x ./cp2scc\n');
fprintf(fd, 'chmod +x ./cp2pc\n');
fprintf(fd, '# Create bin dir if not exists\n');
fprintf(fd, 'if [ ! -d "./bin" ]; then\n');
fprintf(fd, '   mkdir ./bin;\n');
fprintf(fd, 'fi\n');
fprintf(fd, '# move the scripts to bin dir\n');
fprintf(fd, 'mv ./cp2scc ./bin\n');
fprintf(fd, 'mv ./cp2pc ./bin\n');
fprintf(fd, '# Update .bash_profile now\n');
fprintf(fd, 'bash -l\n');
fclose(fd);

% Give move2bin the needed execute attribute
system('chmod +x move2bin');

% Note that all the files generated by makeMacros are to be copied to the PC for use there

