#!/usr/bin/perl

use utf8;
use 5.010;

use strict;
use warnings;

use vars qw( $VERSION );

use App::alios qw(:all);

use Term::ANSIColor;
use Getopt::Std;
use open qw< :encoding(UTF-8) >;
use autodie;

$VERSION = "v2.8.91"; 

my $opt = {};
getopts('vsd:pm:n:h', $opt);

sub version {
	print "$VERSION\n";
}

sub search {
	my $app = init();
	for(@$app){
		print " " x (length $_->{id}) . $_->{name} . colored(['yellow'], "__") . colored(['black on_yellow'], " $_->{nr}") . "\n"; 
	}
}

sub show {
	my $apps = conf();
	print colored(['black on_yellow'], " alios "); for my $app(@$apps){ print " $app->{alios}" }; print " " . colored(['black on_yellow'], " ") . "\n";
}

sub help {
	print "\n" . colored(['bold white'], "alios") . " - CLI tool for quick jumps into iOS app folders\n\n"
	. "\t" . colored(['bold white'], "-v") . " show version\n"
	. "\t" . colored(['bold white'], "-p") . " repath\n"
	. "\t" . colored(['bold white'], "-s") . " search for installed apps\n"
	. "\t" . colored(['bold white'], "-m") . " map alios\n"
	. "\t" . colored(['bold white'], "-d") . " delete mapped alios\n"
	. "\t" . colored(['bold white'], "-h") . " show this help\n\n"
	. colored(['bold white'], "E.g") . "\n"
	. "\t'" . colored(['bold white'], "alios -p") . "' repath the path to app home folders\n"
	. "\t'" . colored(['bold white'], "alios -s") . "' search for installed apps\n"
	. "\t'" . colored(['bold white'], "alios -m 44 -n somename") . "' map 44th app to somename\n"
	. "\t'" . colored(['bold white'], "alios") . "' list alioses\n"
	. "\t'" . colored(['bold white'], "somename") . "' jump to somename app folder\n"
	. "\t'" . colored(['bold white'], "find \$SOMENAME -name \'*.jpg\'") . "' find jpg files in somename app folder\n"
	. "\t'" . colored(['bold white'], "open \$somename") . "' open somename app\n"
	. "\t'" . colored(['bold white'], "alios -d name") . "' delete alios\n\n";
}


if($opt->{m} and $opt->{n}){ $map->($opt->{m}, $opt->{n}); print "'" . colored(['bold white'], "source ~/.alios") . "' to make changes available.\n" }
elsif($opt->{s}){ search }
elsif($opt->{p}){ conf(1) }
elsif($opt->{d}){ del($opt->{d}) and print "Restart shell session '" . colored(['bold white'], "exec \$SHELL") . "' to make changes available.\n" }
elsif($opt->{h}){ help }
elsif($opt->{v}){ version }
else { show };

=encoding utf8

=head1 NAME

alios - tool for quick jumps into iOS app folders 

=head1 VERSION

This document describes alios version 2.8.8

=head1 SYNOPSIS

C<-v> show version 

C<-p> app folder names are changing, this option will update path in ~/.alios config file. To have paths updated at the start of bash session add following line to your ~/.bashrc file 'alios -p && if [ -f ~/.alios ];then source ~/.alios; fi' 

C<-s> search for installed apps

C<-m> map alios

C<-d> delete alios

C<-h> show help


=head1 DESCRIPTION

Creates shell variable '$APP' holding app home folder path, alias 'app' pointing to app home folder and shell variable '$app' holding app id.

=head1 EXAMPLES

C<alios -p> repath the path to app home folders

C<alios -s> search for installed apps

C<alios -m 44 -n name> map 44th app to somename

C<somename> jump to somename app folder

C<find $SOMENAME -name '*jpg'> find jpg files in somename app folder

C<open $somename> open somename app

C<alios> list alioses

C<alios -d name> delete alios


=head1 DEVELOPMENT

alios is hosted on L<github|https://github.com/z448/alios>. You can track and contribute to its development there.

=head1 AUTHOR

Zdeněk Bohuněk, C<< <4zdenek@gmail.com> >>

=head1 COPYRIGHT

Copyright © 2016-2023, Zdeněk Bohuněk C<< <4zdenek@gmail.com> >>. All rights reserved. 

This code is available under the Artistic License 2.0.

=cut
