@@ -91,6 +91,87 @@ func (l *LinuxJoiner) GetName() string {
9191 return fmt .Sprintf ("Linux %s" , l .backend )
9292}
9393
94+ // detectLinuxDistribution detects the Linux distribution
95+ func detectLinuxDistribution () string {
96+ // Check for common distribution files
97+ distFiles := map [string ]string {
98+ "/etc/os-release" : "" ,
99+ "/etc/debian_version" : "debian" ,
100+ "/etc/redhat-release" : "redhat" ,
101+ "/etc/fedora-release" : "fedora" ,
102+ "/etc/arch-release" : "arch" ,
103+ "/etc/SuSE-release" : "suse" ,
104+ }
105+
106+ for file , dist := range distFiles {
107+ if fileExists (file ) {
108+ if dist != "" {
109+ return dist
110+ }
111+ // Read os-release file for more detailed info
112+ if file == "/etc/os-release" {
113+ content , err := os .ReadFile (file )
114+ if err == nil {
115+ contentStr := strings .ToLower (string (content ))
116+ if strings .Contains (contentStr , "ubuntu" ) || strings .Contains (contentStr , "debian" ) {
117+ return "debian"
118+ } else if strings .Contains (contentStr , "fedora" ) {
119+ return "fedora"
120+ } else if strings .Contains (contentStr , "centos" ) || strings .Contains (contentStr , "rhel" ) {
121+ return "redhat"
122+ } else if strings .Contains (contentStr , "arch" ) {
123+ return "arch"
124+ }
125+ }
126+ }
127+ }
128+ }
129+
130+ // Default to debian if we can't detect
131+ return "debian"
132+ }
133+
134+ // installPDFTools installs PDF tools based on the Linux distribution
135+ func installPDFTools (distribution string ) error {
136+ var cmd * exec.Cmd
137+
138+ switch distribution {
139+ case "debian" , "ubuntu" :
140+ fmt .Println ("Installing PDF tools for Debian/Ubuntu..." )
141+ cmd = exec .Command ("sudo" , "apt-get" , "update" )
142+ if err := cmd .Run (); err != nil {
143+ return fmt .Errorf ("failed to update package list: %v" , err )
144+ }
145+ cmd = exec .Command ("sudo" , "apt-get" , "install" , "-y" , "poppler-utils" , "ghostscript" )
146+ if err := cmd .Run (); err != nil {
147+ return fmt .Errorf ("failed to install PDF tools: %v" , err )
148+ }
149+ case "fedora" :
150+ fmt .Println ("Installing PDF tools for Fedora..." )
151+ cmd = exec .Command ("sudo" , "dnf" , "install" , "-y" , "poppler-utils" , "ghostscript" )
152+ if err := cmd .Run (); err != nil {
153+ return fmt .Errorf ("failed to install PDF tools: %v" , err )
154+ }
155+ case "redhat" , "centos" :
156+ fmt .Println ("Installing PDF tools for RHEL/CentOS..." )
157+ cmd = exec .Command ("sudo" , "yum" , "install" , "-y" , "poppler-utils" , "ghostscript" )
158+ if err := cmd .Run (); err != nil {
159+ return fmt .Errorf ("failed to install PDF tools: %v" , err )
160+ }
161+ case "arch" :
162+ fmt .Println ("Installing PDF tools for Arch Linux..." )
163+ cmd = exec .Command ("sudo" , "pacman" , "-S" , "--noconfirm" , "poppler" , "ghostscript" )
164+ if err := cmd .Run (); err != nil {
165+ return fmt .Errorf ("failed to install PDF tools: %v" , err )
166+ }
167+ default :
168+ return fmt .Errorf ("unsupported Linux distribution: %s" , distribution )
169+ }
170+
171+ fmt .Println ("PDF tools installed successfully!" )
172+ return nil
173+ }
174+
94175// NewLinuxJoiner creates a new LinuxJoiner with the best available backend
95176func NewLinuxJoiner () (* LinuxJoiner , error ) {
96177 // Priority order of backends to try
@@ -102,6 +183,23 @@ func NewLinuxJoiner() (*LinuxJoiner, error) {
102183 }
103184 }
104185
186+ // If no backend is available, try to install them
187+ fmt .Println ("No PDF joining tools found. Attempting to install them..." )
188+
189+ distribution := detectLinuxDistribution ()
190+ fmt .Printf ("Detected Linux distribution: %s\n " , distribution )
191+
192+ if err := installPDFTools (distribution ); err != nil {
193+ return nil , fmt .Errorf ("failed to install PDF tools: %v" , err )
194+ }
195+
196+ // Try again after installation
197+ for _ , backend := range backends {
198+ if _ , err := exec .LookPath (backend ); err == nil {
199+ return & LinuxJoiner {backend : backend , command : backend }, nil
200+ }
201+ }
202+
105203 return nil , fmt .Errorf ("no suitable PDF joining tool found on Linux. Please install one of: %s" , strings .Join (backends , ", " ))
106204}
107205
0 commit comments