#!/bin/bash
#
# globalvars (raider global variables)
# This file is part of Raider

SUPPORTED_FS_TYPES="swap ext2 ext3 ext4 xfs jfs reiserfs LVM2_member"	# File system types supported to create a raid array
declare -a FSTYPE
declare -a FORMAT_COM
FSTYPE[0]="swap";		FORMAT_COM[0]="mkswap -f"
FSTYPE[1]="ext2";		FORMAT_COM[1]="mkfs.ext2 -q"
FSTYPE[2]="ext3";		FORMAT_COM[2]="mkfs.ext3 -q"
FSTYPE[3]="ext4";		FORMAT_COM[3]="mkfs.ext4 -q"
FSTYPE[4]="xfs";		FORMAT_COM[4]="mkfs.xfs -f -q -d sunit=512,swidth=1024"
FSTYPE[5]="jfs";		FORMAT_COM[5]="mkfs.jfs -q"
FSTYPE[6]="reiserfs";	FORMAT_COM[6]="mkfs.reiserfs -q"
FSTYPE[7]="vfat";		FORMAT_COM[7]="mkdosfs -F 32"

MAIN_COMMAND=""
RAID_TYPE=""
SWAPRAIDER_MODE=0			# 0 is default. If = 0 create a non-raid swap partition. If = 1, create a raid swap array.
NO_SWAPRAIDER=0				# if == 1 then force swapraider_mode 0 
BOOTP_MODE=0				# 0 is default. If = 1, a /boot partition will be created
BOOTRAID1_MODE=0			# 0 is default. If = 1, the boot partition must be raid 1
BOOTP_SIZE=500MB			# /boot size default if in BOOTP_MODE
BOOTP_FS="ext2"				# /boot default filesystesm is "ext2"


#LOGDIR=/var/log
LOGFILE=${LOGDIR}/raider/raider.log	# raider log file
FSTAB=/etc/fstab
SWAP_PRFIX="####_add_swap_raider_ "
MTAB=/etc/mtab
#R_LIB=/var/lib/raider			# restart files. Directory where are saved files needed when restarting (step3)
DISTRO_TESTED=${RAIDER_LIB}/distro_tested
DISTRO_TESTED_MJ=${RAIDER_LIB}/distro_tested_mj
DISTRO_TESTED_SIMPLE=${RAIDER_LIB}/distro_tested_simple

STEP_FILE=$R_LIB/step					# file with step number to run
ADDERARRAY=$R_LIB/adderarray			# relationship between mdarrays and partition numbers to be used in step 3
SWAP_NEED_FORMAT=$R_LIB/swapneedformat	# swap partition in disk2 which needs to be formatted during step 3
DISKS_STEP3=$R_LIB/disks				# device disks identification to avoid mistakes after swapping disks in step 3
STEP=0									# step number flag
RAID_EXT="__raider"						# default extension name to apply on LVM2 Volume Groups
TEST_MODE=${TEST_MODE:=0}				# test mode flag
ORIGINAL_IS_SYSTEM_DISK=0		# flag saying the original disk is the system disk = 1. 0 = system is not the original disk

DB_DIR=""					# main temp directory
TEMP=""						# a small temp directory to internal temp files
DISK_DB=""					# database disk_db where original disk partition info is saved
DISK_DB_VIRTUAL=""			# database virtual disk partition when a new /boot partition is added (BOOTP_MODE=1)
MOUNTPOINT=""				# mountpoint directory to mount partitions from new disk
SYSTEM_DISK=""				# file where name of system disk is saved
NON_SYSTEM_DISKS=""			# file where other disks are saved
MOUNT_CLEAN=""				# file with a special clean fstab file
MOUNT_CLEAN_VIRTUAL=""		# file with a special clean fstab file applied to BOOTP_MODE=1
LIST_DISKS=""				# directory to save information about all disks
LIST_ARRAYS=""				# directory to save information about all md arrays
MDADM_CONF=""				# mdadm.conf path 
NEWMDADM_CONF=""			# new mdadm.conf path
NEWFSTAB=""					# new fstab path to use in md array
NEWMTAB=""					# new mtab path
NEWMENULST=""				# new grub config
NEWMENULST_MD=""			# new grub config to use in md array
NEWMENULST_MD_EXTRA1=""		# new grub config extra (used in Sabayon)
TMPGRUBS=""					# grub temp files 
TMPLILOS=""					# lilo temp files
GRUBLILOLOG=""				# grub and lilo log file, created in chroot environment to add later into logfile
VARSFILE=""					# path to a file which contains very important variables
GRUB1_RUN_SCRIPT=""			# script to install grub1 inside chroot

MKINITCPIO_CONF=/etc/mkinitcpio.conf 	# initrd builder arch linux distro configuration file
NEW_MKINITCPIO=""						# new config file for mkinitcpio in archlinux

DISTRO=""								# Linux distribution
MKINITRD=""								# program name to create the init ram file system (dependig on distro)
COM_MKINITRD=""							# command line to run the initram file system program
KERNEL_PRESET=""						# to be used when running mkinitcpio

PARTITION_TABLE_TYPE=""		# system hard disk partition table type
BOOTLOADER=""				# bootloader name used by system disk

## variables related with GRUB
GRUB_CONF=""				# directory where grub files are saved
GRUBNAME=""					# 1 or 2
GRUB_VERSION=""				# version number (x.yy)

GRUB_BASENAMES="menu.lst grub.conf grub.cfg"
GRUB_CONFIG_FILES="/boot/grub/menu.lst /grub/menu.lst /grub/grub.cfg 
	/boot/grub/grub.cfg /ubuntu/disks/boot/grub/menu.lst 
	/ubuntu/disks/install/boot/grub/menu.lst
	/boot/grub/grub.conf    /grub/grub.conf    /grub.conf"
GRUB2_INSTALL_NAME_LIST="grub-install grub2-install"
GRUB_INSTALL=""												# grub-install name depending on distro
GRUB2_CONFIG_FILES="/etc/default/grub"						# grub2 conf file to be edited by user before compilation
GRUB2_CFG_FILES="/boot/grub/grub.cfg /boot/grub2/grub.cfg"	# grub2 configuration file after compilation
GRUB2_CFG=""												# grub2 cfg path after being found
GRUB2_MKCONFIG_FILES="grub2-mkconfig grub-mkconfig"			# grub-mkconfig alternative names found in different distros
GRUB2_MKCONFIG=""											# grub-mkconfig name after being found

UEFI_MODE=0													# if 0 = no UEFI. if 1 = UEFI
UEFI_PART=1													# partition number of uefi partition (default=1)

LILO_CONFIG_FILES="/etc/lilo.conf /lilo.conf"				# possible path lilo config file 

MENULST=""													# menu.lst/grub.conf path (use "check_grub" function to parse it)
MENULST_EXTRA1="/etc/default/sabayon-grub"					# extra grub2 conf file (from sabayon)


## variables with values parsed from grub configuration file
vKroot=""			# rootfs 
vInitrd=""			# initrd name 
vResume=""
vInitrd_create=""	# when vInitrd does not exist and it is needed to create one.
ROOT_FS_MD=""		# new vKroot pointing to a mdarray

KERNEL_VERSION=
BASH_VERSION=
MDADM_VERSION=
PARTED_VERSION=

declare -a DISK				# a variable array with each dimension with the diskname.
							# ex: DISK[0]=/dev/sda  : the 0 index must be the single disk.
LASTDISK=""
## Error messages
DISK_OK=0
DISK_DOES_NOT_EXIST=1
DISK_NOT_EMPTY=2
DISK_WITH_RAID=3
DISK_NOT_MOUNTED=4
DISK_NON_LINUX=5
DISK1_DOES_NOT_EXIST_NOT_DEVICE=6
DISKS_ARE_THE_SAME=7
VARIABLE_NOT_EMPTY=8
VARIABLE_EMPTY=9

DISK_NOT_ENOUGH_SPACE=10
KERNEL_RAID_UNABLE=11
DISK_INCOMPATIBLE=12
FATAL_ERROR=99
ERROR_MESSAGE=100

declare -a DISKSTATUS		# variable array - each dimension with status of the disk 
declare -a ERRORS_N
declare -a ERRORS_V

TRAP_TRIGGERED=""			# used in progress bar
BAR_SEMAF=""				# semaphore used in progress bar


COLORIZED=0
if [ $COLORIZED -eq 1 ]; then
	cEnd="\e[0m"	# reset color at the end 
	cB="\e[1m"	# Bold
	cInv="\e[7m"	# Inverse
	cBoldCyan="\e[1;36m"
	cGreen="\e[0;32m"
	cBoldGreen="\e[1;32m"
	TAB_="${cB} :: ${cEnd}"
else
	cEnd=""	# reset color at the end 
	cB=""	# Bold
	cInv=""	# Inverse
	cBoldCyan=""
	cGreen=""
	cBoldGreen=""
	TAB_=" :: "
fi


fill_tempfiles_variables() {		# Fill global variables with pathfiles in /var/lib/raider
	DB_DIR=${R_LIB}/DB								# main raider database directory
	TEMP=$DB_DIR/tmp								# multipurpose temp directory
	DISK_DB=$DB_DIR/dev-disk.db						# main disk partition information database
	DISK_DB_VIRTUAL=$DB_DIR/dev-virtual-disk.db		# virtual disk partition database (with a new /boot partition)
	SYSTEM_DISK=$DB_DIR/system-disk.name			# system disk name 
	NON_SYSTEM_DISKS=$DB_DIR/non-system-disks.name	# non-system disks available
	LIST_DISKS=$DB_DIR/LISTDISKS					# directory to save information about all disks
	CONF_DIR=$DB_DIR/conf							# directory to save configuration files for raid array
	GRUB1_RUN_SCRIPT=$DB_DIR/grub1_run_script		# script to install grub1 inside chroot
	
	LIST_ARRAYS=$TEMP/LISTARRAYS					# directory to save information about all raid arrays
	MBRFILE=$TEMP/mbr.bin							# file with a mbr copy

	MOUNT_CLEAN=$TEMP/mount_clean					# temp file with a cleaner mount file 
	MOUNT_CLEAN_VIRTUAL=$TEMP/mount_clean_virtual
	PARTITION_INFO=$TEMP/partinfo					# temp file with disk partition info 
	NEWMENULST=$TEMP/menulst-copy.raider			# copy of menu.lst in raider directory
		
	NEWMDADM_CONF=$CONF_DIR/mdadm.raider					# The new mdadm.conf to be written in the new raid disk
	NEWFSTAB=$CONF_DIR/fstab.raider							# New /etc/fstab
	NEWMTAB=$CONF_DIR/mtab.raider							# New /etc/mtab
	NEWMENULST_MD=$CONF_DIR/menulst-raid.raider				# menu.lst used in the new mdarray
	NEWMENULST_MD_EXTRA1=$CONF_DIR/menulst-raid-extra1.raider
	MENULST_RESTART=$CONF_DIR/menulst-temprestart.raider	# temporary grub.conf used to restart after creating mdarray
	NEW_MKINITCPIO=$CONF_DIR/mkinitcpio.raider				# temporary mkinitcpio.conf (config file in archlinux)
	GRUB_CONF=$DB_DIR/GRUB
	VARSFILE=$DB_DIR/vars							# file to save important variables
	MOUNTPOINT=$DB_DIR/mountpoint					# mountpoint directory
	TMPGRUBS=$TEMP/grubs							# temporary grub file
	TMPLILOS=$TEMP/lilos							# temporary lilo file
	GRUBLILOLOG=${TEMP}/grublilo.log				# grublilo.log file
	TRAP_TRIGGERED=$TEMP/trap_triggered				# used in progress bar
	BAR_SEMAF=$TEMP/bar-raider-semaf				# semaphore used in progress bar
	
}

