#------------------------------------------------------
# Vars and configs
#------------------------------------------------------
# set display
export DISPLAY=:0
# find alternative apps if it is installed on your system
find_alt() { for i;do which "$i" >/dev/null && { echo "$i"; return 0;};done;return 1; }
# Use the first program that it detects in the array as the default app
export OPENER=$(find_alt xdg-open exo-open gnome-open )
export BROWSER=$(find_alt firefox chromium chromium-browser google-chrome $OPENER )
export BROWSERCLI=$(find_alt links2 links lynx w3m elinks $OPENER )
export EDITOR=$(find_alt nano efte $OPENER )
export FILEMANAGER=$(find_alt pcmanfm thunar nautilus dolphin spacefm enlightenment_filemanager $OPENER )
export MUSICER=$(find_alt mocp mpv mplayer mpg123 cvlc $OPENER )
export PAGER=$(find_alt bat less more most)
export PLAYER=$(find_alt mpv mplayer cvlc $OPENER )
export IMAGEVIEWER=$(find_alt feh display eog $OPENER )

#EXA COlors
export EXA_COLORS="ur=33:uw=33:ue=33:ux=33:gr=33:gw=33:gx=33:tr=33:tw=33:tx=33:xa=33:sn=0;37:sb=1;30:uu=1;37:un=0;37:gu=1;30:gn=0;30:da=1;30:lp=0;35:bO=1;35:cc=1;33"

# main path
export path_doc=~/Documents
export path_dl=~/Downloads
export path_pic=~/Pictures
export path_pub=~/Public
export path_storage=~/Storage
export path_vids=~/Videos
export path_scripts=~/scripts

# color for common commands
# tutorial video: http://www.youtube.com/watch?v=RbVCzxnKJL4
if [ -x /usr/bin/dircolors ]; then
eval "`dircolors -b`"
alias ls='exa -t modified --time-style long-iso --group-directories-first -g -l'
alias l='/usr/bin/ls --color'

#alias ls='ls --color=auto --group-directories-first --si'
alias lsgrep="ls -la | grep -i"
alias dir='dir --color=auto'
alias cd..='cd ..'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
#------------------------------------------------------

#------------------------------------------------------
# Config
#------------------------------------------------------
cfg-fstab() { sudo $EDITOR /etc/fstab ;}
cfg-group() { sudo $EDITOR /etc/group ;}
cfg-hosts() { sudo $EDITOR /etc/hosts ;}
cfg-lightdm() { sudo $EDITOR /etc/lightdm/lightdm.conf ;}
cfg-mplayer() { $EDITOR ~/.mplayer/config ;}
cfg-nanorc() { $EDITOR ~/.nanorc;}
cfg-sudoers() { sudo $EDITOR /etc/sudoers ;}
cfg-sourcelist() { sudo $EDITOR /etc/apt/sources.list ;}
cfg-tmuxrc() { $EDITOR ~/.tmux.conf ;}
cfg-aliases() { $EDITOR ~/.bash_aliases ;}
cfg-openbox() { $EDITOR ~/.config/openbox/lubuntu-rc.xml ;}
cfg-autostart() { $EDITOR ~/.config/lxsession/Lubuntu/autostart ;}
cfg-mame() { $EDITOR ~/.mame/mame.ini ;}
cfg-dosbox() { $EDITOR ~/.dosbox/dosbox-0.74-3.conf ;}
cfg-conky() { $EDITOR ~/.conkyrc ;}
#------------------------------------------------------

#------------------------------------------------------
# Aliases for Linux Commands, that make them better
#------------------------------------------------------
alias dd="dd status=progress"
alias wget='wget -c' 
alias free='free -h' 
alias fte='sfte'
alias lsd='ls -d */' 
alias bat='batcat --style plain'

#------------------------------------------------------
# Various Aliases
#------------------------------------------------------
#CDB Alias
alias c=". /home/x/scripts/cdb"
#ClamAV Aliases
alias clam='clamscan -i -r'
alias clamexcl="clamscan -i -r -exclude='\.(zip|ZIP)'$"
#font cache
alias font-refresh='fc-cache -f -v'
alias font-list='fc-list'
#make a zim file
alias zim-create='echo zimwriterfs -w index.html -I icon.png -c "xqtr" -p "xqtr" -t "Another Droid BBS" -d "Homepage of Another Droid BBS" -l en -i andr01d.zapto.org/ andr01dbbs.zim'
alias dmk="pwd >> ~/.cd_bookmarks"
alias cdmk="cd \$(cat ~/.cd_bookmarks | fzf)"
alias audio_record='arecord -f cd -t raw | oggenc - -r -o'
alias dirsize='du -hs'
#sudo nano that loads the user config file
alias snano='sudo nano --rcfile=/home/x/.nanorc'
#partial, find non case sensitive search
ffind() {
  find . -iname "*$@*"
}
#QRCode, with qrencode
alias qr="qrencode -lM -m2 -tutf8"

#copies a file to the ~/backups dir!
backup() {
    file=${1:?"error: I need a file to backup"}

    timestamp=$(date '+%Y-%m-%d-%H:%M:%S')
    backupdir=~/backups

    [ -d ${backupdir} ] || mkdir -p ${backupdir}
    cp -a ${file} ${backupdir}/$(basename ${file}).${timestamp}
    return $?
}

alias hgrep='history | grep -i'
alias aliaslist='cat ~/.bash_aliases | fzf -e -i'
alias aliasedit='nano ~/.bash_aliases'
alias start='xdg-open'
alias ramdisk_on='sudo mkdir -p /media/ramdisk && sudo mount -t tmpfs -o size=128M tmpfs /media/ramdisk'
alias ramdisk_off='sudo umount /media/ramdisk'
alias clean_thumb='find ~/.thumbnails -type f -atime +7 -exec rm {} \;'
alias resizeall='mogrify -format png -resize 800'
alias imfonts='identify -list font'

#------------------------------------------------------
# System Aliases
#------------------------------------------------------
alias clearmem='sudo sysctl -w vm.drop_caches=3 && sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches'
distro() { uname -a && lsb_release -a ;}
alias sysinfo='inxi -b'

#------------------------------------------------------
# wget Aliases
#------------------------------------------------------
#usage: mirros-site http://site.com
alias wget-mirror-site='wget -mkEpnp --wait=1'
#retrieves a ftp file
alias wget-ftp="wget --user=anonymous --password='xqtr@gmx.com'"

#------------------------------------------------------
# apt Aliases
#------------------------------------------------------
alias install='sudo apt install'
alias remove='sudo apt remove'
alias purge='sudo apt-get remove --purge'
alias update='sudo apt-get update'
alias upgrade='sudo apt-get upgrade'
alias clean='sudo apt-get autoclean && sudo apt-get autoremove'
alias search='apt-cache search'
alias show='apt-cache show'
alias sources='gksudo gedit /etc/apt/sources.list '
alias repos='sudo add-apt-repository'
alias mergelist_problem='sudo rm /var/lib/apt/lists/* -vf & sudo apt-get update'
alias slowinstall='sudo apt-get -o Acquire::http::Dl-Limit=100 install'
alias slowupdate='sudo apt-get update && sudo apt-get -o Acquire::http::Dl-Limit=80 upgrade'
alias fixkeys='sudo apt-get update 2> /tmp/keymissing; for key in $(grep "NO_PUBKEY" /tmp/keymissing |sed "s/.*NO_PUBKEY //"); do echo -e "\nProcessing key: $key"; gpg --keyserver pool.sks-keyservers.net --recv $key && gpg --export --armor $key | sudo apt-key add -; done'

#------------------------------------------------------
#-------- Various Internet Related Aliases
#------------------------------------------------------
alias xampp='sudo /opt/lampp/xampp start'
alias getip='dig +short myip.opendns.com @resolver1.opendns.com'
#alias extract='patool extract'
alias gopher='lynx gopher://'

#------------------------------------------------------
#-------- Youtube Related Aliases
#------------------------------------------------------
utmp3stream() {
	yt-dlp --audio-format best "$1" -o - | ffplay -nodisp -autoexit -i -
}

utplay() {
  yt-dlp -o - "$1" | vlc -
}

#list of videos in a channel ID:TITLE
utchanlist(){
  yt-dlp --print "%(id)s;%(title)s" "$1"
}
alias utmp3='yt-dlp -R5 -c --extract-audio --audio-format=mp3 ' 
alias utmobile='yt-dlp -f278+139 '
alias hate-mp3='utmp3 https://www.youtube.com/channel/UC6qQOTx9LuKMC5p2dbjmSRg --playlist-start 1 --playlist-end 20'
alias utmp4='yt-dlp -S res,ext:mp4:m4a --recode mp4 '

#------------------------------------------------------
#-------- Network Related Aliases
#------------------------------------------------------
alias netscan="sudo arp-scan -l"

#------------------------------------------------------
#-------- File related aliases
#------------------------------------------------------

# get the chmod stats of the files
# https://bbs.archlinux.org/viewtopic.php?pid=1280296#p1280296
getmod () { stat --format "%n %a" $* | column -t; }

#------------------------------------------------------
#-------- GIT
#------------------------------------------------------
git-push() { git add * ; git commit -m "$1" ; git push ;}
function git-dir-download () {
  svn checkout $(echo "$1" | sed "s/tree\/master/trunk/g")
}

#------------------------------------------------------
#-------- Configurations Reload {{{
#------------------------------------------------------
cfg-rld-bashrc() { source ~/.bashrc ;}
cfg-rld-aliases() { source ~/.bash_aliases ;}
cfg-rld-openbox() { openbox --reconfigure ;}

#------------------------------------------------------
# Dpkg
#------------------------------------------------------
alias dpkg-findinstalled='dpkg --get-selections | grep $1'
alias dpkg-showinstalled='dpkg --get-selections'
alias dpkg-deb-show='dpkg --info' #to examine archive files
alias dpkg-deb-content='dpkg --contents' #to list their contents.
dpkg-whichpkg() { dpkg -S $( which $1 ); } #lookup for what package uses this command
dpkg-cmdpkg() { PACKAGE=$(dpkg -S $(which $1) | cut -d':' -f1); echo "[${PACKAGE}]"; dpkg -s "${PACKAGE}" ;}
alias dpkg-history-install='cat /var/log/dpkg.log | grep "install "'
alias dpkg-history-upgrade='cat /var/log/dpkg.log | grep upgrade'
alias dpkg-history-remove='cat /var/log/dpkg.log | grep remove'
dpkg-history-rollback() { cat /var/log/dpkg.log | grep upgrade | grep "$2" -A10000000 | grep "$3" -B10000000 |awk '{print $4"="$5}';}

dpkg-hold() {
	echo -n "Please enter the package you wish to put a hold on:"
	read progID
	echo "$progID hold" |sudo dpkg --set-selections
}

dpkg-unhold() {
	echo -n "Please enter the package you wish to remove the hold on:"
	read progID
	echo "$progID install" |sudo dpkg --set-selections
}
#------------------------------------------------------

#------------------------------------------------------
# MPS Youtube
#------------------------------------------------------
mpsyt-metaljesusrocks() { mpsyt user metaljesusrocks ;}
mpsyt-alonewolverine1984() { mpsyt user alonewolverine1984 ;}
mpsyt-commsprepper() { mpsyt user commsprepper ;}
mpsyt-darkpreppers() { mpsyt user Marmolejo2050 ;}
mpsyt-fugazigr() { mpsyt user UC0bZ1eMlfGkETT0jFjch_6w ;}
mpsyt-gamesack() { mpsyt user MrGameSack ;}
mpsyt-gotbletu() { mpsyt user gotbletu ;}
mpsyt-guerillacom() { mpsyt user GUERRILLAGEEK ;}
mpsyt-mightycarmods() { mpsyt user mightycarmods ;}
mpsyt-wranglerstar() { mpsyt user wranglerstar ;}
mpsyt-thebenheckshow() { mpsyt user thebenheckshow ;}
mpsyt-tomaskim() { mpsyt user yeosujjang ;}
mpsyt-pathfinder() { mpsyt user wildernessoutfitters ;}
mpsyt-retroliberty() { mpsyt user retroliberty ;}
mpsyt-linuxgamecast() { mpsyt user linuxgamecast ;}
mpsyt-tinyhouse-kirstendirksen() { mpsyt user kirstendirksen ;}
mpsyt-tinyhouse-livingbigtinyhouse() { mpsyt user livingbigtinyhouse ;}
mpsyt-tinyhouse-relaxshacksDOTcom() { mpsyt user relaxshacksDOTcom ;}
mpsyt-tinyhouse-tinyhousegj() { mpsyt user tinyhousegj ;}

#------------------------------------------------------
#ffmpeg
#------------------------------------------------------
#Flip Vertical
alias ffmpeg-flipv='ffmpeg -vf "vflip" -i'
#Flip Horizontal
alias ffmpeg-fliph='ffmpeg -vf "hflip" -i'
# convert video to avi container
# usage: vid2avi file.mpg
ffmpeg-vid2avi() { ffmpeg -i "$1" -vcodec mpeg4 -sameq "${1%.*}.vid2avi.avi" ;}
# combine multiple avi files into a single avi file
# tutorial video: https://www.youtube.com/watch?v=EAWGFJoZXAU
ffmpeg-combine-avi() { avimerge -o combineavi_`date +'%F_%Hh%M'`.avi -i "$@" ;}
ffmpeg-webcam-mplayer() { mplayer tv:// -tv driver=v4l2:width=1280:height=720:device=/dev/video0 -fps 30 -vf screenshot ;}

# QuickTerm -------------------------------------------
alias qterm='~/scripts/quickterm/qterm'

#------------------------------------------------------
# snippets
#------------------------------------------------------
alias multisnippet='~/scripts/multisnippets/multi-snippets.sh'
alias snippet='~/scripts/snippets/snippets.sh'
alias cfg-multisnippet='~/scripts/multisnippets/edit-multi-snippets.sh'
alias cfg-snippet='$EDITOR ~/scripts/snippets/snippets.txt'

#------------------------------------------------------
#fzf
#------------------------------------------------------
fzf-locate() { xdg-open "$(locate "*" | fzf -e)" ;}
fzf-music() { mplayer "$(find ~/Music -type f | fzf -e)" ;}
fzf-bash() { 
    # location of snippets
    dir=~/scripts/multisnippet/bash

    # merge filename and tags into single line
    results=$(for FILE in $dir/*
    do
        getname=$(basename $FILE)
        gettags=$(head -n 1 $FILE)

        echo "$getname		$gettags "

    done)

    # copy content into clipboard without tags
    filename=$(echo -e "$(echo "$results" | fzf -e -i )" | cut -d' ' -f 1)
    sed 1d $dir/$filename | xclip -selection clipboard
}

fzf-python() { 
    # location of snippets
    dir=~/scripts/multisnippet/python3

    # merge filename and tags into single line
    results=$(for FILE in $dir/*
    do
        getname=$(basename $FILE)
        gettags=$(head -n 1 $FILE)

        echo "$getname		$gettags "

    done)

    # copy content into clipboard without tags
    filename=$(echo -e "$(echo "$results" | fzf -e -i )" | cut -d' ' -f 1)
    sed 1d $dir/$filename | xclip -selection clipboard
}

# fcd - cd to selected directory
fzf-cd() {
  local dir
  dir=$(find ${1:-*} -path '*/\.*' -prune \
                  -o -type d -print 2> /dev/null | fzf +m) &&
  cd "$dir"
}

# fhistory - repeat history
fzf-history() {
  eval $(([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s | sed 's/ *[0-9]* *//')
}

fzf-locate() { xdg-open "$(locate "*" | fzf -e)" ;}

#------------------------------------------------------
# Help files and Hints
#------------------------------------------------------
hint-bash-hotkey() { # {{{
cat <<EOF
Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L Clears the Screen, similar to the clear command
Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H Same as backspace
Ctrl + R Let’s you search through previously used commands
Ctrl + C Kill whatever you are running
Ctrl + D Exit the current shell
Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W Delete the word before the cursor
Ctrl + K Clear the line after the cursor
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor
Alt + F Move cursor forward one word on the current line
Alt + B Move cursor backward one word on the current line
Tab Auto-complete files and folder names
EOF
}

hint-github() { # {{{
cat <<EOF
Loginto Github make new repo then
Global setup:
Set up git - https://help.github.com/articles/set-up-git
git config --global user.name "Your Name"
git config --global user.email gotbletu@gmail.com
Next steps:
mkdir dotfiles
cd dotfiles
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin https://github.com/gotbletu/dotfiles.git
git push -u origin master
Existing Git Repo?
cd existing_git_repo
git remote add origin https://github.com/gotbletu/dotfiles.git
git push -u origin master
Importing a Subversion Repo?
Check out the guide for step by step instructions.
https://help.github.com/articles/importing-from-subversion
When youre done:
Continue
https://github.com/gotbletu/dotfiles
==================
mkdir code-dump
cd code-dump
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@github.com:gotbletu/code-dump.git
git push -u origin master
------
add new ssh key; make sure openssh is installed
https://help.github.com/articles/generating-ssh-keys
EOF
}

hint-dollarsign() { # {{{
cat <<EOF
http://linuxsig.org/files/bash_scripting.html
\$# Stores the number of command-line arguments that were passed to the shell program.
\$_ Stores the last input
\$? Stores the exit value of the last command that was executed.
\$0 Stores the first word of the entered command (the name of the shell program).
\$* Stores all the arguments that were entered on the command line ($1 $2 ...).
"\$@" Stores all the arguments that were entered on the command line, individually quoted ("\$1" "\$2" ...).
EOF
}

#------------------------------------------------------
# ISO Mount
#------------------------------------------------------
mount-iso() {
DIR=/tmp/ISO_Mounted_Image
# check if directory doesnt exist then create
if [ ! -d "$DIR" ]; then
mkdir "$DIR"
fi
sudo mount -o loop "$1" "$DIR"
echo "Your Image is mounted at: \e[0;33m $DIR"
}
unmount-iso() {
DIR=/tmp/ISO_Mounted_Image
sudo umount "$DIR"
}

#------------------------------------------------------
# Personall ALiases
#------------------------------------------------------
alias sshp="ssh pi@192.168.1.201 -p 5555"
